bitwise_gamgee

Untitled

Jun 9th, 2025
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.76 KB | Fixit | 0 0
  1. #include "Arduino.h"
  2. #define ARMED_LIGHT_PIN 12
  3. #define DISARM_CIRCUIT_PIN 2
  4. #define EXPLODE_BUZZER_PIN 8
  5. #define WRONG_WIRE_PIN 7    
  6.  
  7. // Game state
  8. bool isArmed = true;        // True if bomb is armed
  9. bool hasExploded = false;   // True if wrong wire cut
  10.  
  11. // Debouncing variables
  12. const unsigned long DEBOUNCE_DELAY = 50; // Debounce time (ms)
  13. unsigned long lastDisarmDebounceTime = 0;
  14. unsigned long lastWrongWireDebounceTime = 0;
  15. int lastDisarmState = HIGH;  // Last stable state of disarm pin
  16. int lastWrongWireState = HIGH; // Last stable state of wrong wire pin
  17.  
  18. /*
  19.  * Initializes pins and sets initial game state.
  20.  * - Sets LED and buzzer as outputs.
  21.  * - Sets circuit pins as inputs with internal pull-up resistors (HIGH when open, LOW when grounded).
  22.  * - Turns on armed LED to indicate game start.
  23.  */
  24. void setup() {
  25.   pinMode(ARMED_LIGHT_PIN, OUTPUT);
  26.   pinMode(DISARM_CIRCUIT_PIN, INPUT_PULLUP);
  27.   pinMode(EXPLODE_BUZZER_PIN, OUTPUT);
  28.   pinMode(WRONG_WIRE_PIN, INPUT_PULLUP);
  29.   digitalWrite(ARMED_LIGHT_PIN, HIGH); // BOMB IS ARMED INITIALLY
  30. }
  31.  
  32. /*
  33.  * Reads and debounces circuit inputs.
  34.  * - Reads current state of disarm and wrong wire pins.
  35.  * - If state changes, starts debounce timer.
  36.  * - After debounce delay, updates stable state.
  37.  * - Returns true if either pin's stable state is LOW (wire cut).
  38.  */
  39. bool readInputs(int &disarmState, int &wrongWireState) {
  40.   int currentDisarmState = digitalRead(DISARM_CIRCUIT_PIN);
  41.   int currentWrongWireState = digitalRead(WRONG_WIRE_PIN);
  42.  
  43.   if (currentDisarmState != lastDisarmState) {
  44.     lastDisarmDebounceTime = millis();
  45.   }
  46.   if (millis() - lastDisarmDebounceTime > DEBOUNCE_DELAY) {
  47.     disarmState = currentDisarmState;
  48.   }
  49.  
  50.   if (currentWrongWireState != lastWrongWireState) {
  51.     lastWrongWireDebounceTime = millis();
  52.   }
  53.   if (millis() - lastWrongWireDebounceTime > DEBOUNCE_DELAY) {
  54.     wrongWireState = currentWrongWireState;
  55.   }
  56.  
  57.   lastDisarmState = currentDisarmState;
  58.   lastWrongWireState = currentWrongWireState;
  59.  
  60.   return (disarmState == LOW || wrongWireState == LOW);
  61. }
  62.  
  63. /*
  64.  * Updates game state based on inputs.
  65.  * - If bomb is armed and hasn't exploded:
  66.  *   - Disarm if correct wire is cut (disarmState LOW).
  67.  *   - Explode if wrong wire is cut (wrongWireState LOW).
  68.  * - Locks game after disarming or exploding.
  69.  */
  70.  
  71. void updateGameState(int disarmState, int wrongWireState) {
  72.   if (isArmed && !hasExploded) {
  73.     if (disarmState == LOW) {
  74.       isArmed = false; // Bomb disarmed
  75.     } else if (wrongWireState == LOW) {
  76.       hasExploded = true; // Bomb exploded
  77.     }
  78.   }
  79. }
  80.  
  81. /*
  82.  * Updates LED and buzzer based on game state.
  83.  * - Armed: LED on, buzzer off.
  84.  * - Disarmed: LED off, buzzer off.
  85.  * - Exploded: LED off (or flashes), buzzer on.
  86.  */
  87.  
  88. void updateOutputs() {
  89.   if (isArmed && !hasExploded) {
  90.     digitalWrite(ARMED_LIGHT_PIN, HIGH); // Armed: LED on
  91.     noTone(EXPLODE_BUZZER_PIN);          // Buzzer off
  92.   } else if (!isArmed) {
  93.     digitalWrite(ARMED_LIGHT_PIN, LOW);  // Disarmed: LED off
  94.     noTone(EXPLODE_BUZZER_PIN);          // Buzzer off
  95.   } else if (hasExploded) {
  96.     digitalWrite(ARMED_LIGHT_PIN, LOW);  // Exploded: LED off
  97.     tone(EXPLODE_BUZZER_PIN, 500);       // Buzzer on (500 Hz)
  98.     // Optional: Flash LED
  99.     // digitalWrite(ARMED_LIGHT_PIN, (millis() / 250) % 2);
  100.   }
  101. }
  102.  
  103. /*
  104.  * Main game loop.
  105.  * - Reads inputs, updates game state, and sets outputs.
  106.  * - Only processes inputs if game is active (armed, not exploded).
  107.  */
  108.  
  109. void loop() {
  110.   int disarmState = HIGH;
  111.   int wrongWireState = HIGH;
  112.  
  113.   // Read inputs and check if any wire is cut
  114.   if (readInputs(disarmState, wrongWireState)) {
  115.     updateGameState(disarmState, wrongWireState);
  116.   }
  117.  
  118.   // Update outputs based on game state
  119.   updateOutputs();
  120. }
Tags: Arduino
Advertisement
Add Comment
Please, Sign In to add comment