This document discusses different conditional structures that can be used in Arduino programming such as IF, ELSE, WHILE, and FOR statements. It provides examples of how to use these structures to control an LED by turning it on and off based on different conditions. Finally, it presents exercises for students to create programs that utilize these conditional structures and control LED patterns in various ways.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
55 views16 pages
Experiment 2
This document discusses different conditional structures that can be used in Arduino programming such as IF, ELSE, WHILE, and FOR statements. It provides examples of how to use these structures to control an LED by turning it on and off based on different conditions. Finally, it presents exercises for students to create programs that utilize these conditional structures and control LED patterns in various ways.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16
Laboratory Activity 2
COE 5 Conditional Structure
IF Statements
ELSE Statements
WHILE statements
FOR statements
Combinations and Arrays
IF Statement Control structures allows one to select which code is to be executed and also allow codes to be executed multiple times. The if statement is the first control structure.
absence of const make it a global
variable w/ a start value of 1000
value of delay is changed by subtracting
100 from its original value 1000
if the delay time is less than or equal to 0
then the delay time is set back to 1000
turns the LED on and off. However,
instead of using a fixed number, a variable is used ELSE Statement IF statement can have an ELSE clause which declares a new condition if the IF statement is not true.
done if the delay time is
less than or equal to 100
this is done otherwise,
Note: IF or ELSE statement/code
shall be done but NEVER both WHILE Statement A while statement is just like an if statement except it continues to repeat a block of code (within the curly braces.) as long as the condition is true.
allows the LED to blink FASTER
WHILE Statement cont… allows the LED to blink SLOWER FOR Statement A FOR loop allows a block of code to be executed some number of times
statement 1 condition statement 2 (i = i + 1)-(++ means increment)
Combinations There are ways were one can combine logical conditions Exercise 1: Basic Circuit 2 The purpose of the basic circuit2 is to demo how Arduino will be controlling an LED using conditional control structures.
Write all sketches using conditional structures on the arduino board
and verify the result. Exercise 2: Basic Circuit 3 The purpose of the basic circuit3 is to create different light patterns using conditional structures Exercise 2: sketch Exercise 2: sketch – using Arrays An array is a collection of variables that are indexed with an index number. An example: Number of elements in the array is defined the array is defined
a FOR loop is used to go through
each of the elements in our array and set them as OUTPUT
a FOR loop is used to go through
each of the LEDs and turn them on @ 100 ms delay in between
a FOR loop is used to go backwards
Activity 1. Modify the FOR program to light the LED up 10 times in a row instead of 4. 2. Make a program (sketch) that lights up a single LED five times in a row for one second on and off, and then five times in a row for ½ of a second on and off. 3. Make a program using arrays that lights up the LEDs from top to bottom and then goes backwards so only one LED is on at any time. 4. Make a program that lights up the LEDs in any pattern that you like.
Include comments in your program codes to explain/dissect the overall
program. Have your work checked by your lab professor….. Yours Program Codes Here!
(paste your program codes here)
Observations and Conclusions
(one/two pager only)
References
Arduino Cookbook Recipes to Begin, Expand, and Enhance Your Projects By Michael Margolis