Download presentation
Presentation is loading. Please wait.
Published byDelphia Bishop Modified over 9 years ago
1
using for loops to control LEDs living with the lab 1 1 arduino.cc the for statement allows us to repeat a block of commands a limited number of times © 2012 David Hall
2
living with the lab 2 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. DISCLAIMER & USAGE
3
build this circuit (or just use the built-in LED on pin 13) 470 digital I/O pin 11 living with the lab 3 digital pin 11
4
void setup(){ pinMode(11, OUTPUT); digitalWrite(11,HIGH); delay(200); digitalWrite(11,LOW); delay(200); digitalWrite(11,HIGH); delay(200); digitalWrite(11,LOW); delay(200); digitalWrite(11,HIGH); delay(200); digitalWrite(11,LOW); delay(200); digitalWrite(11,HIGH); delay(200); digitalWrite(11,LOW); delay(200); 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() { } living with the lab 4 these sketches both make the LED blink 5 times without for statementwith for statement
5
for(int i=0; i<5; i++) { digitalWrite(11,HIGH); delay(200); digitalWrite(11,LOW); delay(200); } 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 how the for statement works living with the lab 5 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
6
incrementing for loops living with the lab 6 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
living with the lab 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
8
living with the lab 8 Try to display these patterns: 1,2,3,…100 100,99,98,…-27 0,3,6,9,…69 1000,950,900,…50 1,2,4,8,16,…1024 1024,512,256,…1 1,4,9,16,25,…100
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.