Soy un principiante completo con Arduino, pero he hecho un poco de búsqueda y parece que no puedo solucionar este problema.
Estoy intentando crear un procedimiento al que se llama repetidamente en el loop()
principal que establecerá una variable según el estado actual de un botón, así como la duración que se presionó o presionó el botón. Este código probablemente sea más desordenado de lo que debe ser, pero parece que deja la variable en 0
. ¿Qué me estoy perdiendo?
Hay un código en el mismo loop()
que establece la variable rotaryButtonToAction
nuevamente en noPress
una vez que se ha actuado.
#define noPress 0
#define shortPressReleased 1
#define longPressReleased 2
#define shortPressHolding 3
#define longPressHolding 4
int rotaryButtonState;
int rotaryButtonReading;
int lastRotaryButtonReading = HIGH;
int timeOfLastRotaryButtonReadingChange = 0;
int debounceDelay = 50;
int longPressDelay = 300;
int rotaryButtonToAction = 0;
void checkRotaryButton() {
if (rotaryButtonToAction == noPress) { // if there is nothing to action, check the state of the button
rotaryButtonReading = digitalRead(ROTARY_BUTTON); // store pin's current state
if (rotaryButtonReading != lastRotaryButtonReading) { // if pin state has changed from the last reading, set timeOfLastRotaryButtonReadingChange to the time of the change
timeOfLastRotaryButtonReadingChange = millis();
}
if (millis() - timeOfLastRotaryButtonReadingChange > debounceDelay) { // if more time has passed than the debounce delay, change the action state of the button
if (rotaryButtonReading = LOW) { // if the button is still pressed, set the action to indicate it has been pressed and is still pressed
rotaryButtonToAction = shortPressHolding;
}
else { // if the button was pressed for longer than the debounce delay and was released, set the action
rotaryButtonToAction = shortPressReleased;
}
}
}
if (millis() - timeOfLastRotaryButtonReadingChange > longPressDelay) { // if more time has passed than the long press delay, change the action state of the button
if (rotaryButtonReading == LOW) { // if the button is still pressed, set the action to indicate it has been pressed and is still pressed
rotaryButtonToAction = longPressHolding;
}
else { // if the button was pressed for longer than the long press delay and was released, set the action
rotaryButtonToAction = longPressReleased;
}
}
} // end checkRotaryButton