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);
}


