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);
}
}