Datasheet

05
03 PROG
programming
primer
MATHS OPERATORS
= (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)
Operators used for
manipulating numbers.
(they work like simple
maths).
COMPARISON OPERATORS
== (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)
Operators used for
logical comparison.
CONTROL STRUCTURE
if(condition){ }
else if( condition ){ }
else { }
This will execute the code between
the 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.
Programs are reliant on
controlling what runs
next, here are the basic
control elements (there
are many more online).
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
digitalWrite(pin, value);
Once a pin is set as an OUTPUT,
it can be set either HIGH (pulled
to +5 volts) or LOW (pulled to
ground).
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
either be INPUT or OUTPUT.
int digitalRead(pin);
Once a pin is set as an INPUT
you can use this to return
whether it is HIGH (pulled to
+5 volts) or LOW (pulled to
ground).
ANALOG
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).
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 analogRead(pin);
When the analog input pins are set
to input you can read their voltage.
A value between 0 (for 0
volts) and 1024 (for
5 volts) will be
returned.
.:For a full programming reference visit:.
http://ardx.org/PROG