Criando um projeto usando 10 leds, um Buzzer, dois pushbutton em um contador num Arduino
Vamos criar um projeto com Arduino que utiliza:
- 10 LEDs para indicar visualmente a contagem;
- 2 pushbutton para incrementar a contagem;
- 1 buzzer (acho que você quis dizer “buzzer”, não “burger” 😊) para emitir um som ao contar;
- Um contador que vai de 0 a 10 (limitado pelos 10 LEDs).
🧠 Funcionamento
- Botão 1: incrementa o contador.
- Botão 2: decrementa o contador.
- O contador permanece entre 0 e 10.
- Os LEDs mostram a quantidade atual.
- O buzzer toca em cada alteração.
🔧 Materiais
- 10 resistores de 220Ω (para os LEDs)
- 10 LEDs (5 de cada cor ‘Vermelho, Azul, Amarelo, Verde, Laranja’)
- 1 buzzer (ativo ou passivo)
- 2 pushbutton
- 2 resistor de 10kΩ (pull-down para os botões)
- Arduino UNO (ou compatível)
- Jumpers
- Protoboard
⚡ Esquema de Conexões (Resumo)
- LEDs: pinos 2 a 11 do Arduino
- Botão 1: pino 12
- Botão 2: pino 0
- Buzzer: pino 13
🧾 Código Arduino
const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; const int incButtonPin = 12; const int decButtonPin = 0; const int buzzerPin = 13; int counter = 0; bool lastIncState = LOW; bool lastDecState = LOW; unsigned long lastIncDebounce = 0; unsigned long lastDecDebounce = 0; void setup() { Serial.begin(9600); for (int i = 0; i < 10; i++) { pinMode(ledPins[i], OUTPUT); } pinMode(incButtonPin, INPUT); pinMode(decButtonPin, INPUT); pinMode(buzzerPin, OUTPUT); updateTestLEDs(counter); updateLEDs(counter); } void loop() { handleButton(incButtonPin, lastIncState, lastIncDebounce, true); handleButton(decButtonPin, lastDecState, lastDecDebounce, false); } void handleButton(int pin, bool &lastState, unsigned long &lastTime, bool increment) { int reading = digitalRead(pin); if (reading != lastState) { lastTime = millis(); Serial.print(reading); } if (reading != lastState) { delay(50); if (reading == HIGH && lastState == LOW) { if (increment) { if (counter < 10) counter++; } else { if (counter > 0) counter--; } updateLEDs(counter); tone(buzzerPin, 1000, 100); } } lastState = reading; } void updateLEDs(int value) { for (int i = 0; i < 10; i++) { digitalWrite(ledPins[i], i < value ? HIGH : LOW); } } void updateTestLEDs(int value) { for (int i = 0; i < 10; i++) { digitalWrite(ledPins[i], HIGH); delay(150); } }
Diagrama do Projeto
Abaixo o codigo fonte completo no GitHub
Please follow and like us: