For the boolean variable section, or mode selection i tried implementing something as per your advice, or what i understood it to be. It kind of works, I'm just using LEDs to see if it's working. LED 2 flashes after 3 seconds but LED 1 lights straight away and only turns off when LED 2 flashes. When i press the button again LED 2 flashes straight away then turns off when LED 1 turns on after 3 seconds.
boolean mode1 = true;
const int BUTTON_PIN = 3; // the number of the pushbutton pin
const int LONG_PRESS_TIME = 3000; // 1000 milliseconds
int LED1 = 0;
int LED2 = 1;
int LED1State;
// Variables will change:
int lastState = LOW; // the previous state from the input pin
int currentState; // the current reading from the input pin
unsigned long pressedTime = 0;
bool isPressing = false;
bool isLongDetected = false;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode (LED1, OUTPUT);
digitalWrite (LED1, LOW);
pinMode (LED2, OUTPUT);
digitalWrite (LED2, LOW);
}
void loop() {
// read the state of the switch/button:
currentState = digitalRead(BUTTON_PIN);
if (lastState == HIGH && currentState == LOW) { // button is pressed
pressedTime = millis();
isPressing = true;
isLongDetected = false;
} else if (lastState == LOW && currentState == HIGH) { // button is released
isPressing = false;
}
if (isPressing == true && isLongDetected == false) {
long pressDuration = millis() - pressedTime;
if ( pressDuration > LONG_PRESS_TIME ) {
mode1 = !mode1;
isLongDetected = true;
}
if (mode1) {
LED1State = !LED1State;
digitalWrite (LED1, LED1State);
}
else {
digitalWrite (LED2, HIGH);
delay (100);
digitalWrite (LED2, LOW);
delay (100);
digitalWrite (LED2, HIGH);
delay (100);
digitalWrite (LED2, LOW);
}
}
lastState = currentState;
}