Need Help Writing Code for my alarm project

Hi All,
I am wanting to make an alarm for my shed.
I want it to consist of 16,2 LCD, Arduino UNO, Siren, Buzzer, 3 LED’s, A switch to Arm/Disarm (Later to be replaced with an RFID Reader, Magnetic Reed Switch on door etc.

I previously started a thread asking for help on the schematic and code but I have been working hard today working on the circuit and I think I have came up with something that will suit my needs.

I really am struggling to get my head around the coding especially loops. I just can’t seem to come with a code at all.

Basically when unit is first powered up, I want the LCD screen to say something on the top line and say “Alarm Disabled” on the bottom line. I have managed that… Just!

When the button is pressed I want a delay of 15 seconds and a beep from the buzzer every second to give me time to exit the shed. If the door isn’t closed after 15 seconds, I want the alarm to sound.

If the door is shut, I want the alarm to be armed. Once the door is opened, I want the same 15 seconds delay to press the button again to disarm the alarm.

Green LED = Alarm Disarmed
Yellow LED = Countdown
Red LED = Alarm Armed.

I have the LCD Connected to the analog pins to free up some digital pins for the future when I plan to add RFID.

I have started writing the code but I have no idea what I am doing. I am new to all this lol.

// include the library code:
#include <LiquidCrystal.h>


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
const int ledGreen = 2;
const int ledYellow = 3;
const int ledRed = 4;
const int setAlarm = 8;
const int buzz = 9;
int buttonState = 0;
const int reed = 5;
const int Siren = 6;

void setup() {
  
  // set up the LCD's number of rows and columns: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  pinMode(ledGreen, OUTPUT); //Set Green LED to Output
  pinMode(ledYellow, OUTPUT); //Set Green LED to Output
  pinMode(ledRed, OUTPUT); //Set Green LED to Output
  pinMode(setAlarm, INPUT); //Set Alarm Button as INPUT
  pinMode(buzz, OUTPUT); //Set buzzer as output
  pinMode(reed, INPUT);
  pinMode(Siren, OUTPUT);
  lcd.print("Security System!");
  digitalWrite(ledGreen, HIGH);
  digitalWrite(buzz, HIGH);
  delay(50);
  digitalWrite(buzz, LOW);
  delay(50);
  digitalWrite(buzz, HIGH);
  delay(50);
  digitalWrite(buzz, LOW);
  lcd.setCursor(0,1);
  lcd.print(" Alarm Disarmed");
}

void trip_reed(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("   BREACH DOOR");
  digitalWrite(Siren, HIGH);
}

void exit_time(){
  lcd.clear();
  lcd.print("Security System!");
  lcd.setCursor(0,1);
  lcd.print(" Setting Alarm!");
  digitalWrite(buzz, HIGH);
  delay(500);
  digitalWrite(buzz, LOW);
  delay(500);
   digitalWrite(buzz, HIGH);
  delay(500);
  digitalWrite(buzz, LOW);
  delay(500);
   digitalWrite(buzz, HIGH);
  delay(500);
  digitalWrite(buzz, LOW);
  delay(500);
   digitalWrite(buzz, HIGH);
  delay(500);
  digitalWrite(buzz, LOW);
  delay(500);
   digitalWrite(buzz, HIGH);
  delay(500);
  digitalWrite(buzz, LOW);
  delay(500);
  lcd.clear();
  lcd.print("Security System!");
  lcd.setCursor(0,1);
  lcd.print("   Alarm Set!");
  return;
}


void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(setAlarm);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    exit_time();  
  } 
  else {
    

  }
  int reed = digitalRead(5);
  if ( reed == LOW ){
    trip_reed();
  }
  
  else {}
  
}

Any advice at all would be really appreciated.

Many thanks
Rob

You aren't going to be popular with your neighbours.
You set Siren high (the siren presumably sounds) but nowhere do you set it low.
How are you going to stop it?

Thanks for your reply.
I know I need to set it to low at some point. I just want to get the basic code working for arming/disarming the alarm and also the entry/exit delays

Then I can think about alarm duration's etc.

Once I get help assembling the main code, I can adapt it to my own needs etc.

Regards,
Rob

Right I have stopped being so lazy and started to write the code myself with great success.

Just one question. How can I make the beeper bleep every second for 15 seconds (entry delay)?

I need it to beep 15 seconds then the alarm goes off or beep until the alarm is disabled.

Regards,
Rob

Look at the 'Blink without delay' example either in this website (examples page) or in the IDE.
Also see http://forum.arduino.cc/index.php?topic=223286.0 for further guidance.

Thanks for your reply there.
I was intending to use the "blink without delay" but im not sure how to make it stop after 15 seconds.

Regards
Rob

robbstech:
Thanks for your reply there.
I was intending to use the “blink without delay” but im not sure how to make it stop after 15 seconds.

First of all, I suggest you use a loops for your on / off siren, which yes, will may you really popular with the neighbours. Try something like

for (int i=o; i<10; i++) {
digitalWrite(buzz, HIGH);
delay(500);
digitalWrite(buzz, LOW);
delay(500);
}

Using some maths, with this basic method you can work out times like you 15 seconds. Good to also google some info on the use of millis for timing.

robbstech:
Thanks for your reply there.
I was intending to use the "blink without delay" but im not sure how to make it stop after 15 seconds.

Pseudo code:
if (millis() - millisStart =>15000){
STOP!
}

blink without delay is a hard thing to grasp as it seems you have to write code for the thing you care least about.

first, you have some switch point, say the turn of the key
so you grab that instant.

if (key=high){
keytime-millis();

but, that will just keep setting keytime to 'now' so it cannot work.
you have to look to see if it was set before.

if (key=high){
if (oldkey not equal high){ // means this is the first time it was high
keytime-millis(); // only on the first time, do you set keytime
}
}
oldkey=key // this goes by itself, not as part of the if statements

at this point, you did two things, saw the key changed, and set oldkey to show from now on, that the key had been set at some time prior to now.

the other thing you did was lock in the time, the first time, the key was set.

now you have a point in time to reference.

if(millis()-keytime)>15000){ // here you check 'now' minus ' then' to get duration
do something.
}

in your case, you would want to check to see if the door closed switch was closed. if it is not closed, then sound the horn.

if(millis()-keytime)>15000){ // here you check 'now' minus ' then' to get duration
sound horn
}

of course, this way, the horn will always sound after 15 minutes.
so, you only want to get to this point if the door is open.

if (dooropen=LOW){ // only shows HIGH when the door is closed. alarms on LOW
if(millis()-keytime)>15000){ // here you check 'now' minus ' then' to get duration
sound horn
}
else // this would mean the door is closed.
{
blink light; // some idle thing if the door is closed.
}

once you can wrap your head around the fact that blink without delay has to juggle 3 things,

if the condition was the same as before
if the duration has elapsed
and set the last condition

then, it will start to make sense.