Garage Door Detector Problem

I have a NO sensor hooked up to my garage door. For some reason the particle.publish won’t send the message of the door being open. I am sure I did something stupid, so if anyone can pick it out I would appreciate it. Here is the code. I have the power hooked up to the 3v output.

int doordetector = A2;
int button = D4;
int doorvalue;
int hour;
int out = A5;
bool dooropened = false;
bool after9 = false;


void setup() {
Time.zone(-4);
pinMode(out, OUTPUT);
pinMode (doordetector, INPUT);
pinMode (button, OUTPUT);
Particle.variable ("GarageDoor", doorvalue);
Particle.variable ("hours" , hour);
Particle.function ("GDopener",GDopener);
}
void loop() {
doorvalue=analogRead(doordetector);
hour=Time.hour();
if (analogRead(doordetector) >=3900 && dooropened != true) {
        dooropened = true ;
        Particle.publish("Garage_Door","Opened" , 60 , PRIVATE);
        delay (1000);
        }
    else if (analogRead(doordetector) <=3900) {
    dooropened = false;
    }
    else {
    dooropened = true;
    }
}  

int GDopener(String command){
    if (command == "close"){
        digitalWrite (button , HIGH);
        delay (3000);
        digitalWrite (button, LOW);
        return 1111;
    }
    else {
    }
}

Normally if you’re reading a switch, you would use digitalRead, not analogRead, although that could work if you have things wired up correctly (a wiring diagram would be helpful). You should put in a Serial.print() statement after the first line in loop() to see what value you’re getting from your analog read. Btw, you set the value of doorvalue, but never use it; you should have that in your if statement instead of analogRead(doordetector) (though that’s not causing your problem).

2 Likes

@jzalar, the issue is most likely due to the floating analog input which can read any random value depending on the electrical charge on the pin when you read it. I would follow @Ric’s advice and treat the pin as a digital input and add an external pull-up or pull-down resistor depending on how your door sensor is wired.

1 Like

I will order some resisters today. I will let you guys know how it works after.

@jzalar, depending on the length of your sensor wires, I would use 2.2K, 4.7K or 10K ohm resistors.

Good News and Bad news. First the good. The resister did the trick and keep the analog reading down close to zero. This also helped with my problem on the home alarm system. I keep getting multiple notifications from IFTTT. Related Post here Particle.Function () losing its value

Now the bad news. For some reason, my bool is not working at all. I am getting notifications from IFTTT every 1 sec. The sensor is hooked up to read high (4095) when the garage door is closed, then 0ish when it is open. After installing the resistor the sensor value went very close to zero so that is working. Something is wrong with my code I think.

Let me know what you think.

int doordetector = A2;
int button = D4;
int doorvalue;
int hour;
int out = A5;
bool dooropened = false;
bool after9 = false;


void setup() {
Time.zone(-4);
pinMode(out, OUTPUT);
pinMode (doordetector, INPUT);
pinMode (button, OUTPUT);
Particle.variable ("GarageDoor", doorvalue);
Particle.variable ("hours" , hour);
Particle.function ("GDopener",GDopener);
}
void loop() {
doorvalue=analogRead(doordetector);
hour=Time.hour();
if (analogRead(doordetector) >=3900 && dooropened != true) {
        dooropened = true ;
        Particle.publish("Garage_Door","Opened" , 60 , PRIVATE);
        delay (1000);
        }
    else if (analogRead(doordetector) <=3900) {
    dooropened = false;
    }
    else {
    dooropened = true;
    }
}  

int GDopener(String command){
    if (command == "close"){
        digitalWrite (button , HIGH);
        delay (3000);
        digitalWrite (button, LOW);
        return 1111;
    }
    else {
    }
}

There’s nothing wrong with your bool, the code works fine for me. If you can, you should put in some print statements (as I said in my last post) to check what values you’re analog read is giving you. Also, check your console to see if you’re getting a publish event happening every second to rule out something weird going on at IFTTT. What kind of sensor are you using? If it’s mechanical, you should be doing some de-bouncing.

Yes, there is an problem in the code - although maybe not the only one :wink:

if the reading should happen to be exactly 3900 (or jitter around that value) dooropened will permanently flip.
Also reading the value twice for the same conditional block can cause unexpected behaviour since the two readings may slightly differ from one check to the other.
You may want to separate the trigger thresholds a bit and remove the final else block to avoid that.

I still can't understand why you use analogRead() and not digitalRead() (as @Ric already pointed out). With that the hardware will take care of the "tolerance" issue.

1 Like

Thanks for the feedback. I am using a simple magnetic door sensor mounted on the garage door and wall. I agree that the multiple analogRead need to be removed. I will also try rewriting this with a digitalRead too. I am traveling this week, so it will have to wait until this weekend.

Here is what I am going to try out when I get home. Unless someone tries to open my garage while I am away.

void loop() {
doorvalue=digitalRead(doordetector);
hour=Time.hour();
if (doorvalue=1 && dooropened != true) {
        dooropened = true ;
        Particle.publish("Garage_Door","Opened" , 60 , PRIVATE);
        delay (1000);
        }
    else if (doorvalue = 0) {
    dooropened = false;
    }
} 

Well. It seems to be working. I might be updating my home alarm code with digitalRead this weekend.

int doordetector = A2;
int button = D4;
int doorvalue;
int hour;
int out = A5;
bool dooropened = false;
bool after9 = false;


void setup() {
Time.zone(-4);
pinMode(out, OUTPUT);
pinMode (doordetector, INPUT);
pinMode (button, OUTPUT);
Particle.variable ("GarageDoor", doorvalue);
Particle.variable ("hours" , hour);
Particle.function ("GDopener",GDopener);
}
void loop() {
doorvalue=digitalRead(doordetector);
hour==Time.hour();
if (doorvalue==1 && dooropened != true) {
        dooropened = true ;
        Particle.publish("Garage_Door","Opened" , 60 , PRIVATE);
        delay (1000);
        }
    else if (doorvalue == 0) {
    dooropened = false;
    }
}  

int GDopener(String command){
    if (command == "close"){
        digitalWrite (button , HIGH);
        delay (3000);
        digitalWrite (button, LOW);
        delay (7000);
        if (digitalRead(doordetector)==0) {
        return 1111;
        }
        else {
        return 999999;
        }
    }
    else {
        return 1919191;
    }
}