LCD Clock (Need an Alarm)

Hello All
I am fairly new to this field so need a bit of guidance with my current project.
I have created LCD clock which displays time but now I need it to create an alarm.

I want to achieve this by inputting the alarm time into the code without the use of buttons and when the time is right a buzzer will sound.

I was hoping some of you would help with guidance of reading the data on the lcd and checking it with the alarm time so that the buzzer can sound at the right time.]

Here is my current code for the display

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define DS1307_I2C_ADDRESS 0x68
#define I2C_ADDR    0x3F
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

void getDateDs1307(byte *second,byte *minute,byte *hour,byte *dayOfWeek,byte *dayOfMonth,byte *month,byte *year)
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  *second      = bcdToDec(Wire.read() & 0x7f);
  *minute      = bcdToDec(Wire.read());
  *hour        = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek   = bcdToDec(Wire.read());
  *dayOfMonth  = bcdToDec(Wire.read());
  *month       = bcdToDec(Wire.read());
  *year        = bcdToDec(Wire.read()); 
}

void setup()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();
  lcd.begin(16, 2);
}

void loop()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  String s, m, d, mth, h;

  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  if (second < 10) { s = "0" + String(second); } else { s = String(second); }
  if (minute < 10) { m = "0" + String(minute); } else { m = String(minute); }
  h = String(hour);
  if (dayOfMonth < 10) { d = "0" + String(dayOfMonth); } else { d = String(dayOfMonth); }
  if (month < 10) { mth = "0" + String(month); } else { mth = String(month); }

  char* days[] = { "NA", "Mon", "Tue", "Wed", "Thu", "Wed", "Thu", "Fri", "Sat", "Sun"};

  lcd.clear();
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.setCursor(4,0);
  lcd.print(h + ":" + m + ":" + s);
  lcd.setCursor(1,1);
  lcd.print(String(days[dayOfWeek]) + " " + d + "/" + mth + "/20" + year);
  delay(1000);
}

Any examples of code or any other guidance is much appreciated
Thank You

Hi

You could perform it just like you've already done, by comparing values by using if's. Principle could be like in this example.

void loop()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  
  //here you'll provide an information, when you'd like to alarm raised, 
  //in this case 13th of July 2017, on 1500 hours but not ever again 
  byte alSecond == 0, alMinute == 0, alHour == 15, alDayOfMonth = 13, alMonth == 7, alYear == 17;
  
  // get time from rtc
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  
  ... //your part of the code
  
  // here you'll compare current time and date with alarm time and date
  if ( second == alSecond && minute == alMinute && hour == alHour 
  && dayOfWeek == alDayOfWeek && dayOfMonth == alDayOfMonth && month == alMonth && year == alYear )
  {
      alarm(); 
  }
  
  ... //your part of the code
  
 }
 
 void alarm()
{
    //subprogram for alarming by using buzzer
 
}

You don't have to use 'dayOfWeek' at all in this, unless you want alarm raised on Tuesdays for example.

By the way:

void setup()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

  ... //your part of the code
}

Declaring these variables in setup() is pretty much useless to do 'cos it's done first thing at (main) loop anyway..

TommiP

PS. By adding those buttons you doesn't want, you'll get adjustable alarm but of course it's your choice to make. :slight_smile:

Thanks for your help TommiP.
One quick problem, in this part of the code

  byte alSecond == 0, alMinute == 0, alHour == 15, alDayOfMonth = 13, alMonth == 7, alYear == 17;

I get the error message
exit status 1
expected initializer before '==' token

I have checked the code for missing semi colons and all seem to be there.

Sorry, my mistake. It should be like this:

  byte alSecond = 0, alMinute = 0, alHour = 15, alDayOfMonth = 13, alMonth = 7, alYear = 17;

TommiP