I thought this could be use full for some else as well.
"Disclaimer - This relates to AC current run trough a relay with DC volts so if you didn't understand what this is do not continue!! - I do not take any responsibility for ANY damages or loss of life, fingers , wife and or pets included " (Just to cover my self )
I have some security lights around the house and a fairly high quality perimeter alarm system and to top it off a DVR jut to check in if the Alarm did go off. The power supply for the cameras takes a fair amount of juice to run and we only use the cameras to view remotely , also I dont care for recording as it is useless most of the time , in my case any way.
The idea was to create a remote switch that I could switch the cameras on so that I could view what is going on if the need arises. Now I know Vellman has a kit that does something similar but the cost on the kit is quite high and you dont have much flexibility on it either. So this is what I came up with:
A Arduino powered Relay controller with a LDR so that you can control the relay with one of those old cell phones that is somewhere in a drawer. Al that the cell phone must be able to do is
Have a screen that lights up when you call it
Have n call blocking feature that prevent some telemarketing guy switching on your camera / lights
The kit costs is around :
1 x Arduino Compatible board ( They are quite cheap) R 190 on BoB
1 x Relay Board R 25 for a Single relay This is what I needed.
1 x LDR any size will do as you will calibrate later.
1 x Strip board to set everything up ( Maybe some one could get us some pc boards made up )
Some wires
Now seeing that most of us here is DIY people I am not going to waste time on writing a full tutorial on how to build a PC board , setup Arduino and how this work , there is a massive amount of information available on the net on this topic and as per usual - Google is your friend.
What I needed was
Be able to turn on the security lights and Cameras at the same time if I wanted to look at the cameras or if we arrive late at home and need to get the lights on (You could use two relays but this will push your costs up as you would need most likely a GMS shield for this. I used a day night switch to prevent the lights to come on in day time.)
Be able to turn the lights on or off in the house , else you will need to phone the relay
If the alarm goes off the lights must come one and then stay on for a x amount of time after the Alarm is reset
This basically works on the principle of the LDR detecting the phone screen light that come's on (Hence any old phone will do) and then trigger the relay, I build in a delay in the code so that if the phone screen does come on when blocking a call it will only be on for a short while , in my case the screen is on for only 2 seconds if it is a blocked number. When I call it I need it to ring for two times and the relay will trigger , you will need to play with the numbers to fit you phone ... Then I have a "Door bell" button in the house that you can push to turn the lights on or off. My Alarm also has a "gate" that closes when it get triggered so that is connected as well but you don not need to use it.You can only use the phone if you need just that.
Now for the good stuff:
First off , I am by no means a prolific programmer and most of what you see here is a general mashup of bits of code that I got over then net if you can improve on it PLEASE do so as I would also like to learn a bit more on it. This took me almost 4 weeks to figure out (part of it was not knowing what I was doing) so please don't go and try making money of this just share it along ...... (Thought it might be necessary to say that)
// Pins that will be used
int LDR = A0; // select the input pin for ldr
const int LED = 13; // just a status LED to show that the relay is on or off ( You can go and check out side as well
const int CheckPin = 11; // This pin is Used to ensure that Timer is only used when the Alarm Relay is activated first
const int Relay = 10; // The Relay that Controls the Lights
const int WallSwitch = 8; // The main switch that will be wall mounted (Might be a "bell" Push button (*PinButton)
const int AlarmTrigger = 9 ; // The Relay that will be triggered when the alarm goes off (* Alarm Relay)
//Variables
int LDRValue = 0; // variable to store the value coming from the sensor
int prev_LDRValue = 0; // variable to store the previous state of the sensor
unsigned long LastOnTimeLDR = 0;
int stateRelay = LOW;
long startOnLDR = 0; // The Last time that input was recieved
// Variables for the WallSwitch
//int state = LOW; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
// Variables that will be used for the 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 = 5000; //## milliseconds of how long before the light time goes off ## EDIT CODE TO DECIDE HOW LONG THE LIGHTS SHOULD BE ON##
int stateCheckPin = LOW;
void setup() {
// Asighn the Pins to what they need to do
pinMode (LED, OUTPUT);
pinMode (CheckPin,OUTPUT);
pinMode (Relay, OUTPUT);
// Triggers that will give inputs
pinMode (AlarmTrigger,INPUT);
pinMode (WallSwitch, INPUT);
Serial.begin(9600); //sets serial port for communication - you will need this to see what is you LDR's output to calibrate acordingly
}
void loop() {
// LDR Code to Switch the Relay on or off by calling the Cell phone for X amount of time
LDRValue = analogRead(LDR); // read the value from the sensor
Serial.println(LDRValue); //prints the values coming from the sensor on the screen
if (prev_LDRValue<=35 && LDRValue>35){ //first if statement check for LDR has gone from below 35 to above 35, and set startOnLDR to millis.
startOnLDR=millis();// Start the Timer
prev_LDRValue=LDRValue;
}
if (LDRValue<35) {
startOnLDR=0;
prev_LDRValue=LDRValue;}
{
if (startOnLDR!=0 and (millis()-startOnLDR) >=2000) { // ## EDIT THIS VALUE TO THE TIME THAT THE PHONE WILL BE ON##
if (millis() - LastOnTimeLDR > 1000UL) { // To check if the relay was triggered before and to keep the relay on after the event.
if(stateRelay == HIGH){
stateRelay = LOW;
} else {
stateRelay = HIGH;
}
}
digitalWrite (LED , stateRelay) ;
digitalWrite (CheckPin , HIGH) ; // This pin need to be set high to prevent other timed code to trigger the relay
digitalWrite (Relay , stateRelay) ;
LastOnTimeLDR = millis();
}
delay(100);
}
// The code to Switch the Relay on or Off via the Wall switch
reading = digitalRead(WallSwitch);
// if the input just went from LOW and HIGH and we've waited long enough to ignore any noise on the circuit, toggle the output pin and remember the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (stateRelay == HIGH)
stateRelay = LOW;
else
stateRelay = HIGH;
digitalWrite (CheckPin , HIGH ) ; // This pin need to be set high to prevent other timed code to trigger the relay
time = millis();
}
digitalWrite (LED, stateRelay );
digitalWrite (Relay , stateRelay ) ;
previous = reading;
// The Code to Switch the Relay on if triggered by the Alarm and the lights will be on for as long as the Alarm is sounding and there after still 10 Min
stateButtonAlarm = digitalRead(AlarmTrigger);
stateCheckPin = digitalRead(CheckPin);
unsigned long currentMillis = millis();
if((stateButtonAlarm == HIGH ) && (currentMillis - previousMillis >= OnTime))
{
stateRelay = HIGH; // Turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite (CheckPin , LOW); // Chage to low so that timer can be used to switch the Light off
digitalWrite(LED, stateRelay); // Update the actual LED
digitalWrite (Relay,stateRelay);
}
else if ((stateButtonAlarm == LOW && stateCheckPin == LOW ) && (currentMillis - previousMillis >= OffTime)) // Only activate it Checkpin is Low
{
stateRelay = LOW; // turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(LED, stateRelay); // Update the actual LED
digitalWrite (Relay,stateRelay);
}
}