Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Srija Ponna -20
Published © GPL3+

Timer with Flashing Lights and Annoying Sound

This is a timer that counts down the time, then sounds an alarm and flashes lights when time runs out.

IntermediateShowcase (no instructions)5 hours2,696
Timer with Flashing Lights and Annoying Sound

Things used in this project

Story

Read more

Schematics

Timer Fritzing Diagram

Code

Timer Code

Arduino
This code allows for the timer to count down from a given input, change which LEDs are flashing, and sound the alarm and flash the LEDs when time runs out.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int switchPin = 6;
int switchState1 = 0;
int switchState2 = 0;
int prevSwitchState1 = 0;
int prevSwitchState2 = 0;
int note = 100;
int seconds = 15; // can change value here to determine how long you want timer to run


void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 2);
  pinMode(switchPin, INPUT);
  lcd.print(seconds);
  lcd.setCursor(4,0);
  lcd.print("seconds"); // these set up the LCD screen
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT); // these set up the LEDs
}

void loop() {
  // put your main code here, to run repeatedly:
  switchState1 = digitalRead(6);
  switchState2 = digitalRead(13);
  if (switchState1 == LOW) { // if button is not pressed, nothing happens
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
  }
  while (switchState1 == HIGH && seconds > 0) { //if the button is pressed
    delay(1000);
    lcd.clear();
    lcd.print(seconds - 1);
    seconds = seconds - 1; // timer counts down by 1 second
    if (seconds > 10) {
      digitalWrite(7, HIGH); // green LED is on
      digitalWrite(8, LOW); // yellow LED is off
      digitalWrite(9, LOW); // red LED is off
    }
    if (seconds < 10 && seconds > 5) {
      digitalWrite(7, LOW); // green LED is off
      digitalWrite(8, HIGH); // yellow LED is on
      digitalWrite(9, LOW); // red LED is off
    }
    if (seconds < 5 && seconds > 0) {
      digitalWrite(7, LOW); // green LED is off
      digitalWrite(8, LOW); // yellow LED is off
      digitalWrite(9, HIGH); // red LED is on
    }
    if (seconds <= 0) {
      lcd.clear();
      lcd.print("TIME'S UP!"); // time is up
    }
    while (seconds <=0) {
        tone(10, note);
        delay(300);
        noTone(10);
        delay(300); // makes annoying alarm sound
        digitalWrite(7, HIGH);
        delay(100);
        digitalWrite(7, LOW);
        delay(100); // makes green LED flash
        digitalWrite(8, HIGH);
        delay(100);
        digitalWrite(8, LOW);
        delay(100); // makes yellow LED flash
        digitalWrite(9, HIGH);
        delay(100);
        digitalWrite(9, LOW);
        delay(100); // makes red LED flash
    }
  }
}

Credits

Srija Ponna -20

Srija Ponna -20

1 project • 1 follower

Comments