#include <Arduino.h>
enum Estado {
INICIAL,
ESCOLHE,
CONTAGEM_DECRESCENTE,
};
const int buttonPin1 = 9;
const int buttonPin2 = 10;
const int segmentPins[8] = {0, 1, 2, 3, 4, 5, 6, 7};
const byte numbers[10] = {
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110 // 9
};
Estado estadoAtual = INICIAL;
int valorInicial = 5;
int cont_Decrescente;
void setup() {
for (int i = 0; i < 8; i++) {
pinMode(segmentPins[i], OUTPUT);
}
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
displayNumber(valorInicial);
}
void loop() {
int buttonState1 = digitalRead(buttonPin1);
int buttonState2 = digitalRead(buttonPin2);
if (buttonState1 == LOW && buttonState2 == HIGH && estadoAtual == ESCOLHE) {
valorInicial = min(9, valorInicial + 1);
displayNumber(valorInicial);
}
else if (buttonState1 == HIGH && buttonState2 == LOW && estadoAtual == ESCOLHE) {
valorInicial = max(5, valorInicial - 1);
displayNumber(valorInicial);
}
else if (buttonState1 == LOW && buttonState2 == LOW) {
estadoAtual = CONTAGEM_DECRESCENTE;
cont_Decrescente = valorInicial;
displayNumber(cont_Decrescente);
}
if (estadoAtual == CONTAGEM_DECRESCENTE) {
delay(1000);
cont_Decrescente--;
if (cont_Decrescente < 0) {
for (int i = 0; i < 3; i++) {
digitalWrite(segmentPins[0], LOW);
delay(500);
displayNumber(0); // Blink zero
delay(500);
}
cont_Decrescente = valorInicial;
displayNumber(cont_Decrescente);
}
else {
displayNumber(cont_Decrescente);
}
}
}
void displayNumber(int num) {
for (int i = 0; i < 8; i++) {
digitalWrite(segmentPins[i], HIGH); // Assume HIGH is off
}
// Activate segments based on the number to be displayed
for (int i = 0; i < 8; i++) {
if (bitRead(numbers[num], i) == LOW) {
digitalWrite(segmentPins[7-i], LOW);
}
}
}