Hi!
My name is Pierre, I’m from France, I like to build things myself and work on cars or RC things (planes, boats, cars…)
I’m fairly new to Arduino which I’m discovering with the projects from the book it came with and this will be my first real project for an everyday use.
I’m starting this topic to show what I’m doing and maybe help other people with a similar project and also to benefit from constructive criticism from people on the forum.
The Project :
Since I couldn’t find on the Internet a device simple and matching my needs for an affordable price I decided to build my own alarm system for my classic car (1994 Rover Mini Cooper).
What I want is that if someone breaks in the car and hotwire it (I thinks that’s how you guys say it ;)), the Arduino will start the hazard lights after something like 5 minutes, then after another minute turn the horn ON and OFF and at last after another minute shut the engine off (by cutting either the fuel pump or the ignition).
Ideally I would like the system to be reset by a combination of two buttons.
The Arduino will be powered by a step down 5 or 3 volt supply and the signal to set the alarm will come from the door switch (for the interior lights) and the 12V ignition (I don’t want the alarm to start if I forget my phone into the car or my wife her purse).
The alarm would also be useful in case of a carjacking because if someone opens the door and the ignition is ON, the alarm will start.
I played a little with the code and built a small scale circuit with 4LEDs and 4 switches and it’s working well for now.
//Global Variables
const int doorSwitch = 2; // our door signal
const int ignitionSwitch = 3; // our ignition signal
const int button1 = 4; // our button 1
const int button2 = 5; // our button 2
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
unsigned long hornTurnOnDelay = 4000; // wait to turn on horn
unsigned long relayTurnOnDelay = 5000; // wait to activat relay
unsigned long previousMillis = 0;
long interval = 500; // horn ON and OFF interval
bool hazardOn = false; //
bool hornOn = false; //
bool relayOn = false; //
bool alarmOn = false; // for when alarm is ON
bool ignitionOn = false;
bool doorOn = false;
void setup() {
pinMode(doorSwitch, INPUT_PULLUP);
pinMode(hazard, OUTPUT);
pinMode(LED,OUTPUT);
pinMode(horn,OUTPUT);
pinMode(relay,OUTPUT);
pinMode(button1,INPUT);
pinMode(ignitionSwitch, INPUT);
}
void loop() {
// get the time at the start of this loop()
if (digitalRead(ignitionSwitch) == HIGH){
ignitionOn = true;
}
if (digitalRead(doorSwitch) == HIGH){
doorOn = true;
}
unsigned long currentMillis = millis();
if ((alarmOn) == false){
if ((doorOn) && (ignitionOn)){
alarmOn = true;
alarmOnMillis = currentMillis;
}
}
// check the button
if (alarmOn) {
// update the time when button was pushed
digitalWrite(LED,HIGH);
hazardOn = true;
hornOn = true;
relayOn = true;
}
else{
(digitalWrite(LED,LOW));
hazardOn = false;
(digitalWrite(hazard,LOW));
hornOn = false;
(digitalWrite(horn,LOW));
relayOn = false;
(digitalWrite(relay,LOW));
}
// make sure this code isn't checked until after button has been let go
if (hazardOn) {
if ((unsigned long)(currentMillis - alarmOnMillis) >= hazardTurnOnDelay) {
digitalWrite(hazard, HIGH);
}
}
if (hornOn) {
if ((unsigned long)(currentMillis - alarmOnMillis) >= hornTurnOnDelay) {
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the Klaxon 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) {
digitalWrite(relay, HIGH);
}
}
if (digitalRead(button1) && (digitalRead(button2)== HIGH)) {
alarmOn = false;
ignitionOn = false;
doorOn = false;
}
}
Video test (don't know how to put a video in the text)
If you push the first button (door signal) and then the second (ignition signal) or the other way around, the green light lights (alarm ON light that will be on the dashboard), then 4 seconds later the Yellow LED starts (representing hazard lights) a second later the blue LED starts blinking (representing the horn) and at last the red LED starts (representing the latching relay that will cut the engine off).
For now I have to push simultaneously on button 3 and 4 to disable the alarm but I want to figure out a way to use a combination (like 1-1-2-2-1-2) with the two buttons to turn the alarm OFF.
I would like to use a smaller board than the Uno to save space and draw less power and I will have to add 3 or 4 relays for the outputs and 4 relays for the input to send only 3V into the inputs.
Depending on how much time I have to work on this project, I will try to update it with my problems / solutions.