I been working a garage door alarm and I am at the point where I have everything working pretty good. I have loop at will send an IFTTT notification every 15 min. after 9 PM if my garage door is open. I am using a delay which I believe is not the preferred method. Is there a reason why it is not and does anyone have a better way to do this. I am also concerned my other loops sometimes get hung up while it is in this delay. Is that true? The part of the code is here. Thanks in advanced.
if ((doorvalue ==1 && hour >= 21 ) || (doorvalue ==1 && hour <=3 )) {
Particle.publish ("AFTER9PM","After_9PM", 60, PRIVATE);
delay (900000);
}
else {
}
}
The whole code is below.
int doordetector = A2;
int button = D4;
int doorvalue;
int hour;
int out = A5;
bool dooropened = false;
bool after9 = false;
bool AFH = false;
int awayfromhome;
void setup() {
Time.zone(-4);
pinMode(out, OUTPUT);
pinMode (doordetector, INPUT);
pinMode (button, OUTPUT);
Particle.variable ("GDoor-1open", doorvalue);
Particle.variable ("away__",awayfromhome);
Particle.subscribe("away", myHandler1,MY_DEVICES);
Particle.subscribe("home", myHandler2,MY_DEVICES);
Particle.function ("CLOSEopener",CLOSEopener);
Particle.function ("OPENGarage", OPENGarage);
}
void loop() {
doorvalue=digitalRead(doordetector);
hour=Time.hour();
if (doorvalue==1 && AFH != true && awayfromhome==1 ) {
AFH = true ;
Particle.publish ("AWAYFROMHOME","GDOOR_OPEN" , 60 , PRIVATE);
delay (1000);
}
else if (awayfromhome==0 ) {
AFH = false;
}
if (doorvalue==1 && dooropened != true) {
dooropened = true ;
Particle.publish("Garage_Door","Opened" , 60 , PRIVATE);
delay (1000);
}
else if (doorvalue == 0) {
dooropened = false;
}
if ((doorvalue ==1 && hour >= 21 ) || (doorvalue ==1 && hour <=3 )) {
Particle.publish ("AFTER9PM","After_9PM", 60, PRIVATE);
delay (900000);
}
else {
}
}
int CLOSEopener(String command){
if (command == "close" && (digitalRead(doordetector)==HIGH) ){
digitalWrite (button , HIGH);
delay (500);
digitalWrite (button, LOW);
delay (16000);
if (digitalRead(doordetector)==LOW) {
return 1111;
Particle.publish ("GdoorCommand","Close-Success", 60, PRIVATE);
}
else {
return 999999;
Particle.publish ("GdoorCommand","Close-Fail", 60, PRIVATE);
}
}
else {
return 1919191;
}
}
int OPENGarage(String command){
if (command == "open" && (digitalRead(doordetector)==LOW)){
digitalWrite (button , HIGH);
delay (500);
digitalWrite (button, LOW);
delay (16000);
if (digitalRead(doordetector)==HIGH) {
return 1111;
Particle.publish ("GdoorCommand","Open-Success", 60, PRIVATE);
}
else {
return 999999;
Particle.publish ("GdoorCommand","Open-Fail", 60, PRIVATE);
}
}
else {
return 1919191;
}
}
void myHandler1(const char *event, const char *data) {
awayfromhome = 1;
}
void myHandler2(const char *event, const char *data) {
awayfromhome = 0;
}