Project #2: Repeating with for Loops When designing a sketch, you’ll often repeat the same function. You could simply copy and paste the function to duplicate it in a sketch, but that’s inefficient and a waste of your Arduino’s program memory. Instead, you can use for loops. The benefit of using a for loop is that you can determine how many times the code inside the loop will repeat. To see how a for loop works, enter the following code as a new sketch: Project 2 - Repeating with for Loops int d = 100; void setup() { pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); } void loop() { First Steps 47 for ( int a = 2; a < 7 ; a++ ) { digitalWrite(a, HIGH); delay(d); digitalWrite(a, LOW); delay(d); } } .................................................................................................................................................................. The for loop will repeat the code within th...
Comments
Post a Comment