0% found this document useful (0 votes)
28 views13 pages

Activity 2

The document discusses different types of control flow statements in Arduino including if/else statements, for loops, and while loops. It provides examples of blinking LEDs, reading sensor values, and calibrating sensors using these statements.

Uploaded by

electromagnetism
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views13 pages

Activity 2

The document discusses different types of control flow statements in Arduino including if/else statements, for loops, and while loops. It provides examples of blinking LEDs, reading sensor values, and calibrating sensors using these statements.

Uploaded by

electromagnetism
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Activity 2

Learning objectives:
1. Introduction to loops and conditional statements
2. Understand program structure

If Statement (Conditional Statement)


The if() statement is the most basic of all programming control structures. It allows you to make
something happen or not depending on whether a given condition is true or not. It looks like this:

if (someCondition) {
// do stuff if the condition is true
}

There is a common variation called if-else that looks like this:

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

This example demonstrates the use of if() statements.


It reads the state of a potentiometer (an analog input) and turns on an LED
only if the LED goes above a certain threshold level. It prints the analog value
regardless of the level.

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

* Note: On most Arduino boards, there is already an LED on the board


connected to pin 13, so you don't need any extra components for this example.

*/

// These constants won't change:


const int analogPin = A0; // pin that the sensor is attached to
const int ledPin = 13; // pin that the LED is attached to
const int threshold = 400; // an arbitrary threshold level that's in the range of the
analog input

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);

// if the analog value is high enough, turn on the LED:


if (analogValue > threshold) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin,LOW);
}

// print the analog value:


Serial.println(analogValue);
delay(1); // delay in between reads for stability
}
For Loop (aka The Knight Rider)
Often you want to iterate over a series of pins and do something to each one. For instance, this
example blinks 6 LEDsattached the Arduino by using a for() loop to cycle back and forth through
digital pins 2-7. The LEDS are turned on and off, in sequence, by using both the digitalWrite()
and delay() functions .

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

Demonstrates the use of a for() loop.


Lights multiple LEDs in sequence, then in reverse.

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);
}

// loop from the highest pin to the lowest:


for (int thisPin = 7; thisPin >= 2; thisPin--) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
}
While Loop
Sometimes you want everything in the program to stop while a given condition is true. You can
do this using a while loop. This example shows how to use a while loop to calibrate the value of
an analog sensor.

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

This example demonstrates the use of while() statements.

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.

This is a variation on the calibrate example.

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

*/

// These constants won't change:


const int sensorPin = A2; // pin that the sensor is attached to
const int ledPin = 9; // pin that the LED is attached to
const int indicatorLedPin = 13; // pin that the built-in LED is attached to
const int buttonPin = 2; // pin that the button is attached to

// These variables will change:


int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
int sensorValue = 0; // the sensor value

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);

// read the sensor:


sensorValue = analogRead(sensorPin);

// apply the calibration to the sensor reading


sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

// in case the sensor value is outside the range seen during calibration
sensorValue = constrain(sensorValue, 0, 255);

// fade the LED using the calibrated value:


analogWrite(ledPin, sensorValue);
}

void calibrate() {
// turn on the indicator LED to indicate that calibration is happening:
digitalWrite(indicatorLedPin, HIGH);
// read the sensor:
sensorValue = analogRead(sensorPin);

// record the maximum sensor value


if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

// record the minimum sensor value


if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}

You might also like