I'm using a for loop to light LEDs 2 -42 on a 2560 in sequence.
I know this could be done with an array. But can it be done with a for loop?
For the first itteration of the loop I would like to start at LED 22 or +20.
Next and all future itterations from 2.
There's not offset in a for loop correct?
I'm trying to think though the logic on this.
byte i = 22; //Initial setting of i to 22 This woud be in the global area or above setup.
void setup ()
for (i; i <=42 ; i = i + x) { // First interation of loop i starts at 22.
}
i=2; // I is now set to 2 so next itteration and all future itterations of the loop are from 2.
Woudl this work? Or is there a better way?
If the first iteration (and only the first) is a special case then I think I would just deal with it separately rather than try to write a complex piece of code to make the general situation deal with the special case.
Doug101:
I'm using a for loop to light LEDs 2 -42 on a 2560 in sequence.
I know this could be done with an array. But can it be done with a for loop?
For the first itteration of the loop I would like to start at LED 22 or +20.
Next and all future itterations from 2.
There's not offset in a for loop correct?
I'm trying to think though the logic on this.
byte i = 22; //Initial setting of i to 22 This woud be in the global area or above setup.
void setup ()
for (i; i <=42 ; i = i + x) { // First interation of loop i starts at 22.
}
i=2; // I is now set to 2 so next itteration and all future itterations of the loop are from 2.
Woudl this work? Or is there a better way?
For the second itteration would it start at 2 or 22?
the array is the better way, IMHO. any time you are using a global variable to traverse through a for-loop you need to be asking yourself "why."
Why, becasue I'm learning. I alreadu stated and array would be better. But why use an array if I can use write less code and accomplish the same thing with less code?
I'm going through the logic to see if it is possible.
Doug101:
Why, becasue I'm learning. I alreadu stated and array would be better. But why use an array if I can use write less code and accomplish the same thing with less code?
I'm going through the logic to see if it is possible.