Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
SeanMabli2
Published

Arduino Alarm

With the use of a DS1307 RTC Module, you can accurately set alarms and read the time on an LCD Display.

BeginnerFull instructions provided8,464
Arduino Alarm

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
LCD Display
×1
DS1307 RTC Module
×1
Active Buzzer Module
×1
Push Button Module
×1
Breadboard
×1
Jumper Wire
×23

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Arduino Alarm Schematic

Code

Arduino Alarm

Arduino
#include <Wire.h>
#include <DS3231.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
DS3231 clock;
RTCDateTime dt;

int buzzer = 2; //Alarm
int button = 4; //Snooze
int button_read;
bool alarm_set;
bool alarm_beep;
String alarm_hour;
String alarm_min;
String alarm_AM_PM;
int time_hour;
String time_AM_PM;
int i; //Frequency of Alarm

void setup() {

  lcd.begin(16, 2);

  Serial.begin(9600);

  pinMode(buzzer, OUTPUT);
  pinMode(button, INPUT_PULLUP);

  clock.begin();
    //clock.setDateTime(__DATE__, __TIME__);

  lcd.setCursor(6, 1);
  lcd.print(" Set Alarm");

  Serial.print("Enter Hour of Alarm");
  Serial.println();

  alarm_set =  false;
  alarm_beep = true;
}

void loop() {
  dt = clock.getDateTime();

  if (alarm_hour == 0) {
    if (Serial.available() > 0) {
      alarm_hour = Serial.readString();
      Serial.println("Alarm Hour Set: " + alarm_hour);
      lcd.clear();
      Serial.print("Enter Minute of Alarm");
      Serial.println();
      Serial.end();
      Serial.begin(9600);
    }
  }
  if (alarm_min == 0) {
    if (Serial.available() > 0) {
      alarm_min = Serial.readString();
      Serial.println("Alarm Minute Set: " + alarm_min);
      lcd.clear();
      Serial.print("Enter Ante Meridiem of Alarm");
      Serial.println();
      Serial.end();
      Serial.begin(9600);
    }
  }
  if (alarm_AM_PM == 0) {
    if (Serial.available() > 0) {
      alarm_AM_PM = Serial.readString();
      Serial.println("Alarm Ante Meridiem Set: " + alarm_AM_PM);
      lcd.clear();
      Serial.print("Alarm Set!");
      Serial.println();
      Serial.end();
      Serial.begin(9600);
      alarm_set = true;
    }
  }

  Time_Display();

  Alarm_Display();

  if ((alarm_beep == true) && (alarm_set == true) && (alarm_min.toInt() == dt.minute) && (alarm_hour.toInt() == time_hour) && (time_AM_PM == alarm_AM_PM)) {
    for (i = 0; i < 80; i++)
    {
      digitalWrite(buzzer, HIGH);
      delay(1);
      digitalWrite(buzzer, LOW);
      delay(1);
      Time_Display();
    }
    for (i = 0; i < 100; i++)
    {
      digitalWrite(buzzer, HIGH);
      delay(2);
      digitalWrite(buzzer, LOW);
      delay(2);
      Time_Display();
    }
    button_read = digitalRead(button);
    if (button_read == LOW) {
      alarm_beep = false;
      Time_Display();
    }
  }
}

Alarm Display

Arduino
void Alarm_Display()  {
  
  Time_Display();
  
  lcd.setCursor(0, 1);
  lcd.print("Alarm:");

  if (alarm_set == true) {
    if (alarm_hour.toInt() > 9) {
      lcd.setCursor(6, 1);
      lcd.print(alarm_hour.toInt());
    }
    else {
      lcd.setCursor(7, 1);
      lcd.print(alarm_hour.toInt());
    }

    lcd.setCursor(8, 1);
    lcd.print(":");

    if (alarm_min.toInt() > 9) {
      lcd.setCursor(9, 1);
      lcd.print(alarm_min.toInt());
    }
    else {
      lcd.setCursor(9, 1);
      lcd.print("0");
      lcd.setCursor(10, 1);
      lcd.print(alarm_min.toInt());
    }

    lcd.setCursor(12, 1);
    lcd.print(alarm_AM_PM);

    lcd.setCursor(11, 1);
    lcd.print(" ");
    lcd.setCursor(14, 1);
    lcd.print("    ");
  }
  Time_Display();
}

Time Display

Arduino
void Time_Display()  {
  lcd.setCursor(0, 0);
  lcd.print("Time:");

  if (dt.hour > 12) {
    time_hour = (dt.hour - 12);
    if (time_hour > 9) {
      lcd.setCursor(6, 0);
      lcd.print(time_hour);
    }
    else {
      lcd.setCursor(7, 0);
      lcd.print(time_hour);
    }

    lcd.setCursor(8, 0);
    lcd.print(":");

    if (dt.minute > 9) {
      lcd.setCursor(9, 0);
      lcd.print(dt.minute);
    }
    else {
      lcd.setCursor(9, 0);
      lcd.print("0");
      lcd.setCursor(10, 0);
      lcd.print(dt.minute);
    }
    if (dt.hour != 12) {
      lcd.setCursor(12, 0);
      lcd.print("PM\n");
      time_AM_PM = "PM\n";
    }
    else {
      lcd.setCursor(12, 0);
      lcd.print("AM\n");
      time_AM_PM = "AM\n";
    }
  }
  else {
    time_hour = dt.hour;
    if (dt.hour > 9) {
      lcd.setCursor(6, 0);
      lcd.print(dt.hour);
    }
    else {
      lcd.setCursor(7, 0);
      lcd.print(dt.hour);
    }
    lcd.setCursor(8, 0);
    lcd.print(":");
    if (dt.minute > 9) {
      lcd.setCursor(9, 0);
      lcd.print(dt.minute);
    }
    else {
      lcd.setCursor(9, 0);
      lcd.print("0");
      lcd.setCursor(10, 0);
      lcd.print(dt.minute);
    }
    if (dt.hour != 12) {
      lcd.setCursor(12, 0);
      lcd.print("AM\n");
      time_AM_PM = "AM\n";
    }
    else {
      lcd.setCursor(12, 0);
      lcd.print("PM\n");
      time_AM_PM = "PM\n";
    }
  }
  lcd.setCursor(11, 0);
  lcd.print(" ");
  lcd.setCursor(14, 0);
  lcd.print(" ");
}

Credits

SeanMabli2

SeanMabli2

6 projects • 7 followers

Comments