johnwasser:
Or: If the button is released before the 'long press' interval is reached, do a short press. If the 'long press' interval is reached, do a long press. That way you don't have to wait until the button is released to do the long press. It will happen when the time is reached.
const byte ButtonPin = 2;
const unsigned long DebounceTime = 30;
const unsigned long LongPressInterval = 1000;
boolean ButtonWasPressed = false;
boolean ButtonBeingTimed = false;
unsigned long ButtonStateChangeTime = 0; // Debounce timer
void setup()
{
pinMode (ButtonPin, INPUT_PULLUP); // Button between Pin and Ground
}
void loop()
{
checkButton();
}
void checkButton()
{
unsigned long currentTime = millis();
boolean buttonIsPressed = digitalRead(ButtonPin) == LOW; // Active LOW
// Check for button state change and do debounce
if (buttonIsPressed != ButtonWasPressed &&
currentTime - ButtonStateChangeTime > DebounceTime)
{
// Button state has changed
ButtonWasPressed = buttonIsPressed;
if (ButtonWasPressed)
{
ButtonBeingTimed = true;
}
else
{
// Button was just released
ButtonBeingTimed = false;
if (currentTime - ButtonStateChangeTime < LongPressInterval)
{
// Short Press
}
}
ButtonStateChangeTime = currentTime;
}
if (ButtonBeingTimed && ButtonWasPressed)
{
// Button is still pressed
if (currentTime - ButtonStateChangeTime >= LongPressInterval)
{
ButtonBeingTimed = false;
// Long Press=
}
}
}
This layout has been very helpful! Thank you so much! I tried to add my LED in the mix and can only get the latching portion to work. Trying to figure out how to get out of the momentary part of the code.
const int ButtonPin = 2;
const int LedPin = 13;
const unsigned long DebounceTime = 100;
const unsigned long LongPressInterval = 150;
boolean ButtonWasPressed = false;
boolean ButtonBeingTimed = false;
unsigned long ButtonStateChangeTime = 0; // Debounce timer
void setup()
{
Serial.begin(9600); // Start the serial monitor to see the results.
char fileName[] = {__FILE__};
Serial.println(fileName);
pinMode (ButtonPin, INPUT); // Button between Pin and Ground
digitalWrite(ButtonPin, HIGH);
pinMode(LedPin, OUTPUT);
}
void loop()
{
checkButton();
}
void checkButton()
{
unsigned long currentTime = millis();
boolean buttonIsPressed = digitalRead(ButtonPin) == LOW; // Active LOW
// Check for button state change and do debounce
if (buttonIsPressed != ButtonWasPressed &&
currentTime - ButtonStateChangeTime > DebounceTime)
{
// Button state has changed
ButtonWasPressed = buttonIsPressed;
if (ButtonWasPressed)
{
ButtonBeingTimed = true;
}
else
{
// Button was just released
ButtonBeingTimed = false;
if (currentTime - ButtonStateChangeTime < LongPressInterval)
{
Serial.println("Short press"); // Short Press
digitalWrite(LedPin, !digitalRead(LedPin));
}
}
ButtonStateChangeTime = currentTime;
}
if (ButtonBeingTimed && ButtonWasPressed)
{
// Button is still pressed
if (currentTime - ButtonStateChangeTime >= LongPressInterval)
{
ButtonBeingTimed = false;
Serial.println("Long press");
digitalWrite(LedPin, HIGH);
//??? How to get out of this
}else{
Serial.println("Off");
digitalWrite(LedPin, LOW);
}
}
}
as of right now, when the LED has been engaged with a short press, the long press function will just make it blink once and when the LED is completely off, it won't even turn on with the hold function. I tried adding a quick else statement but no luck.