Activity 2
Activity 2
Learning objectives:
1. Introduction to loops and conditional statements
2. Understand program structure
if (someCondition) {
// do stuff if the condition is true
}
if (someCondition) {
// do stuff if the condition is true
} else {
// do stuff if the condition is false
}
There's also the else-if, where you can check a second condition if the first is false:
if (someCondition) {
// do stuff if the condition is true
} else if (anotherCondition) {
// do stuff only if the first condition is false
// and the second condition is true
}
You'll use if statements all the time. The example below turns on an LED on pin 13 (the built-in
LED on many Arduino boards) if the value read on an analog input goes above a certain
threshold.
Hardware Required
Arduino Board
(1) Potentiometer or variable resistor
(1) 220 ohm resistor
(1) LED
hook-up wire
Circuit
Schematic:
Code
In the code below, a variable called analogValue is used to store the data collected from a
potentiometer connected to the Arduino on analogPin 0. This data is then compared to a
threshold value. If the analog value is found to be above the set threshold the LED connected to
digital pin 13 is turned on. If analogValue is found to be < threshold, the LED remains off.
/*
Conditionals - If statement
The circuit:
* potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital pin 13 to ground
*/
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
We also call this example "Knight Rider" in memory of a TV-series from the 80's where David
Hasselhoff had an AI machine named KITT driving his Pontiac. The car had been augmented
with plenty of LEDs in all possible sizes performing flashy effects. In particular, it had a display
that scanned back and forth across a line, as shown in this exciting fight between KITT and
KARR. This example duplicates the KITT display.
Hardware Required
Arduino Board
(6) 220 ohm resistors
(6) LEDs
hook-up wire
breadboard
Circuit
Connect six LEDS, with 220 ohm resistors in series, to digital pins 2-7 on your Arduino.
Schematic:
Code
The code below begins by utilizing a for() loop to assign digital pins 2-7 as outputs for the 6
LEDs used.
In the main loop of the code, two for() loops are used to loop incrementally, stepping through the
LEDs, one by one, from pin 2 to pin seven. Once pin 7 is lit, the process reverses, stepping
back down through each LED.
/*
For Loop Iteration
The circuit:
* LEDs from pins 2 through 7 to ground
*/
int timer = 100; // The higher the number, the slower the timing.
void setup() {
// use a for loop to initialize each pin as an output:
for (int thisPin = 2; thisPin < 8; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 2; thisPin < 8; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
In the main loop, the sketch below reads the value of a photoresistor on analog pin 0 and uses it
to fade an LED on pin 9. But while a button attached to digital pin 2 is pressed, the program
runs a method called calibrate() that looks for the highest and lowest values of the analog
sensor. When you release the button, the sketch continues with the main loop.
This technique lets you update the maximum and minimum values for the photoresistor when
the lighting conditions change.
Hardware Required
Arduino Board
(1) digital pushbutton or switch
(1) photocell, or analog sensor
(2) 10k ohm resistors
breadboard
Circuit
Connect your analog sensor (e.g. potentiometer, light sensor) on analog input 2 with a 10K ohm
resistor to ground. Connect your button to digital pin, again with a 10K ohm resistor to ground.
Connect your LED to digital pin 9, with a 220 ohm resistor in series.
Schematic:
Code
/*
Conditionals - while statement
While the pushbutton is pressed, the sketch runs the calibration routine.
The sensor readings during the while loop define the minimum and maximum
of expected values from the photo resistor.
The circuit:
* photo resistor connected from +5V to analog in pin 0
* 10K resistor connected from ground to analog in pin 0
* LED connected from digital pin 9 to ground through 220 ohm resistor
* pushbutton attached from pin 2 to +5V
* 10K resistor attached from pin 2 to ground
*/
void setup() {
// set the LED pins as outputs and the switch pin as input:
pinMode(indicatorLedPin, OUTPUT);
pinMode (ledPin, OUTPUT);
pinMode (buttonPin, INPUT);
}
void loop() {
// while the button is pressed, take calibration readings:
while (digitalRead(buttonPin) == HIGH) {
calibrate();
}
// signal the end of the calibration period
digitalWrite(indicatorLedPin, LOW);
// in case the sensor value is outside the range seen during calibration
sensorValue = constrain(sensorValue, 0, 255);
void calibrate() {
// turn on the indicator LED to indicate that calibration is happening:
digitalWrite(indicatorLedPin, HIGH);
// read the sensor:
sensorValue = analogRead(sensorPin);