I started a thread yesterday about manually starting BlinkWithoutDelay with a button switch. How to manually start Blink Without Delay - #5 by Pimpom. Thanks to @PaulRB and others, I inserted a while line in setup() and it worked perfectly with my slightly modified version of BWoD.
Then I tried the same principle with a somewhat longer program and it doesn't work so well. The object is to drive four outputs HIGH progressively - not one at a time. That is, 1, 1+2, 1+2+3, 1+2+3+4.
There are two points of failure, one related to the while line and the other not related to it.
- Unrelated to while: I tried to apply the idea of moving zero time forward at each step so that it can be used as the starting time by the next step. thus -
if (currentMillis - previousMillis >= xxx) //where xxx is the desired interval
{
previousMillis = currentMillis;
digitalWrite(n, HIGH); //where n is the particularly pin to go high
}
This doesn't work, with or without while.. It doesn't progress beyond the first step.
- Related to while: I removed the call to move previousMillis forward at each step and specified the time separately for each step. Thus -
if (currentMillis - previousMillis >= xxx) //xxx is different for each step
digitalWrite(n, HIGH);
This works as intended without while in setup. But the process begins immediately at startup. When I introduce while in setup, things become erratic. Either all outputs go HIGH immediately or only one or two happen in progression.
The initial code is as below, modifications tried as above and in other ways. What am I doing wrong?
unsigned long previousMillis = 0;
void setup ()
{
pinMode(SREG_T, INPUT_PULLUP); //Reset
pinMode(2, INPUT_PULLUP); //Start countdown
pinMode(3, INPUT_PULLUP); // Jumpstart L
pinMode(4, INPUT_PULLUP); // Jumpstart R
pinMode(5, INPUT_PULLUP); //Reserved
pinMode(7, OUTPUT); //Count1 amber
pinMode(8, OUTPUT); //Count2 amber
pinMode(9, OUTPUT); //Count3 amber
pinMode(10, OUTPUT); //GO! green
pinMode(11, OUTPUT); //Jumpstart R red
pinMode(12, OUTPUT); //Jumpstart L red
while (digitalRead(2) == HIGH); //Hold start until pressed
}
void loop()
{
if (digitalRead(3) == LOW) //Jumpstart input L
digitalWrite(12, HIGH); //Jumpstart light L
if (digitalRead(4) == LOW) //Jumpstart input R
digitalWrite(11, HIGH); //Jumpstart light R
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 500)
{
previousMillis = currentMillis;
digitalWrite(7, HIGH);
}
if (currentMillis - previousMillis >= 500)
{
previousMillis = currentMillis;
digitalWrite(8, HIGH);
}
if (currentMillis - previousMillis >= 500)
{
previousMillis = currentMillis;
digitalWrite(9, HIGH);
}
if (currentMillis - previousMillis >= 500)
{
previousMillis = currentMillis;
digitalWrite(10, HIGH);
}
}