So I found a code example that does something I want and after stripping out the blinking light stuff that always seems to be in every code example (stop that!, It makes it more confusing, really), I was able to set up this
#define PRESSED LOW
#define NOT_PRESSED HIGH
int led = 13;
const unsigned long shortPress = 100;
const unsigned long longPress = 500;
typedef struct Buttons {
int pin = 10;
const int debounce = 10;
unsigned long counter=0;
bool prevState = NOT_PRESSED;
bool currentState;
} Button;
// create a Button variable type
Button button;
void setup() {
pinMode(led, OUTPUT);
pinMode(button.pin, INPUT_PULLUP);
}
void loop() {
// check the button
button.currentState = digitalRead(button.pin);
// has it changed?
if (button.currentState != button.prevState) {
delay(button.debounce);
// update status in case of bounce
button.currentState = digitalRead(button.pin);
if (button.currentState == PRESSED) {
// a new press event occured
// record when button went down
button.counter = millis();
}
if (button.currentState == NOT_PRESSED) {
// but no longer pressed, how long was it down?
unsigned long currentMillis = millis();
//if ((currentMillis - button.counter >= shortPress) && !(currentMillis - button.counter >= longPress)) {
if ((currentMillis - button.counter >= shortPress) && !(currentMillis - button.counter >= longPress)) {
// short press detected.
handleShortPress();
}
if ((currentMillis - button.counter >= longPress)) {
// the long press was detected
handleLongPress();
}
}
// used to detect when state changes
button.prevState = button.currentState;
}
}
void handleShortPress() {
digitalWrite(led, LOW);
}
void handleLongPress() {
digitalWrite(led, HIGH);
delay(5000);
digitalWrite(led, LOW);
I added the delay part but thats what I need to change.
I want to do the handleLongpress just like that, but I want to be able to make the handleShortPress pull the led LOW if the button is pressed before the 5 seconds is up.
I tried multiple variations of hacking up the blink without delay code so that it didn't blink, just stayed HIGH, but would be able to turn off early and I failed.
I dont know how to do that and I need help.
I cobbled together this code from another example that works as a timer when power is applied and I cant figure out how to apply it to the handleLongButtonPress function either
unsigned long time = millis();
int ledpin = 13;
void setup()
{
pinMode(ledpin, OUTPUT); //Onboard LED
digitalWrite(ledpin, HIGH); //Initial LED state when powered up
}
void loop()
{
if(millis()-time > 5000) //Has 5 seconds passed?
digitalWrite(ledpin, LOW); //turn off LED
}
Im lost, frustrated and I think I just need a clear explanation of what I need to do properly so that I can get the idea of how the work flow should be like.