int valor1 = 0, valor2 = 0;
int cont = 0;
int estado = 0;
bool entrada1 = false;
bool entrada0 = false;
const int MAX = 3;
const int botao1 = 2; // Incrementar
const int botao0 = 3; // Enter
const int led0 = 8;
const int led1 = 9;
const int led2 = 10;
const int segmentos[] = {4, 5, 6, 7, 11, 12, 13};
const byte digitos[10][7] = {
{1,1,1,1,1,1,0}, // 0
{0,1,1,0,0,0,0}, // 1
{1,1,0,1,1,0,1}, // 2
{1,1,1,1,0,0,1}, // 3
{0,1,1,0,0,1,1}, // 4
{1,0,1,1,0,1,1}, // 5
{1,0,1,1,1,1,1}, // 6
{1,1,1,0,0,0,0}, // 7
{1,1,1,1,1,1,1}, // 8
{1,1,1,1,0,1,1} // 9
};
void setup() {
Serial.begin(9600);
Serial.println("Sistema iniciado. Display = 0");
pinMode(botao1, INPUT_PULLUP);
pinMode(botao0, INPUT_PULLUP);
pinMode(led0, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
for (int i = 0; i < 7; i++) {
pinMode(segmentos[i], OUTPUT);
}
exibeDisplay(0);
}
void loop() {
exibeDisplay(cont);
// Botão de incremento
if (leitura(botao1) == LOW && !entrada1) {
entrada1 = true;
cont++;
if (cont > MAX) cont = 0;
Serial.print("Número atual: ");
Serial.println(cont);
}
if (leitura(botao1) == HIGH && entrada1) {
entrada1 = false;
}
// Botão Enter
if (leitura(botao0) == LOW && !entrada0) {
entrada0 = true;
switch (estado) {
case 0:
valor1 = cont;
Serial.print("Primeiro número confirmado: ");
Serial.println(valor1);
cont = 0;
estado = 1;
Serial.println("Inserindo segundo número...");
break;
case 1:
valor2 = cont;
Serial.print("Segundo número confirmado: ");
Serial.println(valor2);
cont = 0;
estado = 2;
mostrarResultado(valor1 + valor2);
break;
case 2:
apagarLEDs();
Serial.println("Sistema reiniciado.");
Serial.println("Inserindo novo primeiro número...");
cont = 0;
estado = 0;
break;
}
}
if (leitura(botao0) == HIGH && entrada0) {
entrada0 = false;
}
delay(10); // debounce
}
void exibeDisplay(int numero) {
for (int i = 0; i < 7; i++) {
digitalWrite(segmentos[i], digitos[numero][i]);
}
}
void mostrarResultado(int resultado) {
Serial.print("Resultado da soma: ");
Serial.print(resultado);
Serial.print(" (binário: ");
Serial.print(bitRead(resultado, 2));
Serial.print(bitRead(resultado, 1));
Serial.print(bitRead(resultado, 0));
Serial.println(")");
digitalWrite(led0, bitRead(resultado, 0));
digitalWrite(led1, bitRead(resultado, 1));
digitalWrite(led2, bitRead(resultado, 2));
}
void apagarLEDs() {
digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
int leitura(int pino) {
if (digitalRead(pino) == LOW) {
delay(20); // debounce simples por temporização
if (digitalRead(pino) == LOW) return LOW;
}
return HIGH;
}