Co będzie potrzebne:
Arduino UNO + kabel USB
płytka stykowa 400 punktów
przewody połączeniowe męsko-męskie
3 x diody LED
3 x rezystor 330 ohm
2 x przycisk
2 x rezystor 10 k ohm
Budujemy układ.
Programujemy:
Skopiuj poniższy kod do Arduine IDE.
int APlayerInput = 2;
int BPlayerInput = 4;
int APlayerLed = 10;
int BPlayerLed = 11;
int RandomLed = 12;
int APlayerState = LOW;
int BPlayerState = LOW;
unsigned long randNumber;
unsigned long minTandomNumber = 2000;
unsigned long maxRandomNumber = 6000;
void setup() {
pinMode(APlayerInput, INPUT);
pinMode(BPlayerInput, INPUT);
pinMode(APlayerLed, OUTPUT);
pinMode(BPlayerLed, OUTPUT);
pinMode(RandomLed, OUTPUT);
Serial.begin(115200);
digitalWrite(APlayerLed, LOW);
digitalWrite(BPlayerLed, LOW);
digitalWrite(RandomLed, LOW);
randomSeed(analogRead(0));
randNumber = random(minTandomNumber,maxRandomNumber);
Serial.println("Przygotuj się do gry ...");
delay(2000);
Serial.println("START!");
delay(randNumber);
digitalWrite(RandomLed, HIGH);
}
void loop() {
APlayerState = digitalRead(APlayerInput);
BPlayerState = digitalRead(BPlayerInput);
if (APlayerState == HIGH){
digitalWrite(APlayerLed, HIGH);
Serial.println("Wygrał gracz A");
delay(20000);
}
else if (BPlayerState == HIGH){
digitalWrite(BPlayerLed, HIGH);
Serial.println("Wygrał gracz B");
delay(20000);
}
delay(20);
}
Zasady gry.
Po uruchomieniu skryptu, czekamy na zapalenie się diody.
Po zapaleniu się diody ten gracz, który pierwszy naciśnie swój przycisk - wygrywa.
Komunikaty informujące o rozgrywce wyświetlane są w Arduino IDE na porcie 115 200.