Information Technology Principles
(Arduino)
Section 2
Previous Section Task
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
• Note that after each illumination period the LED is turned off!
Previous Section Task
Comments
• Comments are for you – the programmer and your
friends…or anyone else human that might read your
code.
// this is for single line comments
// it’s good to put a description at
the top and before anything ‘tricky’
/* this is for multi-line comments
Like this…
And this….
*/
Comments
Comments
Variables
• A variable is like “bucket”
• It holds numbers or other values temporarily
Value
DECLARING A VARIABLE
int val =
Data Type
5;
Value
Variable Name Assignment
sign
“becomes”
Variable Types
1bits 8 bits 16 bits 32 bits
boolean (bool) byte int long
char unsigned int unsigned long
float
Variables
Variable Scope
•Global
_________________
•Function-level
Using Variables
int delayTime = 2000; Declare delayTime and
int greenLED = 10; greenLED
void setup() { Variable
pinMode(greenLED, OUTPUT);
}
void loop() {
digitalWrite(greenLED, HIGH);
delay(delayTime);
digitalWrite(greenLED, LOW);Use delayTime
Variable
delay(delayTime);
}
Arithmetic Operations
Name Meaning Example Result
+ Addition 34 + 1 35
- Subtraction 34 - 1 33
* Multiplication 300 * 30 9000
/ Division 1.0 / 2.0 0.5
Task
Modify the previous section task:
• Replace all delay times with variables
• Replace LED pin numbers with variables
Using Serial Communication
(Output)
Method used to transfer data between two devices.
Data passes between the computer and Arduino through the USB cable.
Data is transmitted as zeros (‘0’) and ones (‘1’) sequentially.
• To setup Serial communication we use the following
void setup() {
Serial.begin(9600);
}
Using Serial Communication
(Output)
Opens up a Serial
Terminal Window
Writing to the Console
Write a program to write “Hello World!”
Conditions
• To make decisions in Arduino code we use an ‘if’ statement
• ‘If’ statements are based on a TRUE or FALSE question
if(true)
{
“perform some
action”
}
Comparison Operations
<Boolean> Description
( ) == ( ) is equal?
( ) != ( ) is not equal?
() > () greater than
( ) >= ( ) greater than or equal
() < () less than
( ) <= ( ) less than or equal
IF Condition example
Write a program to write from 0 to 9
IF - ELSE Condition
IF - ELSE Condition example
Write a program to write “less than 10” if
counter from 0 to 9 and “greater than or
equal to 10” otherwise
IF - ELSE IF Condition
if( “answer is true”) {
“perform some action”
}
else if( “answer is true”)
{
“perform some other
action”
}
else {
“perform some other
action”
}
IF – ELSE IF Condition example
Write a program to write “less than 10”
if counter from 0 to 9, and “equal to 10”
if counter equal 10, and “greater than 10”
otherwise.
Task 1
• Modify previous section code so that each time a new LED is
illuminated the console displays the status of the stoplight
• Example
Task 2
• Create a program that illuminates the green LED
if the counter is less than 10, illuminates the yellow LED
if the counter is between 11 and 20 and illuminates the
red LED if the counter is greater than 20
THANK YOU