hi forum. i have 10 leds. how to light it one by one start from the left most and the right most at the same time and end at the middle and go back to its origin
using for loop.
bellow the code i try, its succeed run from the left most and the right most but at different time rate.
its succeed run from the left (pin 2) most side and the right (11) most side to the middle but from pin 11 to pin pin 7 run more faster than pin 2 to pin 7
when you are not sure what the algorithm should look like, decompose in small steps
turn on led 0 and 9 - pause - turn off led 0 and 9 - pause
turn on led 1 and 8 - pause - turn off led 1 and 8 - pause
turn on led 2 and 7 - pause - turn off led 2 and 7 - pause
turn on led 3 and 6 - pause - turn off led 3 and 6 - pause
turn on led 4 and 5 - pause - turn off led 4 and 5 - pause
turn on led 3 and 6 - pause - turn off led 3 and 6 - pause
turn on led 2 and 7 - pause - turn off led 2 and 7 - pause
turn on led 1 and 8 - pause - turn off led 1 and 8 - pause
turn on led 0 and 9 - pause - turn off led 0 and 9 - pause
so you could have a crude implementation doing all those steps "manually"
but you could notice that the sum of the leds you turn on and off is always 9, so if you were to do
for(int i = 0; i<5; i++) {
// here you could do something on led #i and led #9-i
// what could it be ?
}
for(int i = 3; i>=0; i--) {
// here you could do something on led #i and led #9-i
// what could it be ?
}
In general - Arrays and structs are your friends.
Don't duplicate code in your sketch. Write code once - use it multiple times.
You should not use magic numbers. The I/O pins love to have a functional name.
Do you have experience with programming in C++?
The task can easily be realised with an object.
A structured array contains all information, such as pin addresses for the I/O devices, as well as the information for the timing.
A single service takes care of this information and initiates the intended action.
The structured array makes the sketch scalable until all I/O pins are used up without having to adapt the code for the service.
It is cool stuff, isn´t it?