PROG
programming primer

.:A Small Programming Primer:.

(ARDX) .:Arduino Experimentation Kit:. (ARDX)

Arduino Programming in Brief

The Arduino is programmed in the C language. This is a quick primer targeted at people who have a little bit of programming experience and just need a briefing on the idiosyncrasies of C and the Arduino IDE. If you find the concepts a bit daunting, don't worry, you can start going through the circuits and pick up most of it along the way. For a more in-depth intro, the Arduino.cc website is a great resource.

Structure

Each Arduino program (often called a sketch) has two required functions (also called routines, or subroutines).

void setup () { }

All the code between the two curly brackets will be executed once when your Arduino program first runs.

void loop () { }

This function is run after setup has finished. After it has run once it will be run again, and again, until power is removed.

Syntax

One of the slightly frustrating elements of C is its formatting requirements (this also makes it very powerful). If you remember the following you should be alright.

// (single line comment)

It is often useful to write notes to yourself as you go along, about what each line of code does. To do this, type two forward slashes and everything until the end of the line will be ignored by your program.

/* */ (multi line comment)

If you have a lot to say, you can span several lines as a comment. Everything between these two symbols will be ignored in your program.

{ } (curly brackets)

Used to define when a block of code starts and ends (used in functions as well as loops).

; (semicolon)

Each statement (usually one line of code) must be ended with a semicolon (a missing semicolon is often the reason for a program refusing to compile).

Variables

A program is nothing more than instructions to move numbers around in an intelligent way. Variables are used to do the moving.

int (integer)

The main workhorse, stores a number in 2 bytes (16 bits). Has no decimal places and will store a value between -32,768 and 32,767.

long (long)

Used when an int is not large enough. Takes 4 bytes (32 bits) of RAM and has a range between -2,147,483,648 and 2,147,483,647.

boolean (boolean)

A simple True or False variable. Useful because it only uses 8 bits of RAM.

float (float)

Used for floating point math (decimals). Takes 4 bytes (32 bits) of RAM and has a range between -3.4028235E+38 and 3.4028235E+38.

char (character)

Stores one character using the ASCII code (IE 'A' = 65). Uses one byte (8 bits) of RAM. The Arduino handles strings as an array of char’s.

Math Operators

Operators used for manipulating numbers. (they work like simple math).

=
(assignment) makes something equal to something else (eg. x = 10 * 2 (x now equals 20))
%
(modulo) gives the remainder when one number is divided by another (ex. 12 % 10 (gives 2))
+
(addition)
-
(subtraction)
*
(multiplication)
/
(division)

Comparison Operators

Operators used for logical comparison.

==
(equal to) (eg. 12 == 10 is FALSE or 12 == 12 is TRUE)
!=
(not equal to) (eg. 12 != 10 is TRUE or 12 != 12 is FALSE)
<
(less than) (eg. 12 < 10 is FALSE or 12 < 12 is FALSE or 12 < 14 is TRUE)
>
(greater than) (eg. 12 > 10 is TRUE or 12 > 12 is FALSE or 12 > 14 is FALSE)

Control Structure

Programs are reliant on controlling what runs next, here are the basic control elements (there are many more documented online).

if( condition ) { }
else if( condition ) { }
else { }

This will execute the code between the first curly brackets if the condition is true, and if not it will test the else if condition if that is also false the else code will execute.

for( int i = 0; i < #repeats; i++ ) { }

Used when you would like to repeat a chunk of code a number of times (can count up i++ or down i-- or use any variable)

Digital

pinMode( pin, mode );

Used to set a pin's mode. Pin is the pin number you would like to address 0-19 (analog 0-5 are 14-19). The mode can INPUT, INPUT_PULLUP, or OUTPUT.

digitalWrite( pin, val );

Once a pin is set as an OUTPUT, its value can be set either HIGH (pulled to +5 volts) or LOW (pulled to ground).

int digitalRead( pin );

Once a pin is set as an INPUT or INPUT_PULLUP, you can use this to return whether it is HIGH (pulled to +5 volts) or LOW (pulled to ground).

Analog

The Arduino is a digital machine but it has the ability to operate in the analog realm (through tricks). Here's how to deal with things that aren't digital.

int analogWrite( pin, value );

Some of the Arduino's pins support pulse width modulation (3, 5, 6, 9, 10, 11). This turns the pin on and off very quickly making it act like an analog output. The value is any number between 0 (0% duty cycle ~0v) and 255 (100% duty cycle ~5 volts).

int analogRead( pin );

When the analog input pins are set to input you can read their voltage. A value between 0 (for 0 volts) and 1023 (for 5 volts) will be returned. The upper (voltage) limit of the range can set using analogReference().