I have a simple alarm.
uses motion sensors. the output 12VDC when in normal, and then open the circuit (0VDC) when in alarm.
I have a latching relay. one pulse to set the relay into alarm.
a second pulse to take it out of alarm
for testing, I was looking to just have the second pulse wait about 5 seconds after the sensors go high.
it seems I am continuously pulsing the relay
int led = 13;
const int buttonPin = 2;
int buttonState = 0;
int lastButtonState = LOW;
int ledState = HIGH;
int interval = 5000;
unsigned long then = 0;
int lastState = HIGH;
int relay1 = 8;
int relay2 = 9;
void setup() {
pinMode(led, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
if (buttonState == LOW){ // natural state is high for normal - low is alarm
if (lastState == HIGH){
digitalWrite (relay1,HIGH);
delay (200); // this is to pulse the relay coil one half second.
digitalWrite (relay1, LOW);
}
}
if (buttonState == HIGH){
if (lastState == LOW){
then = millis(); //stores the time for the interval
}
}
if (millis()-then > interval ){ // I think this is where I am going wrong
digitalWrite (relay2, HIGH);
delay(200); // this is to pulse the relay coil one half second.
digitalWrite (relay2,LOW);
}
lastState=buttonState;
} // end of loop