Sto realizzando un timer...
Il problema è che quando vado a cambiare il tempo da minuti a secondi mi decrementa di un unità il blocco successivo;
Ovvero se sono su i minuti e schiaccio il pulsante per cambiare i secondi diminuiranno di uno;
Il problema è che non esegue sempre questo errore, dove è l'errore?
#include <TM1637Display.h>
#define CLK 2
#define DIO 3
#define outputA 7
#define outputB 8
const int ledPin = 13;
const int buttonPin = 6;
int ledState = 1;
int buttonState;
int lastButtonState = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
/*int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
*/
int click = 0;
int clickState;
int secondi = 0;
int minuti = 0;
int aState;
int aLastState;
int aState_;
int aLastState_;
TM1637Display display(CLK, DIO);
void setup() {
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
pinMode (buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin (9600);
aLastState = digitalRead(outputA);
aLastState_ = digitalRead(outputA);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == 0) {
ledState = !ledState;
}
}
}
digitalWrite(ledPin, ledState);
if(ledState == 1){
click = 1;
}else{
click = 0;
}
lastButtonState = reading;
/*
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 2 == 0) {
digitalWrite(ledPin, HIGH);
click = 0;
} else {
digitalWrite(ledPin, LOW);
click = 1;
}
*/
switch(click){
case 0:
seconds();
break;
case 1:
minutes();
break;
}
}
void seconds(){
Serial.println(secondi);
display.setBrightness(7, true);
display.showNumberDec(secondi, true, 2, 2);
aState = digitalRead(outputA);
if (aState != aLastState){
if (digitalRead(outputB) != aState) {
secondi ++;
if(secondi > 59){
secondi = 0;
}
} else {
secondi --;
if(secondi < 0){
secondi = 59;
}
}
Serial.print("SECONDI: ");
Serial.println(secondi);
}
aLastState = aState;
}
void minutes(){
Serial.println(minuti);
display.setBrightness(7, true);
display.showNumberDec(minuti, true, 2, 0);
aState_ = digitalRead(outputA);
if (aState_ != aLastState_){
if (digitalRead(outputB) != aState_) {
minuti ++;
if(minuti > 99){
minuti = 0;
}
} else {
minuti --;
if(minuti < 0){
minuti = 99;
}
}
Serial.print("MINUTI: ");
Serial.println(minuti);
}
aLastState_ = aState_;
}