Ciao a tutti ho riscontrato un problema nel pulsante reset, ovvero il mio obiettivo è quello che se metto in pausa il timer, e poi premo il tasto reset si resetti tutto.
Il problema sta che se premo il tasto pausa mi va direttamente nella sezione reset e non capisco.
Prima di inserire il tasto reset la funzione play e pausa funzionava correttamente come posso risolvere questo problema?
#include <Arduino.h>
#include <TM1637Display.h>
//display 7 segmenti
#define CLK 2
#define DIO 3
//START-PAUSA
const int buttonPin = 7;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
//RESET
const int reset_buttonPin = 8;
int reset_buttonState = 0;
//variabile tempo
int k = 10;
int step_funzione = 0;
TM1637Display display(CLK, DIO);
unsigned long previousMillis = 0;
const long interval = 1000;
void setup() {
Serial.begin(4800);
pinMode(buttonPin, INPUT);
pinMode(reset_buttonPin, INPUT);
}
void loop() {
switch (step_funzione) {
case 0:
inizio();
break;
case 1:
edit_time();
break;
case 2: // VAI ALLA FUNZIONE: CONTO ALLA ROVESCIA
countdown();
break;
case 3:
tempo_scaduto();
break;
case 4:
reset();
break;
}
}
void inizio(){
//START
Serial.println("Inizio");
display.setBrightness(0x0f);
display.showNumberDec(k);
//-- VAI A SETTA IL TEMPO
//-- VAI A COUNTDOWN
int buttonState = digitalRead(buttonPin);
if(buttonState == HIGH){
step_funzione = 2;
}
}
void edit_time(){
Serial.println("Edit_time");
//inserisci encoder
}
////////////////////////////////COUNTDOWN///////////////////////////////
void countdown(){
Serial.println("Countdown");
int buttonState = digitalRead(buttonPin);
if(buttonState != lastButtonState){
if (buttonState == HIGH){
buttonPushCounter++;
}
delay(50);
}
lastButtonState = buttonState;
if (buttonPushCounter % 2 == 1){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
display.showNumberDec(k);
k--;
if(k<0){
step_funzione = 3;
}
}
}else{
Serial.println("Pausa");
int reset_buttonState = digitalRead(reset_buttonPin);
if(reset_buttonState == HIGH){
step_funzione = 4;
delay(50);//delay per capire il secondo comando.
}
}
}
///////////////////////////////////TEMPO SCADUTO///////////////////////////////
void tempo_scaduto() {
int buttonState = digitalRead(buttonPin);
if(buttonState == HIGH){
k=10;
step_funzione = 0;
delay(50);//delay per capire il secondo comando.
}
}
//////////////////////////////RESET///////////////////////////////////////////
void reset(){
Serial.println("Reset");
}