#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Wire.h>
#include "RTClib.h"////// DS1307 RTC for Date and Time
#include "DHT.h" ////// DHT22 FOR HUMIDITY AND TEMPERATURE
#define DHTPIN 25 // what pin we're connected to
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
//Variables
int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value
//////////////////////////////////////////
#define buzzer 27
#define BUTTON1 A0
#define BUTTON2 A1
#define BUTTON3 A2
#define LED1 13
#define LED2 12
//////////////////////
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const char ROWS = 4;
const char COLS = 20;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
int cursorColumn = 0;
char colPins[COLS] = { 5, 4, 3, 2 };
char rowPins[ROWS] = { 9, 8, 7, 6 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 20, 4);
int alarmHour;
int alarmMinute;
void setup() {
Serial.begin(9600);
while (!init); // for Leonardo/Micro/Zero
lcd.init();
lcd.backlight();
dht.begin();
if (! rtc.begin()) {
lcd.setCursor(0, 3);
lcd.print("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
lcd.setCursor(1, 3);
lcd.print("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
alarmHour = 15;
alarmMinute = 0;
pinMode(buzzer, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2,OUTPUT);
}
void Time(){
DateTime now = rtc.now();
/////DATE
int minute = now.minute();
int hour = now.hour();
lcd.setCursor(0, 0);
lcd.print(now.year());
lcd.setCursor(4,0);
lcd.print('/');
if(now.month()<=9){
lcd.setCursor(5,0);
lcd.print("0");
lcd.setCursor(6,0);
lcd.print(now.month());
}
if(now.month()>=10){
lcd.setCursor(6,0);
lcd.print(now.month());
}
lcd.setCursor(7,0);
lcd.print('/');
lcd.setCursor(8,0);
lcd.print(now.day());
lcd.setCursor(10,0);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
//////TIME
lcd.setCursor(0,1);
if(hour < 10){
lcd.print(0);
}
lcd.print(now.hour());
lcd.setCursor(2,1);
lcd.print(':');
lcd.setCursor(3,1);
if(minute < 10){
lcd.print(0);
}
lcd.print(now.minute());
lcd.setCursor(5,1);
lcd.print(':');
lcd.setCursor(6,1);
lcd.print(now.second());
lcd.println();
}
void HumidityTemperature(){
hum = dht.readHumidity();
temp= dht.readTemperature();
lcd.setCursor(0,3);
lcd.print(hum);
lcd.setCursor(5,3);
lcd.println(" %");
lcd.setCursor(8,3);
lcd.print(temp);
lcd.setCursor(14,3);
lcd.println((char)223);
lcd.setCursor(15,3);
lcd.println("C");
}
void DisplayAlarm(){
lcd.setCursor(0, 2);
if(alarmHour < 10){
lcd.print(0);
}
lcd.print(alarmHour);
lcd.print(':');
if(alarmMinute < 10){
lcd.print(0);
}
lcd.print(alarmMinute);
lcd.print(' ');
}
void loop() {
char key = keypad.getKey();
lcd.setCursor(2,2);
lcd.print(":");
HumidityTemperature();
Time();
DisplayAlarm();
if(digitalRead(BUTTON1)==LOW)
{ lcd.setCursor(0, 2);
alarmHour = hours();
alarmMinute = minutes();
int dowhile = 0;
tone(buzzer, 988, 250);
while(dowhile == 0)
{
char key = keypad.getKey();
if(key == 'A')
{
dowhile = 1;
lcd.clear();
}
}
}
DateTime now = rtc.now();
if(digitalRead(BUTTON2)==LOW){
lcd.clear();
lcd.setCursor (6,2);
lcd.print("ON");
Serial.print(alarmHour);
Serial.print(" ");
Serial.println(now.hour());
Serial.print(alarmMinute);
Serial.print(" ");
Serial.println(now.minute());
}
if(digitalRead(BUTTON3)==LOW){
lcd.clear();
lcd.setCursor (6,2);
lcd.print("OFF");
}
if(alarmHour == now.hour() && alarmMinute == now.minute() ){
Serial.print("ALARMSSSSS ");
tone(buzzer, 988, 250);
delay(1000);
tone(buzzer, 988, 250);
delay(400);
tone(buzzer, 880, 250);
delay(400);
tone(buzzer, 988, 250);
delay(1000);
tone(buzzer, 988, 250);
delay(400);
tone(buzzer, 880, 250);
delay(400);
tone(buzzer, 988, 250);
delay(1000);
tone(buzzer, 988, 250);
delay(400);
tone(buzzer, 880, 250);
delay(400);
tone(buzzer, 784, 1000);
delay(1000);
tone(buzzer, 880, 1000);
}
}
int hours(){
int hours = 0;
int timerHour = 0;
lcd.setCursor(0, 2);
lcd.print(" Alarm: ");
lcd.setCursor(14, 2);
lcd.print(":");
lcd.setCursor(12, 2);
int a = 0;
while(a == 0)
{
char key = keypad.getKey();
if(key == '0' || key == '1' || key == '2')
{
lcd.print(key);
hours = key - 48;
tone(buzzer, 1000, 10);
a = 1;
}
}
timerHour = hours * 10;
hours = 0;
a = 0;
while(a == 0)
{
char key = keypad.getKey();
if(timerHour == 20)
{
if(key == '0' || key == '1' || key == '2' || key == '3'||key == '4'||key == '5'||key == '6'||key == '7'||key == '8'||key == '9')
{
lcd.print(key);
hours = key - 48;
tone(buzzer, 1000, 10);
a = 1;
}
}
else
{
if(key != NULL)
{
if(isdigit(key))
{
tone(buzzer, 1000, 10);
lcd.print(key);
hours = key - 48;
a = 1;
}
}
}
}
timerHour = timerHour + hours;
return timerHour;
}
int minutes(){
int minutes = 0;
int timerMinutes = 0;
lcd.setCursor(15, 2);
int a = 0;
while(a == 0)
{
char key = keypad.getKey();
if(key == '0' || key == '1' || key == '2' || key == '3' || key == '4' || key == '5')
{
lcd.print(key);
minutes = key - 48;
tone(buzzer, 1000, 10);
a = 1;
}
}
timerMinutes = minutes * 10;
minutes = 0;
a = 0;
while(a == 0){
char key = keypad.getKey();
if(key != NULL)
{
if(isdigit(key))
{
lcd.print(key);
minutes = key - 48;
tone(buzzer, 1000, 10);
a = 1;
}
}
}
timerMinutes = timerMinutes + minutes;
return timerMinutes;
}