Help understanding delay values and publish

I am trying to have my garage door SMS me when the door has been open for ten minutes.
I took the code from here.

I changed his code so when the door is closed the reed switch will read LOW and will read High when open

The problem is that that the time intervals between texts are as follows; 15,17,6,44,21,12,3,15,8,23,29,15(this last one I got two texts at the same time) minutes between texts. Once Particle publishes “garageStatus” IFTTT texts me

The code I’m using is this

// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>

char auth[] = "30c7d03a598a436c837f5ed187f9eb73";
int reedSwitch = D4; //input for the reed switch - detects open/closed status
bool current = 0; //logical placeholder - current status of the garage
bool previous = 1; //logical placeholder - previous status of the garage 
int seconds = 0;
int openlimit = 600; //number of seconds before text sent indicating garage open

void setup()
  Serial.begin(9600); //begin serial communication
  Blynk.begin(auth); //begin Blynk app communication
  pinMode(D0, OUTPUT); //initialize D0 as output pin - relay coil control
  pinMode(D4,INPUT); //initialize reedswitch pin as input

void loop()
{; //run Blynk app
    current = digitalRead(reedSwitch); //set current status open/closed
    if (current != previous){ //only run if there is a status change from previous state
        previous = current;   //reinitialize
        if (digitalRead(reedSwitch) == LOW) { //is signal HIGH? i.e. garage is closed
            Particle.publish("garageStatus","closed",60,PRIVATE); //this line commented out as closed status not of interest
        else {
        // Otherwise, this isn't a new status, and we don't have to do anything.
        if (digitalRead(reedSwitch) == HIGH) { //is signal LOW? i.e. garage is open
            while (seconds < openlimit && digitalRead(reedSwitch) == HIGH){ //while loop to publish only if garage is open more than ten minutes
                if (seconds ==599){
                    Particle.publish("garageStatus","open",60,PRIVATE); //send to spark publish which then triggers text via IFTTT
                    seconds = 0; 
        else {
          // Otherwise, this isn't a new status, and we don't have to do anything.

Aside from the timing issues my other questions are,
if in IFTTT if I have not just “garageStatus” but “garageStatus open” or “garageStatus” “open” that I can differentiate between open and closed so I can get notifided once it closes.

Last question is about adding another garage door. I think I should duplicate the same code add another pin for input and say “garagetwo” for publishing. Does that sound correct?

Thank you,

Have you got a pull resistor attached to provide a proper level when the switch is open?

I guess you have a floating pin issue.

I’d never duplicate code but rather put the shared code into a dedicated function and provide a parameter to distinguish between targets (= which door) and substitute the respective specifics (e.g. pins, messages, …).

1 Like

The reed switch is connected to 3v3 and D4. I am using a jumper wire for now to simulate the reed switch.

So when I have the jumper connected to 3v3 and D4 simulating that the door is open (HIGH) I don’t see how a resister will come into play.

Unless it would be placed somewhere other that GND and D4. ( Pull down)

I get texts when 3v3 is present at D4 just not at ten minute intervals.


Also as far as the pull resister goes can’t I use the INPUT_PULLUP / INPUT_PULLDOWN
Instead of a pull down to ground would it be a pull up?
So maybe I change my thinking on reed switch placement and put a resister from D4 to 3v3 and put the reed switch from D4 to GRD and change code so that reed switch reads low when open.

That’s another way to accomplish what I want but don’t understand why it is not texting every ten minutes the other way and wondering if it will still happen if I have it text when D4 reads low?


You can use INPUT_PULLDOWN for short wires, but with longer wires (which I assume you will be using) you will need a stronger pull-down.

When your switch is closing to 3v3 then you need a pull-down. If you close toward GND you need a pull-up.

So you’re saying that you get texts at uneven intervals even with the jumper wire in place? If you look at the console, are the Particle.publish events also showing up at something other than 10 minute intervals? Are you still getting some times longer, and some shorter than 10 minutes (using the jumper wire)?

1 Like

Add a pull-down per ScruffR, and check the Console per Ric.
Also remember, a Published Event Applet on IFTTT is officially checked “Within an Hour”, even though it sometimes triggers much quicker.

Another Thought: With so many delays(), it seems that much more than 599 seconds will have to elapse during a door open event before the seconds int variable reaches 599. Most folks on this forum recommend removing all delay()'s if you can.