Coding The Arduino: "Embedded Systems"
Coding The Arduino: "Embedded Systems"
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)
I/O pins
Reset
9V or 12V battery
Power pins
Brain
SENSOR
COMPUTER
I/O Commands
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
10K PIN 4
Program Structure
/* declare variable names here to be used below */ Comments void { // } void { // } setup()
Commands
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)
b7
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
type
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; }
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