Particle.publish function publishes 4 times once triggered

I was building a Internet connected Rattrap. It was supposed to publish “Rat Trapped” to the cloud once D6 pin goes low. It does get triggered but it publishes 4 times once triggered. Here is the code.

int triggerPin = D6;
bool messageSent = false;
void setup() {
pinMode(triggerPin, INPUT);
}

void loop() {
int trigger = digitalRead(triggerPin);
if(trigger == LOW && messageSent == false){
Particle.publish(“Rat Trapped”);
messageSent = true;
}
if(trigger == HIGH){
messageSent = false;
}

}

Most switches require debouncing - at the moment of contact or release it may switch back and forth between high and low several times. Since you got 4 timestamps in the same second, and there’s a maximum of 4 publishes per second, I’d guess that’s what you’re seeing.

There are a number of ways you could fix this; in your trap example instead of clearing after a single HIGH you might wait for, say 50 high loops before clearing messageSent.

Thanks for the help! I will try to implement this debouncing thing! :sweat_smile:

@rickkas7 I tried to implement debouncing but It’s still publishing 4 events. May be it’s an issue with my code. Here It is

int triggerPin = D6;
bool messageSent = false;
int lbounce = 0;
int hbounce = 0;
int lowbounce;
int highbounce;
void setup() {
pinMode(triggerPin, INPUT);
}

void loop() {
int trigger = digitalRead(triggerPin);
if(trigger == LOW){
lowbounce = lbounce++;
}
if(trigger == HIGH){
highbounce = hbounce++;
}
if(lowbounce >= 50 && messageSent == false){
Particle.publish(“RatTrapped”);
messageSent = true;
}
if(highbounce >= 50){
messageSent = false;
}
}

Please Help!

Your logic isn’t quite right; since low bounce and high bounce are never reset to zero, it’s not quite going to work. I was thinking something simpler like:

int triggerPin = D6;
bool messageSent = false;
int highCount = 0;

void setup() {
    pinMode(triggerPin, INPUT);
}

void loop() {
    int trigger = digitalRead(triggerPin);
    if(trigger == LOW && messageSent == false){
        Particle.publish("Rat Trapped");
        messageSent = true;
        highCount = 0;
    }
    if(trigger == HIGH && messageSent == true) {
        if (highCount++ > 100) {
            messageSent = false;
        }
    }
}
1 Like

@rickkas7 Copied+Pasted+Flashed. Its still publishing 4 events. :cry:

Do you have an external pull-up resistor on your switch? If not, try changing pinMode(triggerPin, INPUT); to pinMode(triggerPin, INPUT_PULLUP); Also, try changing the test to 1000 from 100. I just ran a test on an actual Photon and it worked with those settings.

@rickkas7 No, I Haven’t attached any external PULLUP Resistor. Currently, This is my current Code

int triggerPin = D6;
bool messageSent = false;
int highCount = 0;

void setup() {
    pinMode(triggerPin, INPUT_PULLUP);
}

    void loop() {
        int trigger = digitalRead(triggerPin);
        if(trigger == LOW && messageSent == false){
            Particle.publish("Rat Trapped");
            messageSent = true;
            highCount = 0;
        }
        if(trigger == HIGH && messageSent == true) {
            if (highCount++ > 1000) {
                messageSent = false;
            }
        }
    }

Now, It’s NOT even publishing once…

The switch connects from D6 to ground (not 3.3V), right? If that’s not it, I’m stumped.

@rickkas7 Seems like I have to connect a External Pull-up. It’s working fine if I manually connect D6 from 3.3v to GND. Thanks for the help! :relaxed: