They are all very close to what i want it to do but still not quite right.
This is the code i'm using at the moment. Still pinched off Mr Jeff Saltzman but slightly modified
/* Click and Press+Hold Test Sketch
By Jeff Saltzman
To keep input interfaces simple, I want to use a single button to:
1) click (fast press and release) for regular button use, and
2) press and hold to enter a configuration mode.
#define buttonPin 6 // analog input pin to use as a digital input
#define ledPin1 12 // digital output pin for LED 1 indicator
#define ledPin2 13 // digital output pin for LED 2 indicator
#define debounce 20 // ms debounce period to prevent flickering when pressing or releasing the button
#define holdTime 1000 // ms hold period: how long to wait for press+hold event
// Button variables
int buttonVal = 0; // value read from button
int buttonLast = 0; // buffered value of the button's previous state
long btnDnTime; // time the button was pressed down
long btnUpTime; // time the button was released
boolean ignoreUp = false; // whether to ignore the button release because the click+hold was triggered
boolean menuMode = false;
// LED variables
boolean ledVal2 = false; // state of LED 2
// Set button input pin
digitalWrite(buttonPin, HIGH );
// Set LED output pins
// Read the state of the button
buttonVal = digitalRead(buttonPin);
// Test for button pressed and store the down time
if (buttonVal == LOW && buttonLast == HIGH && (millis() - btnUpTime) > long(debounce))
btnDnTime = millis();
// Test for button release and store the up time
if (buttonVal == HIGH && buttonLast == LOW && (millis() - btnDnTime) > long(debounce))
if (ignoreUp == false) event1();
else ignoreUp = false;
btnUpTime = millis();
// Test for button held down for longer than the hold time
if (buttonVal == LOW && (millis() - btnDnTime) > long(holdTime))
ignoreUp = true;
btnDnTime = millis();
buttonLast = buttonVal;
// Events to trigger by click and press+hold
ledVal2 = !ledVal2;
I modified it slightly so that event 1 simply flashes the led on briefly instead of cycling between on and off. More just for prettiness than functionality. I also added a debounce delay after the button is pressed at the start of the loop because the button was a bit erratic.
What i really want is for the same button that flashes the led momentarily to flash a different led while event 2 is active. i.e. pressing the button briefly will have one function but after pressing and holding the same button to activate a menu mode, a brief press of the same button will now have a different function.
I added a "boolean menuMode = false;" statement during the intial declarations with the intention of having it go "true" after the long press. Should i be making a 3rd event that states that when menuMode = true, the button has a different function to when it is false? If so, I just have no idea how to go about doing that.
Can anyone point me in the right direction?