I'm using JChristensen's Button Library to debounce my buttons and provide extra functionality. I'm trying to use his press & hold function to do something if the button is pressed and held for greater than 2 seconds. If it is just pressed momentarily it should do something else. His example use switch cases, and I couldn't get it to work. Here is my example that currently doesn't work.
#include <Button.h>
#include <SPI.h>
#include <printf.h>
// Keypress state setup
byte clearPin = 16; // Clear pin
#define PULLUP true
#define INVERT true // Since the pullup resistor will keep the pin high unless the
#define DEBOUNCE_MS 20 // A debounce time of 20 milliseconds usually works well for tactile button switches.
#define LONG_PRESS 2000 // We define a "long press" to be 2000 milliseconds.
unsigned long ms; // The current time from millis()
Button clearBtn(clearPin, PULLUP, INVERT, DEBOUNCE_MS); // Declare the button
int score=1;
void setup ()
{
pinMode(16, INPUT_PULLUP); // Pin for clear button
Serial.begin(9600); // Radio Setup
}
void loop()
{
clearScore(); // sets both home and away scores to 0 or enters settings menu
}
void clearScore()
{
ms = millis(); //record the current time
clearBtn.read(); // Read the button
if (clearBtn.pressedFor(2000)) {
// Enter settings menu
Serial.println("Settings Menu");
while (clearBtn.read()!=1) {
} // End While
return; // Hopefully not proceed down to the next if
}
if (clearBtn.wasReleased() && !clearBtn.pressedFor(2000)) {
score=0;
Serial.println("Clearing Scores");
delay(50);
}
}
Can anyone help please? My serial output repeats "Settings Menu" over and over. Once I let go of the button variable1 always gets set to 0.