Hello DVDdoug, and thanks for your help!
Sorry I wasn't precise enough, yes I tested everything a few line at a time with 3 buttons and 4 LED on my breadboard to simulate the sequence. And I also shortened the sequence to 3 seconds then 1 seconde and 1 second.
Everything was working fine until I realized that the sequence would stop (and the blinking LED representing the horn) if I pressed again the button 1 witch is the door switch.
I was focused on learning about the change state command because I thought it would be the solution and then I saw that the wiring could be wrong but nothing worked.
Then I found the solution, I realized that my sequence is not stopping, it's just resetting.
Every time I push the button (doorSwitch) it sets my alarmOnMillis equal to currentMillis and hence restarts my timer for my delays with millis.
Adding the following if function did the trick by only allowing the alarmOnMillis to be set to currentMillis if the alarm is OFF. (there might be something more elegant though ;))
void loop() {
unsigned long currentMillis = millis();
if ((alarmOn) == false){
if (digitalRead(doorSwitch) == HIGH){ // door switche pressed
alarmOn = true; // alarm is ON
alarmOnMillis = currentMillis; // start timing for futur events
}
So the whole corrected code for now is:
//Global Variables
const int doorSwitch = 2; // door switch linked to input 2
const int button1 = 3; // button 1 linked to input 3
const int button2 = 4; // button 2 linked to input 4
const int LED = 13; // to ON LED
const int hazard = 12; // to hazard lights
const int horn = 11; // to car horn
const int relay = 10; // to latchning relay (cuts off fuel pump)
int hornState = LOW;
unsigned long alarmOnMillis; // when alarm was activated
unsigned long hazardTurnOnDelay = 3000; // wait to turn on hazard after alarm activated
unsigned long hornTurnOnDelay = 4000; // wait to turn on horn after alarm activated
unsigned long relayTurnOnDelay = 5000; // wait to activat relay after alarm activated
unsigned long previousMillis = 0;
long interval = 500; // horn ON and OFF interval
bool hazardOn = false; //
bool hornOn = false; //
bool relayOn = false; //
bool alarmOn = false; //
void setup() {
pinMode(doorSwitch, INPUT);
pinMode(hazard, OUTPUT);
pinMode(LED,OUTPUT);
pinMode(horn,OUTPUT);
pinMode(relay,OUTPUT);
pinMode(button1,INPUT);
pinMode(button2, INPUT);
}
void loop() {
unsigned long currentMillis = millis();
if ((alarmOn) == false){
if (digitalRead(doorSwitch) == HIGH){ // door switche pressed
alarmOn = true; // alarm is ON
alarmOnMillis = currentMillis; // start timing for futur events
}
}
if (alarmOn) {
digitalWrite(LED,HIGH); // alarm status LED turns ON
hazardOn = true; // hazard code activated
hornOn = true; // horn code activated
relayOn = true; // relay code activated
}
else{ // if alarm is deactivated by button 2 (see above)
(digitalWrite(LED,LOW)); // shuts done everything
hazardOn = false;
(digitalWrite(hazard,LOW));
hornOn = false;
(digitalWrite(horn,LOW));
relayOn = false;
(digitalWrite(relay,LOW));
}
if (hazardOn) {
if ((unsigned long)(currentMillis - alarmOnMillis) >= hazardTurnOnDelay) {
// check if hazard delay has passed
digitalWrite(hazard, HIGH); // when yes, starts hazard
}
}
if (hornOn) {
if ((unsigned long)(currentMillis - alarmOnMillis) >= hornTurnOnDelay) {
// check if horn delay has passed
if (currentMillis - previousMillis >= interval) {
// if yes, starts horn ON and OFF depending on interval
previousMillis = currentMillis;
// if horn is off turn it on and vice-versa:
if (hornState == LOW) {
hornState = HIGH;
} else {
hornState = LOW;
}
// set the horn with the hornState of the variable:
digitalWrite(horn, hornState);
}
}
}
if (relayOn) {
if ((unsigned long)(currentMillis - alarmOnMillis) >= relayTurnOnDelay) {
// check if relay delay has passed
digitalWrite(relay, HIGH); // starts relay
}
}
if (digitalRead(button1) == HIGH) { // check if button 1 is pressed
alarmOn = false; // turn off alarm
}
}