Argon - having trouble with false button presses

I’ve put this post in here because it’s one of the very first projects we should be doing with our Particles, but I’d appreciate if someone more knowledgeable can check my code and explain why I’m randmonly getting false button presses.
My setup is an argon, powered by USB on an Adafruit tripler board with an xbee, having just 3.3v, GND, and TX pins connected to the xbee.
I have a terminal block connected to GND and A1. This is where I’ve connected the pushbutton.
The purpose of this setup is to use as a remote trigger for a loading dock warning system, which obviously is hopeless if it keeps random button pressing!

/*  Simple sketch which takes a momentary push button as input then sends command to Xbee via uart.  A receiving device with Xbee will then respond accordingly.
*/
SYSTEM_THREAD(ENABLED);

// Global variables
int led = 7; // Use the onboard blue LED
int pushButton = A1;  // pushbutton is connected to A1
int reading;
int previous = HIGH;
unsigned long timeOfPress = 0;
unsigned long debounce = 200;
bool buttonPressed = 0;
bool goodPress = 0;

void setup() {
    Serial.begin(9600);
    Serial1.begin(9600);
    pinMode(led, OUTPUT);  // Initialise D7 pin as output
    pinMode(pushButton, INPUT_PULLUP);  // Initialise A1 pin as input with internal pullup resistor
}

void loop() {
    int reading = digitalRead(pushButton);
    if ((reading == LOW) && (previous == HIGH)){
        timeOfPress = millis();
        previous = LOW;
    }
    if ((millis() - timeOfPress > debounce) && (reading == LOW) && (previous == LOW)){
            digitalWrite(led, HIGH);
            previous = LOW;
            reading = digitalRead(pushButton);
            goodPress = 1;
    }
    if ((goodPress) && (reading == HIGH) && (previous == LOW)){
                digitalWrite(led, LOW);
                buttonPressed = 1;
                previous = HIGH;
                goodPress = 0;
     }

    if (buttonPressed){
          Serial.println();
          Serial.println("Button Released");
         Serial.write(0x2A);
         Serial.write(0xA);
          Serial1.write(0x2A);
          Serial1.write(0xA);
        buttonPressed = 0;
        digitalWrite(led, LOW);
        delay(4000);
    }
}

Your debounce code looks ok, I have not tested it yet.

How long is the wire going to your button? Is it shielded?

More trouble shooting and we were able to find the issue. The pushbutton is one of those big mushroom head ones, with about 500mm of wire connecting it to the particle. We found that when we disconnected the button the false presses completely stopped. So with this knowledge we put a capacitor across the terminals on the button (I think it was a 1uF from memory, just whatever we had on hand) and viola, problem resolved.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.