#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DS3231.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const byte brightnessPin = 6;
byte i = 0;
byte ledGreen = 3;
byte ledRed = 2;
int LDRInput = A0;
int PSInput = A1;
int valuePS,cp,test;
DS3231 Clock;
byte year, month, date, DoW, hour, minute, second;
bool century = false;
bool h12, pm;
const int delayPeriod = 1000;
unsigned long lastTime;
void setup()
{
lcd.init();
lcd.clear();
lcd.display();
lcd.backlight();
pinMode(PSInput, INPUT_PULLUP);
Serial.begin(9600);
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT); // set leds as outputs
pinMode(LDRInput, INPUT);
pinMode(PSInput, INPUT); // set sensors as inputs
lcd.init();
GetDateTime();
}
void loop()
{
//LDR
int valueLDR=analogRead(LDRInput);
Serial.println("LDR value is :");
Serial.println(valueLDR);
if(valueLDR>500) //no light
{
lcd.noDisplay();
lcd.noBacklight();// no display
}
else
{
lcd.display();
lcd.backlight(); // otherwise display on
}
//PS
int valuePS = analogRead(PSInput);
Serial.println("PS value is :");
Serial.println(valuePS);
valuePS = analogRead(PSInput);
if (valuePS==8) //if someone on seat
{ test=1;
cp=0; // initialize the timer to 0
while(test==1) {
lcd.setCursor(4, 2);
cp++; // increment the counter
lcd.print("Timer :");
lcd.print(cp);
delay(1000);
valuePS = analogRead(PSInput);
if (valuePS >9) { //if the come off seat
test=0; // stop the clock
delay(1000);
}
}
}
//rtc
if(millis()- lastTime > delayPeriod)
{
lastTime = millis();
GetDateTime();
String dt =GetDateTimeString();
Serial.println(dt);
lcd.setCursor(3,0);
lcd.print(dt);
}
}
void GetDateTime() //for RTC, read time as previously set on RTC
{
date = Clock.getDate();
month = Clock.getMonth( century);
year = Clock.getYear();
DoW = Clock.getDoW();
hour = Clock.getHour( h12, pm);
minute = Clock.getMinute();
second = Clock.getSecond();
}
String GetDateTimeString() //for RTC, format time for printing
{
String dt = String(date) + "/" + String(month) + "/" + String(year) + " ";
dt += Get2Digits(hour) + ":" + Get2Digits(minute) + ":" + Get2Digits(second);
return dt;
}
String Get2Digits(byte number) // for RTC
{
return number < 10 ?" " + String(number) : String(number);
}