Unit 12: Triggered countdown

 

 

 

 

Code for Sketch One: 


 

#include <GyverTM1637.h>

 

// Define pins for TM1637 display

const int CLK_PIN = 3;

const int DIO_PIN = 2;

 

// Define pins for wires

const int WIRE1_PIN = 10, WIRE2_PIN = 11, WIRE3_PIN = 12, WIRE4_PIN = 13;

// Define pin for the buzzer

const int BUZZER_PIN = 8;

 

// Create display object

GyverTM1637 display(CLK_PIN, DIO_PIN);

 

// Variables to manage the countdown timer

int timeRemaining = 120; // Countdown time in seconds (2 minutes)

unsigned long previousMillis = 0;

const long interval = 1000; // Interval for countdown (1 second)

 

// Variable to track if the countdown is running

bool countdownRunning = true;

bool wireCut[4] = {false, false, false, false}; // Track which wires have been cut

 

void setup() {

  // Initialize the display

  display.clear();

  display.brightness(7);

  updateDisplay();

 

  // Initialize pins for wires

  pinMode(WIRE1_PIN, INPUT_PULLUP);

  pinMode(WIRE2_PIN, INPUT_PULLUP);

  pinMode(WIRE3_PIN, INPUT_PULLUP);

  pinMode(WIRE4_PIN, INPUT_PULLUP);

 

  // Initialize pin for the buzzer

  pinMode(BUZZER_PIN, OUTPUT);

}

 

void loop() {

  // Check if the countdown is running

  if (countdownRunning) {

    // Check if it’s time to update the countdown

    unsigned long currentMillis = millis();

    if (currentMillis - previousMillis >= interval) {

      previousMillis = currentMillis;

 

      timeRemaining--;

      updateDisplay();

 

      // If the time runs out, sound the alarm

      if (timeRemaining <= 0) {

        countdownRunning = false;

        soundAlarm();

      }

    }

  }

 

  // Check the status of the wires

  checkWires();

}

 

// Function to check the status of the wires

void checkWires() {

  if (digitalRead(WIRE1_PIN) == HIGH && !wireCut[0]) {

    soundAlarm();

    wireCut[0] = true;

  } else if (digitalRead(WIRE2_PIN) == HIGH && !wireCut[1]) {

    timeRemaining -= 10;

    if (timeRemaining < 0) timeRemaining = 0;

    updateDisplay();

    wireCut[1] = true;

  } else if (digitalRead(WIRE3_PIN) == HIGH && !wireCut[2]) {

    timeRemaining -= 10;

    if (timeRemaining < 0) timeRemaining = 0;

    updateDisplay();

    wireCut[2] = true;

  } else if (digitalRead(WIRE4_PIN) == HIGH && !wireCut[3]) {

    timeRemaining -= 10;

    if (timeRemaining < 0) timeRemaining = 0;

    updateDisplay();

    wireCut[3] = true;

  }

}

 

// Function to update the display

void updateDisplay() {

  int minutes = timeRemaining / 60;

  int seconds = timeRemaining % 60;

  display.display(0, minutes / 10);    // Tens of minutes

  display.display(1, minutes % 10);    // Ones of minutes

  display.display(2, seconds / 10);    // Tens of seconds

  display.display(3, seconds % 10);    // Ones of seconds

  display.point(1); // Display the colon

}

 

// Function to sound the alarm

void soundAlarm() {

  digitalWrite(BUZZER_PIN, HIGH);

  delay(3000); // Sound the alarm for 3 seconds

  digitalWrite(BUZZER_PIN, LOW);

}

 

Code for Sketch Two:


 

 

#include <GyverTM1637.h>

 

// Define pins for the display, wires and buzzer

const int WIRE1_PIN = 10, WIRE2_PIN = 11, WIRE3_PIN = 12, WIRE4_PIN = 13, BUZZER_PIN = 8, CLK_PIN = 3, DIO_PIN = 2;

 

// Create display object

GyverTM1637 display(CLK_PIN, DIO_PIN);

 

int timeRemaining = 120; // Countdown time in seconds (2 minutes)

unsigned long previousMillis = 0;

const long interval = 1000; // Interval for countdown (1 second)

bool countdownRunning = true;

bool wireCut[4] = {false, false, false, false};

 

// Random safe wire variable

int safeWire;

 

void setup() {

  // Initialize random seed

  randomSeed(analogRead(0));

 

  // Select a random safe wire

  safeWire = random(1, 5); // Random number between 1 and 4

 

  // Initialize the display

  display.clear();

  display.brightness(7);

  updateDisplay();

 

  // Initialize pins for wires

  pinMode(WIRE1_PIN, INPUT_PULLUP);

  pinMode(WIRE2_PIN, INPUT_PULLUP);

  pinMode(WIRE3_PIN, INPUT_PULLUP);

  pinMode(WIRE4_PIN, INPUT_PULLUP);

 

  // Initialize pin for the buzzer

  pinMode(BUZZER_PIN, OUTPUT);

}

void loop() {

  if (countdownRunning) {

    unsigned long currentMillis = millis();

    if (currentMillis - previousMillis >= interval) {

      previousMillis = currentMillis;

      timeRemaining--;

      updateDisplay();

 

      // If the time runs out, sound the alarm

      if (timeRemaining <= 0) {

        countdownRunning = false;

        soundAlarm();

      }

    }

  }

 

  checkWires();

 

}

void checkWires() {

  if (digitalRead(WIRE1_PIN) == HIGH && !wireCut[0]) {

    if (safeWire == 1) {

      countdownRunning = false;

    } else {

      timeRemaining -= 10;

      if (timeRemaining < 0) timeRemaining = 0;

    }

    updateDisplay();

    wireCut[0] = true;

  } else if (digitalRead(WIRE2_PIN) == HIGH && !wireCut[1]) {

    if (safeWire == 2) {

      countdownRunning = false;

    } else {

      timeRemaining -= 10;

      if (timeRemaining < 0) timeRemaining = 0;

    }

    updateDisplay();

    wireCut[1] = true;

  } else if (digitalRead(WIRE3_PIN) == HIGH && !wireCut[2]) {

    if (safeWire == 3) {

      countdownRunning = false;

    } else {

      timeRemaining -= 10;

      if (timeRemaining < 0) timeRemaining = 0;

    }

    updateDisplay();

    wireCut[2] = true;

  } else if (digitalRead(WIRE4_PIN) == HIGH && !wireCut[3]) {

    if (safeWire == 4) {

      countdownRunning = false;

    } else {

      timeRemaining -= 10;

      if (timeRemaining < 0) timeRemaining = 0;

    }

    updateDisplay();

    wireCut[3] = true;

  }

}

void updateDisplay() {

  int minutes = timeRemaining / 60;

  int seconds = timeRemaining % 60;

  display.display(0, minutes / 10);    // Tens of minutes

  display.display(1, minutes % 10);    // Ones of minutes

  display.display(2, seconds / 10);    // Tens of seconds

  display.display(3, seconds % 10);    // Ones of seconds

  display.point(1); // Display the colon

}

// Function to sound the alarm

void soundAlarm() {

  digitalWrite(BUZZER_PIN, HIGH);

  delay(3000); // Sound the alarm for 3 seconds

  digitalWrite(BUZZER_PIN, LOW);

}

 

Notes: