Arduino Coding


The Arduino language is based on the languages C and C++. Arduino code can be written inside the open source Arduino software (download). This software is also used to upload a finished program to the Lilypad main board [tutorial].

A full indexed reference of the Arduino language can be found on the Arduino website. However, before looking at this reference it is a good idea to read through the section on programming principles from this wiki and then to read Leah Buechley's introduction to Arduino programming.

Information and examples for writing code for the various components within the Lilypad kit can be found on the individual component wiki pages. Use the navigation bar at the left to navigate to the component you are interested in.

Week 3 Exercises

The easiest way to learn about the Arduino programming language is to open up the Program, and choose 'Examples' under the File Menu. There are many samples and examples that demonstrate the use of the programming language. In fact often it is a good idea to open up a program that does something similar to what you are trying to achieve and add your functionality to that.

Examples -> Digital -> Blink

This program blinked an LED on and off, once a second. By altering the number in the delay, we could change the length of time (in milliseconds) that the LED would be on or off, and generate patterns of blips.


Examples -> Analog -> AnalogInput

This program allows the use of a sensor to read information in the physical world and converts it to a number that we can then take action on. We used it to blink an LED faster, when there was less light.

This is because the value that came out of the sensor was smaller if there was less light. Our program simply said "Smaller Value" meant "Smaller Delay Time".

As it turned out this interaction was quite limited. The value from the sensor was directly used to control the delay time between each blink. In order to express something more interesting in our interaction, we need to manipulate that number and cause something different to happen, as we choose.

Map Function

The map command allows you to take the input number and map it to a new output number, this means that you can for example invert the behaviour, or amplify a certain range.


In this case we put that result into brightValue like this:

Serial output

Next we saw that in order to choose the Values most appropriate to our needs we had to output something to the screen

in setup loop:

when you want to print sensorValue:

To print text put it in quotes:
Serial.print("SensorValue: ");

After the program is uploaded and we press the Serial Monitor (right most button in the program) we get this:

Other Outputs

We then added another output mode of an external fading LED. This introduced us to the problems of using the delay command, which halts all operations until the timer is reached.

Blink Without Delay

Examples -> Digital -> BlinkWithoutDelay
This program added a whole new level of complexity, because it tried to track the passage of time, by storing the time passed each time the loop was processed. This meant that we had to use a lot of variables and introduce conditionals such as "if" and "else". These are questions that the program asks to see if the code inside the braces { } should be run, or just skipped over.