" I've found out recently that for loops are as bad as delay()!!"
They can be. If you are stepping backwards through an array , say to reverse it , and your loop control variable is an unsigned integer, you could be in for a long wait.
I've found out recently that for loops are as bad as delay()!!
They are not as bad as delay, but you need to use them carefully. If you use them for something that will take forever (well, even a few milliseconds) then they can be as bad as delay. For (and while) should be used for things you can be sure will complete quickly and will not depend on anything else that might slow them down.
rcayot:
looking fore shortcut. What about reverse_copy?
Make a copy that is the reverse?
Just looking for a way not to iterate through it.
How do you think a "reverse copy" feature would work? It would have to iterate through the array element-by-element and copy them into a new memory area in the reverse sequence. So, not only is that NOT a shortcut, but it uses more memory.
You can do it with swapping (using the degenerated machinery of a swap sort). Swap the first and last, second and second last, etc... but you have to make a special case for an odd number of elements because you will have one extra in the middle that doesn't move.
I have this code with the help of others. It takes a set of six LEDs and pulses them with PWM. The first LED pulses, then after a short delay. the second starts (before the first ends) etc cetera.
The code below works, but what I want to do is reverse the elements in the array so I can then run trhe sequence backwards. If I terate throught the loop, the LEDs will NOT light simultaneously.
/*
pulse LED with PWM
https://forum.arduino.cc/index.php?topic=675980
by noiasca
*/
uint32_t CMillis = 0;
class PulseLed
{
private :
const byte ledPin; // has to be a GPIO with PWM
uint8_t interval = 2; // the intervall in milliseconds for each change
uint32_t previousMillis = 0; // stores the last change
uint8_t pwm = 0; // actual value of PWM
uint8_t start = 0; // range from (lowest PWM value)
uint8_t end = 255; // range to (highest PWM value
uint8_t pulsecount = 0;
int y =0;
bool dir = true;
public :
PulseLed ( byte ledPin) :
ledPin(ledPin)
{}
void begin()
{
pinMode(ledPin, OUTPUT);
}
void setInterval(byte newInterval)
{
interval = newInterval;
}
void setRange( byte newStart, byte newEnd)
{
if (newEnd > newStart)
{
start = newStart;
end = newEnd;
}
}
void setpulsecount(byte newpulsecount)
{
pulsecount = newpulsecount;
}
void tick()
{
if ((millis() - previousMillis > interval) && (pulsecount != 0))
{
previousMillis = millis();
if (y >= start && dir == true)
{
if ((y <= end -1) && (dir == true)) y = y + 17;
else dir = false;
}
else
{
if ((y > start) && (dir == false)) y = y - 1;
else dir = true;
}
analogWrite(ledPin, y);
if (y <= start)
{
pulsecount = (pulsecount - 1);
}
}
}
};
PulseLed pulseLed[] {2, 3, 4, 5, 6, 7};
void setup()
{
Serial.begin(9600);
for (int i = 0; i < 6; i ++)
{
pulseLed[i].setRange(0, 255); // set an individuall PWM range from ... to ...
pulseLed[i].setInterval(2); // set the interval/speed of pulsing
pulseLed[i].setpulsecount(1); // set number of times to pulse
}
CMillis = millis();
}
void loop()
{
pulseLed[0].tick();
if (millis() - CMillis <= 50) {return;}
pulseLed[1].tick();
if (millis() - CMillis <= 100) {return;}
pulseLed[2].tick();
if (millis() - CMillis <= 150) {return;}
pulseLed[3].tick();
if (millis() - CMillis <= 200) {return;}
pulseLed[4].tick();
if (millis() - CMillis <= 250) {return;}
pulseLed[5].tick();
if (millis() - CMillis <= 1100) {return;}
{
// Serial.println(millis() - CMillis);
CMillis = millis();
for (int i = 0; i < 6; i ++)
{
pulseLed[i].setpulsecount(1);
}
}
}
SO, If I could reverse the order pin numbers are in teh array, the void loop will keep running back and forth.
Roger Ayotte
P.S. of course the for loops are not exactly as bad as delay, BUT, my experience, which is very limited, is that the for loop will run exclusively until completed. Perhaps using an if loop in hte middle might work, but at my stage of expertise, I have not been able to accomplish that....... yet!