Hello Guys
I am building a Security light relay board that will will be triggered by various inputs. So far I have been able to get everything working well but now I would like to add "timed" event.
I have a LDR that triggers the relay when a certain threshold is crossed but now I would like the trigger only after the threshold was crossed for a set time.
Some thing like this:
if (LDRValue > 80) // and stays at that value for 3 seconds - how will I do this?
if(stateRelay == HIGH){
stateRelay = LOW;
digitalWrite (LED , LOW);
digitalWrite (CheckPin , HIGH);
} else {
stateRelay = HIGH;
digitalWrite (LED , HIGH);
}
I have read many posts where button press is used with millis() to time between events but I can figure out how to check for a time elapsed before event.
Can someone maybe help?
Below is the current working code is full if some one would like to use it.
//Constant Values
const int LDR = A0;
const int Relay = 10; // 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 trigger is recieved ( Else will need to go outside )
int CheckPin = 11; // This pin is Used to ensure that Timer is only used when the Alarm Relay is activated first
//
//Varialbles
int LDRstate = LOW;
int ALARMstate = LOW ;
int previous2 = LOW;
int stateCheckPin = LOW;
// 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 = 5000; // milliseconds of how long before the light time goes off after Alarm has stopped
// 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 = 2000;
void setup() {
pinMode (AlarmRelay,INPUT);
pinMode(pinButton, INPUT);
pinMode(Relay, OUTPUT);
pinMode (LED, OUTPUT);
pinMode (CheckPin,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 > 80) {
// ...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);
digitalWrite (CheckPin , HIGH);
} else {
stateRelay = HIGH;
digitalWrite (LED , HIGH);
digitalWrite (CheckPin , HIGH);
}
}
LastOnTime = millis();
}
// Code for house on off switch so that if the cell phone switch was on it can be swithed off inhouse
stateButton = digitalRead(pinButton);
if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
if(stateRelay == HIGH){
stateRelay = LOW;
digitalWrite (CheckPin , HIGH);
} else {
stateRelay = HIGH;
digitalWrite (CheckPin , HIGH);
}
time = millis();
}
digitalWrite(Relay, stateRelay);
digitalWrite (LED , stateRelay);
// digitalWrite (CheckPin , stateRelay);
delay (200) ;
previous == stateButton;
{
// Alarm Code
}
stateButtonAlarm = digitalRead(AlarmRelay);
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);
}
}