Hello I cannot wrap my head around how to implement a long press funktion in my sketch.
I have tried a bunch of things but it doesn't work.
Basically I want every button press to be a long press, because the button I have is spring loaded and it sometimes make a fast double press which gives false readings.
Therefore to eliminate that issue I want the button to be press at least 1 second before it gets detected as a press.
Here is my sketch:
#define trButton 2 // training/reset button (being eliminated/modified)
#define processInput 2 // needs the sensor from the process
#define faultLED 3
#define readyLED 13
#define runningLED 10
bool needCalibration; // set this to force a calibration run
enum bar { IDLE = 0,
MEASURE,
CHECK,
IFAULT,
NFAULT,
};
void setup() {
Serial.begin(9600);
Serial.print("\nhello world.\n");
pinMode(readyLED, OUTPUT);
pinMode(runningLED, OUTPUT);
pinMode(faultLED, OUTPUT);
pinMode(trButton, INPUT_PULLUP);
pinMode(processInput, INPUT_PULLUP);
needCalibration = true; // first run, and any run after a fault
}
unsigned int counter;
unsigned char state = IDLE; // does. is.
unsigned long now;
unsigned long timeLimit;
unsigned long timer;
void loop() {
delay(20);
now = millis();
bool controlX = digitalRead(processInput) == HIGH;
bool buttonState = digitalRead(trButton) == HIGH;
static bool lastButtonState;
static bool buttonPress;
if (buttonState != lastButtonState) {
if (buttonState) {
buttonPress = !buttonPress;
}
lastButtonState = buttonState;
}
switch (state) {
case IDLE:
digitalWrite(readyLED, HIGH);
digitalWrite(runningLED, LOW);
if (controlX) {
timer = now;
// if (buttonPress || needCalibration) {
if (needCalibration) {
state = MEASURE;
Serial.print("training for time\n");
buttonPress = false;
needCalibration = false;
} else {
state = CHECK;
Serial.print("X up. monitoring time\n");
}
}
break;
case CHECK:
digitalWrite(readyLED, LOW);
digitalWrite(runningLED, HIGH);
if (now - timer > timeLimit) {
state = IFAULT;
break;
}
if (!controlX) {
Serial.print("X down made it by ");
Serial.println(timeLimit - (now - timer));
state = IDLE;
}
break;
case MEASURE:
digitalWrite(readyLED, LOW);
digitalWrite(runningLED, HIGH);
if (!controlX) {
timeLimit = now - timer;
timeLimit += timeLimit / 10; // comfort margarine 10 percent here
Serial.print("X down new period = ");
Serial.println(timeLimit);
state = IDLE;
}
break;
case IFAULT:
Serial.print(" initialize fault mechanism");
state = NFAULT;
Serial.print(" / perpetuate fault mechanism\n");
digitalWrite(faultLED, HIGH);
buttonPress = false; // eat any stray button presses
break;
case NFAULT:
digitalWrite(readyLED, LOW);
digitalWrite(runningLED, LOW);
digitalWrite(faultLED, millis() & 512 ? HIGH : LOW);
if (buttonPress) {
Serial.print("sytem to IDLE\n");
digitalWrite(faultLED, LOW);
buttonPress = false;
needCalibration = true;
state = IDLE;
}
break;
}
}