Interaktywny system do głosowania/quizu - projekt pozwoli na zbudowanie prostego systemu,
który reaguje na wybory i prezentuje wyniki.
Co będzie potrzebne:
Arduino UNO + kabel USB
płytka stykowa 400 punktów
przewody połączeniowe męsko-męskie
przewody połączeniowe żeńsko-żeńskie
2 x dioda LED (zielona, czerwona)
2 x rezystor 330 Ohm
2 x przycisk (Tact switch)
2 x rezystor 10 kOhm
wyświetlacz szeregowy LCD 2x16 znaków (opcjonalnie)
Budujemy układ
Przycisk A → pin 2 Arduino
Przycisk B → pin 3 Arduino
Każdy przycisk do masy przez rezystor 10 kΩ
Drugi koniec przycisku do +5V
Zielona LED (poprawna) → pin 8 przez rezystor 220 Ω
Czerwona LED (błędna) → pin 9 przez rezystor 220 Ω
Buzzer → pin 10 (drugi pin buzzera do masy)
Programujemy
Wersja bez wyświetlacza LCD, pytanie wyświetla się na porcie 19600 baund.
const int buttonA = 2;
const int buttonB = 3;
const int ledGreen = 8;
const int ledRed = 9;
const int buzzer = 10;
const char correctAnswer = 'A'; // poprawna odpowiedź to A
bool pytanieWyswietlone = false;
void setup() {
pinMode(buttonA, INPUT);
pinMode(buttonB, INPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600); // <-- inicjalizacja komunikacji szeregowej
}
void loop() {
if (!pytanieWyswietlone) {
Serial.println("Quiz: Ile to jest 2 + 2?");
Serial.println("A) 4");
Serial.println("B) 5");
pytanieWyswietlone = true;
}
if (digitalRead(buttonA) == HIGH) {
handleAnswer('A');
delay(3000);
reset();
}
if (digitalRead(buttonB) == HIGH) {
handleAnswer('B');
delay(3000);
reset();
}
}
void handleAnswer(char answer) {
if (answer == correctAnswer) {
digitalWrite(ledGreen, HIGH);
tone(buzzer, 1000, 300); // pozytywny dźwięk
Serial.println("Poprawna odpowiedz!");
} else {
digitalWrite(ledRed, HIGH);
tone(buzzer, 200, 500); // negatywny dźwięk
Serial.println("Niepoprawna odpowiedz.");
}
}
void reset() {
digitalWrite(ledGreen, LOW);
digitalWrite(ledRed, LOW);
noTone(buzzer);
pytanieWyswietlone = false;
}
Wersja z wyświetlaczem LCD
LCD → Arduino
VSS → GND
VDD → +5V
V0 → Środkowy pin potencjometru (10kΩ)
RS → D12
RW → GND
E → D11
D4 → D5
D5 → D4
D6 → D3
D7 → D2
A → +5V przez 220Ω
K → GND
Programujemy:
#include <LiquidCrystal.h>
// Inicjalizacja LCD: RS, E, D4, D5, D6, D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Piny przycisków
const int buttonA = 7;
const int buttonB = 6;
// Piny LED i buzzera
const int ledGreen = 8;
const int ledRed = 9;
const int buzzer = 10;
const char correctAnswer = 'A';
bool questionDisplayed = false;
void setup() {
lcd.begin(16, 2);
pinMode(buttonA, INPUT);
pinMode(buttonB, INPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(buzzer, OUTPUT);
lcd.clear();
}
void loop() {
if (!questionDisplayed) {
lcd.setCursor(0, 0);
lcd.print("Ile to 2 + 2 ?");
lcd.setCursor(0, 1);
lcd.print("A) 4 B) 5");
questionDisplayed = true;
}
if (digitalRead(buttonA) == HIGH) {
handleAnswer('A');
delay(3000);
reset();
}
if (digitalRead(buttonB) == HIGH) {
handleAnswer('B');
delay(3000);
reset();
}
}
void handleAnswer(char answer) {
lcd.clear();
if (answer == correctAnswer) {
digitalWrite(ledGreen, HIGH);
tone(buzzer, 1000, 300);
lcd.print("Poprawna :)");
} else {
digitalWrite(ledRed, HIGH);
tone(buzzer, 200, 500);
lcd.print("Zle :(");
}
}
void reset() {
digitalWrite(ledGreen, LOW);
digitalWrite(ledRed, LOW);
noTone(buzzer);
questionDisplayed = false;
delay(500);
}