Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Arduino.h"
- #define ARMED_LIGHT_PIN 12
- #define DISARM_CIRCUIT_PIN 2
- #define EXPLODE_BUZZER_PIN 8
- #define WRONG_WIRE_PIN 7
- // Game state
- bool isArmed = true; // True if bomb is armed
- bool hasExploded = false; // True if wrong wire cut
- // Debouncing variables
- const unsigned long DEBOUNCE_DELAY = 50; // Debounce time (ms)
- unsigned long lastDisarmDebounceTime = 0;
- unsigned long lastWrongWireDebounceTime = 0;
- int lastDisarmState = HIGH; // Last stable state of disarm pin
- int lastWrongWireState = HIGH; // Last stable state of wrong wire pin
- /*
- * Initializes pins and sets initial game state.
- * - Sets LED and buzzer as outputs.
- * - Sets circuit pins as inputs with internal pull-up resistors (HIGH when open, LOW when grounded).
- * - Turns on armed LED to indicate game start.
- */
- void setup() {
- pinMode(ARMED_LIGHT_PIN, OUTPUT);
- pinMode(DISARM_CIRCUIT_PIN, INPUT_PULLUP);
- pinMode(EXPLODE_BUZZER_PIN, OUTPUT);
- pinMode(WRONG_WIRE_PIN, INPUT_PULLUP);
- digitalWrite(ARMED_LIGHT_PIN, HIGH); // BOMB IS ARMED INITIALLY
- }
- /*
- * Reads and debounces circuit inputs.
- * - Reads current state of disarm and wrong wire pins.
- * - If state changes, starts debounce timer.
- * - After debounce delay, updates stable state.
- * - Returns true if either pin's stable state is LOW (wire cut).
- */
- bool readInputs(int &disarmState, int &wrongWireState) {
- int currentDisarmState = digitalRead(DISARM_CIRCUIT_PIN);
- int currentWrongWireState = digitalRead(WRONG_WIRE_PIN);
- if (currentDisarmState != lastDisarmState) {
- lastDisarmDebounceTime = millis();
- }
- if (millis() - lastDisarmDebounceTime > DEBOUNCE_DELAY) {
- disarmState = currentDisarmState;
- }
- if (currentWrongWireState != lastWrongWireState) {
- lastWrongWireDebounceTime = millis();
- }
- if (millis() - lastWrongWireDebounceTime > DEBOUNCE_DELAY) {
- wrongWireState = currentWrongWireState;
- }
- lastDisarmState = currentDisarmState;
- lastWrongWireState = currentWrongWireState;
- return (disarmState == LOW || wrongWireState == LOW);
- }
- /*
- * Updates game state based on inputs.
- * - If bomb is armed and hasn't exploded:
- * - Disarm if correct wire is cut (disarmState LOW).
- * - Explode if wrong wire is cut (wrongWireState LOW).
- * - Locks game after disarming or exploding.
- */
- void updateGameState(int disarmState, int wrongWireState) {
- if (isArmed && !hasExploded) {
- if (disarmState == LOW) {
- isArmed = false; // Bomb disarmed
- } else if (wrongWireState == LOW) {
- hasExploded = true; // Bomb exploded
- }
- }
- }
- /*
- * Updates LED and buzzer based on game state.
- * - Armed: LED on, buzzer off.
- * - Disarmed: LED off, buzzer off.
- * - Exploded: LED off (or flashes), buzzer on.
- */
- void updateOutputs() {
- if (isArmed && !hasExploded) {
- digitalWrite(ARMED_LIGHT_PIN, HIGH); // Armed: LED on
- noTone(EXPLODE_BUZZER_PIN); // Buzzer off
- } else if (!isArmed) {
- digitalWrite(ARMED_LIGHT_PIN, LOW); // Disarmed: LED off
- noTone(EXPLODE_BUZZER_PIN); // Buzzer off
- } else if (hasExploded) {
- digitalWrite(ARMED_LIGHT_PIN, LOW); // Exploded: LED off
- tone(EXPLODE_BUZZER_PIN, 500); // Buzzer on (500 Hz)
- // Optional: Flash LED
- // digitalWrite(ARMED_LIGHT_PIN, (millis() / 250) % 2);
- }
- }
- /*
- * Main game loop.
- * - Reads inputs, updates game state, and sets outputs.
- * - Only processes inputs if game is active (armed, not exploded).
- */
- void loop() {
- int disarmState = HIGH;
- int wrongWireState = HIGH;
- // Read inputs and check if any wire is cut
- if (readInputs(disarmState, wrongWireState)) {
- updateGameState(disarmState, wrongWireState);
- }
- // Update outputs based on game state
- updateOutputs();
- }
Advertisement
Add Comment
Please, Sign In to add comment