Coding the Arduino
Embedded Systems
Software
Hardware
Lives in a Virtual World Restart makes all OK Easy to Modify; programmable Expensive: i7 ~$800
Deals with the Real World --Arduous to Modify; NOT programmable Expensive: PCB >$100
Software
Firmware
Embedded Systems
Hardware
Lives in a Virtual World Restart makes all OK Easy to Modify; programmable Expensive: i7 ~$800
Does Both Restart makes Some things OK Easy to Modify; programmable Inexpensive:$1-$30
Deals with the Real World --Arduous to Modify; NOT programmable Expensive: PCB >$100
Software
Computers do exactly what you tell them, no more, no less Arduino uses the C programming language You can go a long ways with just a few instructions See the Arduino Guide (2011 web site)
On the Arduino Programming
cable
I/O pins
Reset
9V or 12V battery
Power pins
Brain
Schematic Icons: Hardware
1K TIP120
Dealing With the Outside World
SENSOR
COMPUTER
ACTUATOR Lamp Relay Motor Solenoid
Switch Light beam Potentiometer Encoder Temperature
I/O Commands
DigitalWrite(n,HIGH); DigitalWrite(n,LOW); DigitalRead(n);
set pin n to +5 V set pin n to 0 V
read state of pin n
Driving Outputs
Program sets pin high/low (1/0) digitalWrite(4,HIGH); digitalWrite(4,LOW); +5V 0V Board pin set to +5V/0V
+12 V
Interface electronics use signal voltages and power supply to switch motor on/off
PIN 4
1K TIP120
Reading Sensors
Program reads value of pins (1/0) digitalRead(4); +5V 0V Board pins set to +5V/0V
+5 V
Interface electronics change sensor signals into +5V/0V
10K PIN 4
Program Structure
/* declare variable names here to be used below */ Comments void { // } void { // } setup()
Commands
commands to initialize go here loop() commands to run your machine go here
Anatomy of a Program
/*--------------------------Turn on LED for sec ----------------------------*/ void setup() { pinMode(2,OUTPUT); digitalWrite(2,HIGH); delay(500); digitalWrite(2,LOW); } void loop() { } // one-time actions
// // // //
define pin 2 as an output pin 2 high (LED on) wait 500 ms pin 2 low (LED off)
// loop forever
Digital Numbers
A bit is one binary digit: 0/1 A byte is 8 bits 00000011 (binary) = 3 (decimal)
11111111 (binary) = 255 (decimal)
b7
b0 Type #bits Number range 0-1
bit
x x x x x x x
byte
x x x x x x x x
0-255
0-65,535 -32,768-32,767
word 16 int 16
Arduino Data Variables
Declare at top of program
type
byte i; word k; int length; int width;
0 to255 0 to 65,536 -32,768 to 32,767
name
Variable Names: Cant have white-space, use camelCase: myVariableName Make them short but meaningful: motorSpd, redLED
Use byte variables unless expecting large numbers; Dont mix types:byte i=266 will roll over to 0
Constant Symbols
#define LED 2 // define the LED pin void setup() { pinMode(LED,OUTPUT); } void loop() { digitalWrite(LED,HIGH); delay(500); digitalWrite(LED,LOW); delay(500); }
Changeable Variables
#define LED 2 // define the LED pin int myDelay = 500; void setup() { pinMode(LED,OUTPUT); } void loop() { digitalWrite(LED,HIGH); delay(myDelay); digitalWrite(LED,LOW); delay(myDelay); myDelay = myDelay - 50; }
Setting Pin Direction
void setup() { pinMode(2,OUTPUT); pinMode(3,INPUT); } void loop() {}
What Does This Program Do?
#define LED 2 // the LED pin byte i,j; void setup() { pinMode(LED,OUTPUT); for (i=0;i<4;i++) { flasher(); delay(5000); } } void loop() {} void flasher() { for (j=0;j<3;j++) { digitalWrite(LED,HIGH); delay(500); digitalWrite(LED,LOW); delay(500); } }
Printing to the Terminal
void setup() { Serial.begin(9600); Serial.println("Hello World"); } void loop() {}
Debugging an Input
void setup() { Serial.begin(9600); } void loop() { // Read from digital pin 2 & // spit the value out to serial Serial.println(digitalRead(2));
// Every 100ms or so delay(100); }
Want More?
Arduino Microcontroller Guide Language Reference section of Arduino site