#include <JC_Button.h> // https://github.com/JChristensen/JC_Button
// pin assignments
const byte BUTTON_PIN(7); // connect a button switch from this pin to ground
const byte LED_PIN(13); // the standard Arduino "pin 13" LED
Button myBtn(BUTTON_PIN); // define the button
void setup() {
Serial.begin(115200);
myBtn.begin(); // initialize the button object
pinMode(LED_PIN, OUTPUT); // set the LED pin as an output
}
enum states_t {PLAY_OFF, PLAY_ON, WAIT_5S};
states_t state = PLAY_OFF; // current state machine state
void loop() {
static uint32_t lastBtnRead;
// read the button
myBtn.read();
// -Se premo il pulsante mantenendolo premuto (suono on)
// -se il pulsante rimane premuto per più di 3 secondi (suono off)
// -se rilascio il pulsante prima dei 3 secondi (suono off)
// -se la pressione del pulsante raggiunge i 3 secondi, il suono rimane disabilitato per 5 secondi e poi è possibile riattivarlo come dall’inizio.
switch (state) {
case PLAY_OFF:
// Se premo il pulsante mantenendolo premuto (suono on)
if (myBtn.wasPressed()) {
lastBtnRead = millis();
state = PLAY_ON;
digitalWrite(LED_PIN, HIGH);
Serial.println("Start play");
// musicPlayer.startPlayingFile("/track002.mp3");
// tutto il resto
}
break;
case PLAY_ON:
// Se rilascio il pulsante prima dei 3 secondi (suono off)
if (myBtn.wasReleased() && millis() - lastBtnRead < 3000) {
state = PLAY_OFF;
digitalWrite(LED_PIN, LOW);
Serial.println("Stop play");
// musicPlayer.stopPlaying();
// tutto il resto
}
// Se il pulsante rimane premuto per più di 3 secondi (suono off).
// Se la pressione del pulsante raggiunge i 3 secondi,
// il suono rimane disabilitato per 5 secondi e poi è possibile riattivarlo come dall’inizio.
if (myBtn.pressedFor(3000)) {
lastBtnRead = millis();
state = WAIT_5S;
digitalWrite(LED_PIN, LOW);
Serial.println("Stop play, wait 5 seconds");
// musicPlayer.stopPlaying();
// tutto il resto
}
break;
case WAIT_5S:
// e poi è possibile riattivarlo come dall’inizio.
if (millis() - lastBtnRead > 5000) {
state = PLAY_OFF;
}
break;
}
}