I added an lcd.print to the readButton() function.
With 'PID myPID(&input, &output, &setpoint, 1.0, 1.0, 1.0, DIRECT );' commented out, it prints 1 when the button isn't clicked, and 0 when it's clicked (seems correct.) If I uncomment the PID code, it produces the number 42949552. I can't work out why!
Edit: If I comment out handleShortPress() and handleLongPress() it produced the 1 and 0 again.
Code below:
void readButton() {
int reading = digitalRead(ENCODER_SW);
lcd.setCursor(8, 0);
lcd.print(int(reading));
if (reading != lastButtonState) lastDebounceTime = millis();
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
pressStartTime = millis();
buttonPressedFlag = false;
} else {
unsigned long pressDuration = millis() - pressStartTime;
if (pressDuration >= 500) {
handleLongPress();
} else if (!buttonPressedFlag) {
handleShortPress();
buttonPressedFlag = true; // Prevent repeated short press handling
}
}
}
}
lastButtonState = reading;
}