Download presentation
Presentation is loading. Please wait.
1
Arduino: For Loops
2
DISCLAIMER & USAGE The content of this presentation is for informational purposes only and is intended for students attending Louisiana Tech University only. The authors of this information do 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 damage and injury. Louisiana Tech University, its officers, employees, agents and 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. The Living with the Lab logos should remain attached to each slide, and the work should be attributed to Louisiana Tech University. If you do not agree, then please do not view this content. boosting application-focused learning through student ownership of learning platforms
3
Build this Circuit then Implement the Code
470W digital I/O pin 11 void setup(){ pinMode(11, OUTPUT); for(int i=0; i<5; i++) { digitalWrite(11,HIGH); delay(200); digitalWrite(11,LOW); delay(200); } void loop() {
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,…,22, 21) for(int i=0; i<80; i+=2) { } • increment i by 2 each time (0,2,4,6,…,76,78) for(int i=50; i>=0; i-=5) { } • decrement i by 5 each time (50,45,40,35,…,5,0) for(int i=2; i<100; i=i*1.5) { } • multiply i by 1.5 each time (2,3,4,6,9,13,19,28,42,63,94)
7
Experiment 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
Click here to get serial monitor to display
Play around…ideas for different patterns to display: Click here to get serial monitor to display 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
© 2024 SlidePlayer.com. Inc.
All rights reserved.