Hello guys, I'm pretty new to Arduino and have only completed the projects within my book and also a few on my own.
I've been searching a solution for my issue but couldn't find someone having the same problem, even though I'm pretty sure it's something really simple based on a function I must be using incorrectly.
I'm trying to create a car alarm system that if you open a door will flash the hazard lights after 4 minutes then make the horn go off after 5 minutes and then cut the fuel pomp (with a latching relay) after 6 minutes unless you press a hidden button.
After looking for similar needs I could come to the following code which is almost OK but I realized that if you push the button simulating the door switch at any moment, the sequence will stop.
If someone could tell me what I did wrong that would be very kind ![]()
Pierre
//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 (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
}
}