Low Tech Home Automation Security Lights Sketch

Hello every one , my name is Paul and this will be the first time that I will post here , although I have spend many hours reading through various posts here and thank you for every one that contributes to all the questions posted here it is always been a good point of reference for me.

My Goal:

I have a Single relay that will turn on security lights out side my house. The lights are on as special 12v / 220v relay so I just need to trigger the Relay to turn on / off the lights (for safety reasons).

There will be three inputs to turn on the lights:

  1. LDR sensor that will be triggered by a old cell phone that we can call (This will only accept two numbers)
  2. A push button switch that will turn the lights on or off depending on its state
  3. A Alarm relay that will turn on the lights when it is triggered and stay on for a set time after the alarm has stopped.

My Method:
I stated by building each trigger separately in a sketch and then added them together in one sketch - now I am not even close to knowing exactly what I am doing but I do understand the general logic of the coding -

I managed to get Triggers 1 and 2 working perfectly and they would behave exactly as I planned but the moment I add the Alarm trigger every thing goes a bit pear shape.
The Alarm trigger wont send the "HIGH" signal to the relay but will turn the LED on for the set time but on the triggers 1 and two the relay will go on but the LED will not stay on with the Relay to indicate that the relay is on.
Now I assume that the various codes are now interfering with each other but I cannot find the it and I hope that it is just a small piece that needs to be changed.

Can any one help?

The Code Below:

// Bits and pieces taken from all over the net to compose this code. I could not take any credit for all it and so shouldn’t any one else. Use it change it delete it as you like!!!!
// This code will run a single relay that will switch on a set of security lights. And will have 3 inputs. 
// 1) LDR that will be triggered by a old Cell phone (Phone will be programed to accept only two numbers)
// 2) Push Button switch located in the house 
// 3) Alarm that will turn on the security lights when it is triggered 


const int LDR = A0;     // The LDR that monitors the cell phone screen
const int Relay = 2;   // The Relay that Controles the Lights
int pinButton = 8;     // The main switch that will be wall mounted (Might be a "bell" Push button)
int AlarmRelay = 9 ;  // The Relay that will be triggered when the alarm goes off
int LED = 13;         // The Led that will show that the relay is on or off
// Values for Alarm trigger
int stateButtonAlarm ;              // to Chek the state of the Alarm Relay
int ledState = LOW;                // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
long OnTime = 100;               // milliseconds of on-time
long OffTime = 10000;           // milliseconds of how long before the light time goes off after 

// VALUES for LDR
int sensorValue = 0;  // variable to store the value coming from the sensor
int previous = LOW;    // the previous reading from the input pin
long time = 0;         // the last time the output pin was toggled

// Values for Push Button
unsigned long LastOnTime = 0;
boolean RelayState = LOW;
int stateRelay = LOW;
int stateButton;
long debounce = 500;
 
void setup() {
  pinMode (AlarmRelay,INPUT);
  pinMode(pinButton, INPUT);
  pinMode(Relay, OUTPUT);
  pinMode (LED, OUTPUT);
  Serial.begin(9600); //sets serial port for communication

}
 
void loop() {

// Cell Phone Switch Code 

   int LDRValue = analogRead(LDR); // read the value from the LDR
   Serial.println(LDRValue); //prints the values coming from the sensor on the screen

// code for checking the LDR and if the phone is on
  // If the screen is ON...
  if(LDRValue > 400) {

   //  ...and it has been Less than 1 seconds since it was last seen on...
    if (millis() - LastOnTime > 1000UL) {
   if(stateRelay == HIGH){
      stateRelay = LOW;
      digitalWrite (LED , LOW);
    } else {
       stateRelay = HIGH;
       digitalWrite (LED , HIGH);
    }
     
    }
   
  LastOnTime = millis();
  }

 // Code for house on / off switch so that if the cell phone switch was on it can be swithed off in house
   
  stateButton = digitalRead(pinButton);  
  if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
    if(stateRelay == HIGH){
      stateRelay = LOW;
      digitalWrite (LED , LOW);
    } else {
       stateRelay = HIGH;
       digitalWrite (LED , HIGH);
    }
    time = millis();
  }
  digitalWrite(Relay, stateRelay);
    delay (300) ;
  previous == stateButton;


// Alarm Code - The Alarm closes a relay as it gets trigger and the siren goes off the relay will be 
//closed and will be as long as the siren is activated. The security lights must stay on for a set amount of time after that siren stopped. 

{
  stateButtonAlarm = digitalRead(AlarmRelay);
   unsigned long currentMillis = millis();
   if((stateButtonAlarm == HIGH) && (currentMillis - previousMillis >= OnTime))
  {
    ledState = HIGH;  // Turn it on
    previousMillis = currentMillis;  // Remember the time
    digitalWrite(LED, ledState);  // Update the actual LED
    digitalWrite (Relay,HIGH);    // Turn on the Security Lights
  }
  else if ((stateButtonAlarm == LOW) && (currentMillis - previousMillis >= OffTime))
  {
    ledState = LOW ;  // turn it on
    previousMillis = currentMillis;   // Remember the time
    digitalWrite(LED, ledState);    // Update the actual LED
    digitalWrite (Relay,LOW);       // Turn off the security lights
  }
}
}[code]

Welcome. Please do us all a favour and replace the [­quote] tags around your code with [­code] tags to make it easier to deal with.

Ah - that is how you do it I have changed accordingly thank you for the tip!

I I had 3 working sketches to combine, I would let each of the sketches set a variable to true, instead of activating the relay. In that way, code from one trigger can't deactivate the relay if another trigger activated it. For example use,
statusFromLDR
statusFromButton
statusFromAlarm

Finally

if (statusFromLDR=true || statusFromButton=true || statusFromAlarm=true) {
   digitalWrite (Relay, HIGH);
   }else{
   digitalWrite (Relay, LOW);
}

Just make sure you dont use same variable name in more than one sketch.

Yes but I would like to allow the different triggers to deactivate the Relay so that If the LDR activated the relay you can deactivate it with the Button. Does "statusFromLDR=true" imply that the LDR is HIGH and the false be LOW?
Sorry this might be a silly question but I am still learning.

Ah I see now that I have mixed variables in and out of the code. I will do a clean up and check in again; Thank you for some feed back.