/*
Wire Loop Buzzer Game
Compatible with ATtiny13 and ATtiny85
Robert's 3-strike game with LEDs and buzzer
Wiring:
PB0 - Wand input (loop contact, active LOW, INPUT_PULLUP)
PB1 - Buzzer output
PB2 - LED1 (yellow)
PB3 - LED2 (orange)
PB4 - LED3 (red)
*/
#define WAND_PIN 0
#define BUZZER_PIN 1
#define LED1_PIN 2
#define LED2_PIN 3
#define LED3_PIN 4
uint8_t strikes = 0;
bool contact = false;
unsigned long lastTouch = 0;
void setup() {
pinMode(WAND_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(LED3_PIN, OUTPUT);
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, LOW);
}
void loop() {
bool hit = (digitalRead(WAND_PIN) == LOW);
if (hit && !contact) {
contact = true;
handleStrike();
lastTouch = millis();
}
// simple debounce / holdoff
if (!hit && contact && (millis() - lastTouch > 100)) {
contact = false;
}
}
void handleStrike() {
strikes++;
switch (strikes) {
case 1:
buzz(150);
flashLED(LED1_PIN, 3, 100);
digitalWrite(LED1_PIN, HIGH);
break;
case 2:
buzz(150);
flashLED(LED2_PIN, 3, 100);
digitalWrite(LED2_PIN, HIGH);
break;
case 3:
gameOver();
break;
}
}
void buzz(unsigned int duration) {
// simple software tone, works on all cores
unsigned long t = millis();
while (millis() - t < duration) {
digitalWrite(BUZZER_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(BUZZER_PIN, LOW);
delayMicroseconds(500);
}
}
void flashLED(uint8_t pin, uint8_t times, unsigned int delayMs) {
for (uint8_t i = 0; i < times; i++) {
digitalWrite(pin, HIGH);
delay(delayMs);
digitalWrite(pin, LOW);
delay(delayMs);
}
}
void gameOver() {
for (uint8_t i = 0; i < 5; i++) {
digitalWrite(LED3_PIN, HIGH);
buzz(100);
delay(150);
digitalWrite(LED3_PIN, LOW);
delay(150);
}
// long buzz + all LEDs on
buzz(500);
digitalWrite(LED1_PIN, HIGH);
digitalWrite(LED2_PIN, HIGH);
digitalWrite(LED3_PIN, HIGH);
// stop everything until reset
while (true);
}