Presentation is loading. Please wait.

Presentation is loading. Please wait.

using for loops to control LEDs

Similar presentations


Presentation on theme: "using for loops to control LEDs"— Presentation transcript:

1 using for loops to control LEDs
the for statement allows us to repeat a block of commands a limited number of times arduino.cc

2 DISCLAIMER & USAGE The content of this presentation is for informational purposes only and is intended only for students attending Louisiana Tech University. The author of this information does not make any claims as to the validity or accuracy of the information or methods presented. Any procedures demonstrated here are potentially dangerous and could result in injury or damage. Louisiana Tech University and the State of Louisiana, their officers, employees, agents or volunteers, are not liable or responsible for any injuries, illness, damage or losses which may result from your using the materials or ideas, or from your performing the experiments or procedures depicted in this presentation. If you do not agree, then do not view this content. The copyright label, the Louisiana Tech logo, and the “living with the lab” identifier should not be removed from this presentation. You may modify this work for your own purposes as long as attribution is clearly provided.

3 same circuit as before 470W digital I/O pin 2

4 these sketches both make the LED blink 5 times
without for statement with for statement void setup(){ pinMode(11, OUTPUT); digitalWrite(11,HIGH); delay(200); digitalWrite(11,LOW); delay(200); } void loop() { void setup(){ pinMode(11, OUTPUT); for(int i=0; i<5; i++) { digitalWrite(11,HIGH); delay(200); digitalWrite(11,LOW); delay(200); } void loop() {

5 how the for statement works
declare i as an integer (a number that can vary between -32,768 and 32,767) the first value of i will be zero continue to loop as long as i is less than 5 increment i each time through loop i=0 the first time through loop i=1 the second time through loop i=2 the third time through loop i=3 the fourth time through loop i=4 the fifth time through loop for(int i=0; i<5; i++) { digitalWrite(11,HIGH); delay(200); digitalWrite(11,LOW); delay(200); }

6 incrementing for loops
for(int i=0; i<5; i++) { } - increment i by one each time (0,1,2,3,4) for(int i=100; i>20; i--) { } - decrement i by one each time (100,99,98,…) for(int i=0; i<80; i+=2) { } - increment i by 2 each time (0,2,4,6,…) for(int i=50; i>=0; i-=5) { } - decrement i by 5 each time (50,45,40,…) for(int i=2; i<100; i=i*1.5) { } - multiply i by 1.5 each time (2,3,4,6,9,13,…)

7 play around with incrementing and decrementing for loops
void setup() { Serial.begin(9600); for(int x=0; x<100; x++) { Serial.println(x); } void loop() { send output to serial monitor


Download ppt "using for loops to control LEDs"

Similar presentations


Ads by Google