Arduino
Arduino
2
Mini City
• Light sensitive street lights
• Timed Traffic Lights
• Automated Gates using proximity sensor
• Cars that drive the City streets
3
Arduino Programming
Digital I/O (2 – 13) Serial Transfer (0 -1)
Arduino
IDE
Window
Code Editor
7
Arduino Code Basics
Arduino programs run on two basic sections:
void setup() {
}
void loop() {
}
SETUP
• The setup section is used for assigning input
and outputs (Examples: motors, LED’s,
sensors etc) to ports on the Arduino
• It also specifies whether the device is
OUTPUT or INPUT
• To do this we use the command “pinMode”
9
SETUP
void setup() {
port #
pinMode(9, OUTPUT);
Input or Output
}
http://www.arduino.cc/en/Reference/HomePage
LOOP
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(1000);
Turn the LED on
or off
} Wait for 1 second
or 1000 milliseconds
11
TASK 1
• Using 3 LED’s (red, yellow and green) build a
traffic light that
– Illuminates the green LED for 5 seconds
– Illuminates the yellow LED for 2 seconds
– Illuminates the red LED for 5 seconds
– repeats the sequence
12
TASK 2
• Modify Task 1 to have an advanced green
(blinking green LED) for 3 seconds before
illuminating the green LED for 5 seconds
13
Variables
• A variable is like “bucket”
• It holds numbers or other values temporarily
value
14
DECLARING A VARIABLE
int val = 5;
assignment
Type “becomes”
value
variable name
15
Task
• Replace all delay times with variables
• Replace LED pin numbers with variables
16
USING VARIABLES
int delayTime = 2000;
int greenLED = 9;
void setup() {
Declare delayTime
pinMode(greenLED, OUTPUT); Variable
void loop() {
digitalWrite(greenLED, HIGH);
delay(delayTime);
Use delayTime
digitalWrite(greenLED, LOW);
Variable
delay(delayTime);
}
17
Using Variables
int delayTime = 2000;
int greenLED = 9;
void setup() {
pinMode(greenLED, OUTPUT);
}
void loop() {
digitalWrite(greenLED, HIGH);
delay(delayTime);
digitalWrite(greenLED, LOW);
delayTime = delayTime - 100;
delay(delayTime);
} subtract 100 from
delayTime to gradually
increase LED’s blinking
speed
18
Conditions
a>b a >= b
a<b a <= b
a == b a != b
20
IF Condition
if(true)
{
asdfadsf
“perform some action”
}
IF Example
int counter = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
22
TASK
• Create a program that resets the delayTime
to 2000 once it has reached 0
23
Input & Output
• Transferring data from the computer to an
Arduino is done using Serial Transmission
• To setup Serial communication we use the
following
void setup() {
Serial.begin(9600);
}
24
Writing to the Console
void setup() {
Serial.begin(9600);
Serial.println(“Hello World!”);
void loop() {}
25
Task
• Modify your traffic light code so that each time
a new LED is illuminated the console displays
the status of the stoplight
• Example
Advanced Green
Green
Yellow
Red
Advanced Green
...
26
IF - ELSE Condition
–Example
31
BOOLEAN OPERATORS - OR
• If we want either of the conditions to be true
we need to use ‘OR’ logic (OR gate)
• We use the symbols ||
–Example
32
TASK
• Create a program that illuminates the green
LED if the counter is less than 100,
illuminates the yellow LED if the counter is
between 101 and 200 and illuminates the
red LED if the counter is greater than 200
33
INPUT
• We can also use our Serial connection to get
input from the computer to be used by the
Arduino
int val = 0;void setup() {
Serial.begin(9600); }void loop() {
if(Serial.available() > 0) {
val = Serial.read();
Serial.println(val); }
}
34
Task
• Using input and output commands find the
ASCII values of
36
Task
• Create a program so that when the user
enters 1 the green light is illuminated, 2 the
yellow light is illuminated and 3 the red light
is illuminated
38
BOOLEAN VARIABLES
39
Run-Once Example
boolean done = false;void setup() { Serial.begin(9600);}void loop() {
if(!done) { Serial.println(“HELLO WORLD”);
done = true; }
40
TASK
• Write a program that asks the user for a
number and outputs the number that is
entered. Once the number has been output
the program finishes.
– EXAMPLE:
Please enter a number: 1 <enter>
The number you entered was: 1
41
TASK
• Write a program that asks the user for a
number and outputs the number squared
that is entered. Once the number has been
output the program finishes.
42
Christmas Light Assignment
• Using at least 5 LEDs create a program that
has at least 4 different modes
• Must use if statements and user input to
switch modes
• Marks:
– Creativity [4 Marks]
– Working user input [2 Marks]
– Four Working modes [2 Marks]
– Instructions [1 Mark]
43