#include "RTClib.h"
#include <LiquidCrystal.h>
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sun ", "Mon ", "Tue ", "Wed ", "Thu ", "Fri ", "Sat "};
const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int sw = 4;
int dt = 3;
int clk = 2;
int cw = 0;
int contrast = 7 ;
int LDR_sensor = 5;
const float BETA = 3950;
int menu = 0;
void time(){
lcd.clear();
DateTime now = rtc.now();
lcd.setCursor(0,0);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(now.year(), DEC);
lcd.print("/");
lcd.print(now.month(),DEC);
lcd.print("/");
lcd.print(now.day(),DEC);
lcd.setCursor(0,1);
lcd.print("Time ");
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
lcd.print(now.second(), DEC);
delay(50);
}
void encoder()
{
if(digitalRead(clk)==0)
{
cw++;
delay(200);
}
if(digitalRead(dt)==0)
{
cw--;
delay(200);
}
if(digitalRead(sw)==0)
{
menu++;
delay(50);
}
}
void setup() {
lcd.begin(16, 2);
lcd.print("loading...");
pinMode(clk , INPUT);
pinMode(dt, INPUT);
pinMode(sw , INPUT_PULLUP);
rtc.begin();
Serial.begin(57600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
//rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
attachInterrupt(digitalPinToInterrupt(clk), encoder, LOW);
attachInterrupt(digitalPinToInterrupt(dt), encoder, LOW);
}
void loop() {
if(digitalRead(sw)==0)
{
menu++;
delay(50);
}
switch (menu) {
case 0:
time();
break;
case 1:
switch(cw)
{
case 0:
lcd.clear();
lcd.print(">Display time");
lcd.setCursor(0,1);
lcd.print(" Room condition");
delay(50);
break;
case 1:
lcd.clear();
lcd.print(" Display time");
lcd.setCursor(0,1);
lcd.print(">Room condition");
delay(50);
break;
}
break;
case 2 :
if(cw==0)
{
time();
}
if(cw==1)
{
room_condition();
}
break;
case 3:
menu = 0;
cw = 0;
break;
}
if(cw>1)
{
cw=0;
}
if(cw<0)
{
cw=1;
}
if(digitalRead(LDR_sensor)==1)
{
digitalWrite(contrast, HIGH);
}
if(digitalRead(LDR_sensor)==0)
{
digitalWrite(contrast , LOW);
}
}
void room_condition(){
lcd.clear();
int analogValue = analogRead(A0); //to convert from Kelv to C
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
lcd.print(" Temp : ");
lcd.print(celsius);
lcd.println(" *c");
lcd.setCursor(0,1);
if (digitalRead(LDR_sensor) == LOW) {
lcd.print(" -Room Light!-");
}
else {
lcd.print(" -Room Dark-");
}
delay(50);
}