Problem in Resetting millis() back to 0

Hi! Beginner here so pls bear with me. So Im having a hardtime adding a code that puts the states(i.e., Case 2 , Case 3 and Case 4) back to accessory mode(i.e., Case 1) when the A3 button is pushed. I tried adding an if statement like the one below but it seems like the delay line is preventing it from happening since it goes directly to case 3. So basically it has to go to case 1 if the user has clicked the A3 button within 5 seconds, else it goes to state 3. I tried millis() as a workaround but I'm not exactly sure how to reset it back to zero so that: if (millis()<=5) it goes to case 1, else it goes to case 3.

case 1:
...
case 2:
//if delay timer(i.e., 5s) is done, transition the state
delay(5000);
state=3;

// if seatbelt button is pressed, it goes back to case 1 or accessory mode
if (buttonA3 == LOW) {
state = 1;
delay(500);
}
break;
case 3:

The flowchart of my algorithm and the circuit is attached below. The arrows in color red is what Im trying to fix.
The complete codes is is here:

int state;
int buttonA3;
int buttonA4;
int buttonA5;

// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(9600);
// initialize digital pin LED_BUILTIN as an output.
for (int ledPin = 2; ledPin <= 13; ledPin++)
pinMode(ledPin, OUTPUT);
for (int ledPin = 2; ledPin <= 13; ledPin++)
digitalWrite(ledPin, LOW);

pinMode (A3, INPUT_PULLUP);
pinMode (A4, INPUT_PULLUP);
pinMode (A5, INPUT_PULLUP);

// initialize our state
state = 0;
}

// the loop function runs over and over again forever
void loop() {
buttonA3 = digitalRead(A3);
buttonA4 = digitalRead(A4);
buttonA5 = digitalRead(A5);
unsigned long currentMillis = millis();

switch (state) {
case 0: //OFF Mode; nothing happens here basically
for (int ledPin = 2; ledPin <= 13; ledPin++)
digitalWrite(ledPin, LOW);

//If the driver turns ON the key, the next state is in Accessory Mode
if (buttonA5 == LOW) {
state = 1;
delay(100);
}

break;

case 1: //Acessory Mode; nothing happens here basically
for (int ledPin = 2; ledPin <= 13; ledPin++)
digitalWrite(ledPin, LOW);

//If the Ignition is turned ON, the next state is in Ignition Mode
if (buttonA4 == LOW) {
state = 2;
delay(100);
}

break;

case 2: //Ignition mode

//if delay timer(i.e., 5s) is done, transition the state
delay(5000);
state=3;

// if seatbelt button is pressed, it goes back to acessory mode
if (buttonA3 == LOW) {
state = 1;
delay(500);
}
break;

case 3:
digitalWrite (2, HIGH);

////if delay timer(i.e., 10s) is done, transition the state
delay(10000);
state=4;
// ON; if seatbelt button is pressed, it goes back to acessory mode
if (buttonA3 == LOW) {
state = 1;
delay(500);
}

break;

case 4:
for (int ledPin = 2; ledPin <= 13; ledPin++){
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}

// ON; if seatbelt button is pressed, it goes back to acessory mode
if (buttonA3 == LOW) {
state = 1;
delay(500);
}
// if ignition button is turned off, it goes back to acessory mode
else if (buttonA4 == LOW) {
state = 1;
delay(500);
}
// if key is turned off, it goes back to acessory mode
else if (buttonA5 == LOW) {
state = 1;
delay(500);
}

break;

default:
break;
}
Serial.print("state = ");
Serial.print(state);
Serial.print(" currentMillis = ");
Serial.println(currentMillis);

delay(100);

}

MyCkt.JPG

MyCkt.JPG

Read the sticky at the top of the forum "Read this before posting a programming question".

If you post your code as described in the how to use this forum sticky, more forum members will read it.

I don't understand your question at all. You are not using millis() in your loop except to print the currentMillis at the end of the loop. What's that for??

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

Please edit your post to add code tags.

There is no reason to set millis() to zero. To learn how to avoid using delay(), study this excellent tutorial.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.