Reset Time on Switch LOW

Hi all,

What should be fairly simple (I think) is turning out to be a little tricky for me. I'm aiming to move through a sequence of lighting LEDs. This sequence, simple at the moment but more complexity will be added to it, is to be controlled by a switch; switch HIGH - sequence started; switch LOW - sequence stopped and reset. Resetting is important so that the next time the switch is HIGH the sequence starts from the beginning again.

At the moment, code below, the switch turns the LEDs on/off but does not reset the time so the sequence of LED lighting is not reset. Is there a better way of doing this?

Any advice is much appreciated!

// Step Through 3 LEDs

int led1Pin = 12;                // LED connected to digital pin 12
int led2Pin = 8;                 // LED connected to digital pin 8
int led3Pin = 7;                 // LED connected to digital pin 7
int switchPin = 2;               // Switch connected to digital pin 2
int switchState = 0;
int value = LOW;                 // previous value of the LED
long Time = 0;        



void setup()
{
  pinMode(led1Pin, OUTPUT);      
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(switchPin, INPUT);
}

void loop()
{
  switchState = digitalRead(2);
  Time = millis();
  
  // If switch is high then start LED sequence
  if (switchState == 1) {
  
  if (Time >= 3000) {
    value = HIGH;
    digitalWrite(led1Pin, value);
  }
  
  if (Time >= 6000) {
    value = HIGH;
    digitalWrite(led2Pin, value);  
}

  if (Time >= 10000) {
    value = HIGH;
    digitalWrite(led3Pin, value);  
}

}

  // If switch is low turn all LEDs off
  else {
      value = LOW;
    digitalWrite(led1Pin, value);
    digitalWrite(led2Pin, value);
    digitalWrite(led3Pin, value);
}
}

Just a little tweaked:

[UNTESTED CODE]

// Step Through 3 LEDs

#define led1Pin 12 // LED connected to digital pin 12
#define led2Pin 8 // LED connected to digital pin 8
#define led3Pin 7 // LED connected to digital pin 7
#define switchPin 2 // Switch connected to digital pin 2

#define DELAY1 3000
#define DELAY2 6000
#define DELAY3 10000
#define TIMEOUT 1000

bool switchState = 0;
bool value = LOW; // previous value of the LED
unsigned long time = 0;

void setup()
{
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(switchPin, INPUT);
}

void loop()
{
time = millis();
value = switchState = digitalRead(switchPin);

// If switch is high then start LED sequence
if ( switchState==HIGH ) {
//as long as switch stays HIGH
while ( switchState==HIGH ) {
if ( time + DELAY1 <= millis() ) {
digitalWrite(led1Pin, value);
}
if ( time + DELAY2 <= millis() ) {
digitalWrite(led2Pin, value);
}
if ( time + DELAY3 <= millis() ) {
digitalWrite(led3Pin, value);
}
if( time + DELAY3 + TIMEOUT <= millis() ) {
time = millis();
}
switchState = digitalRead(switchPin);
}
} else {
digitalWrite(led1Pin, value);
digitalWrite(led2Pin, value);
digitalWrite(led3Pin, value);
}
}

Thanks AlphaBeta for the reworked code. I've taken the 'while' loop element and tried it in this format (times between the LEDs are arbitrary at this point). The LED sequence works fine but even when the switch is LOW, it never leaves the while loop!? I'm assuming it should be checking the switchState every time it goes around the loop? I might be completely wrong of course!

// Step Through 3 LEDs

int led1Pin = 12; // LED connected to digital pin 12 int led2Pin = 8; // LED connected to digital pin 8 int led3Pin = 7; // LED connected to digital pin 7 int switchPin = 2; // Switch connected to digital pin 2 int switchState = LOW; int value = LOW; // previous value of the LED long Time = 1;

void setup() { pinMode(led1Pin, OUTPUT); pinMode(led2Pin, OUTPUT); pinMode(led3Pin, OUTPUT); pinMode(switchPin, INPUT); }

void loop() { switchState = digitalRead(2);

// If switch is high then start LED sequence if (switchState == HIGH) { while (switchState == HIGH) { ++Time; value = HIGH; digitalWrite(led1Pin, value);

if (Time > 50000) { value = HIGH; digitalWrite(led2Pin, value); }

if (Time > 100000) { value = HIGH; digitalWrite(led3Pin, value); } } } // If switch is low turn all LEDs off else { value = LOW; digitalWrite(led1Pin, value); digitalWrite(led2Pin, value); digitalWrite(led3Pin, value); Time = 0; } }