// #define BUZZER_PIN 7 #define PLAYER_COUNT 2 int buttonPins[PLAYER_COUNT] = {2, 13} int ledPins[PLAYER_COUNT] = {3, 12} void setup() { pinMode(BUZZER_PIN, OUTPUT); for (int player = 0; player < PLAYER_COUNT; ++player) { pinMode(ledPins[player], OUTPUT); pinMode(buttonPins[player], INPUT_PULLUP); } } void loop() { // delay(ms) delay(random(1000, 3000)); // random time // from 1 to 3 seconds // tone(pin, frequency) // tone(pin, frequency, duration) tone(BUZZER_PIN, 2000, 400); // 2 kilohertz, // 400 milliseconds delay(1000); for (int player = 0; ; player = (player+1) % PLAYER_COUNT) { // if player push button if (digitalRead(buttonPins[player])) { // turn on the LED and the victory signal // for 2 seconds digitalWrite(ledPins[player], HIGH); tone(BUZZER_PIN, 3500, 300); delay(1000); digitalWrite(ledPins[player], LOW); // There is a winner! Exit the for loop break; } } }
#define BUZZER_PIN 7
#define PLAYER_COUNT 2
int buttonPins[PLAYER_COUNT] = {2, 13}
int ledPins[PLAYER_COUNT] = {3, 12}
void setup()
{
pinMode(BUZZER_PIN, OUTPUT);
for (int player = 0;
player < PLAYER_COUNT;
++player)
{
pinMode(ledPins[player], OUTPUT);
pinMode(buttonPins[player], INPUT_PULLUP);
}
}
void loop()
{
// delay(ms)
delay(random(1000, 3000)); // random time
// from 1 to 3 seconds
// tone(pin, frequency)
// tone(pin, frequency, duration)
tone(BUZZER_PIN, 2000, 400); // 2 kilohertz,
// 400 milliseconds
delay(1000);
for (int player = 0; ;
player = (player+1) % PLAYER_COUNT) {
// if player push button
if (digitalRead(buttonPins[player])) {
// turn on the LED and the victory signal
// for 2 seconds
digitalWrite(ledPins[player], HIGH);
tone(BUZZER_PIN, 3500, 300);
delay(1000);
digitalWrite(ledPins[player], LOW);
// There is a winner! Exit the for loop
break;
}
}
}