I have a key switch for a simple alarm. what I want to do is that when I turn it, lock in the time.
then if the time is between 1 and 2 seconds, set the alarm. (away mode)
if the time is over 3 seconds, re-set (stay mode)
the relays are latching relays, so only take a pulse to change and hold the state.
I need to look at the condition after the keyswitch is released and only then change the state of the relays.
what I want is to eliminate the pulse from being sent to set one relay while waiting to reach the 3 second timer.
I have not fleshed out the whole program, just looking to see if this is typically how to look at a switch after it is released.
I will add an LED to show the person turning the key that the time is between 1 and 2 seconds and the another after the time is over 3 seconds.
one thing that concerns me is what if someone turns the key just after the first if() statement.
guess I am looking to see if I am on the right track.
void loop(){
int sw = digitalRead(2); // read switch on pin 2
if (sw==HIGH){
if (lastSW==LOW){
swthen=millis();
}
}
swTime=(millis()-swThen);
if(sw==HIGH){
if(swTime>3000){
resetVal=HIGH;
setVal=LOW;
}
if(swTime>1000) && (swTime<2000){
resetVal=LOW;
setVal=HIGH;
}
else{
resetVal=LOW;
setVal=LOW;
}
}
if(sw==LOW){
if (lastSW==HIGH){
digitalWrite(relay1, resetVal);
digitalWrite(relay2, setVal);
}
}
// stuff at end of void loop
lastSW=sw;
} // end of void loop