#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <Wire.h>
#define Button 2
#define led 8
#define led2 7
#define led3 6
#define led4 5
#define SERIAL_OPTION 0
LiquidCrystal_I2C lcd(0x27,16,2);
RTC_DS1307 RTC;
int temp;
const float GAMMA = 0.7;
const float RL10 = 50;
byte lastButtonState;
byte ledstate = LOW;
byte ledtest = LOW;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(Button, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lastButtonState = digitalRead(Button);
RTC.begin();
// RTC.adjust(DateTime(2022,9,30,18,13,40));
}
void loop() {
DateTime now=RTC.now();
byte buttonState = digitalRead(Button);
if(buttonState != lastButtonState){
lastButtonState = buttonState;
if(buttonState == LOW ){
if(ledstate == HIGH){
ledstate = LOW;
}else{
ledstate = HIGH;
}
}
}
light_check();
temp_check();
lcd.setCursor(0,0); lcd.print("DATE: ");
lcd.print(now.year()); lcd.print("/"); lcd.print(now.month()); lcd.print("/"); lcd.print(now.day());
lcd.setCursor(0,1); lcd.print("TIME: ");
lcd.print(now.hour()); lcd.print(":"); lcd.print(now.minute());
delay(100);
temp = now.second();
if(temp == 30){
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
if(temp == 45){
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
}
if(temp == 0){
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
}
}
void light_check(){
int analogValue = analogRead(A0);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.print("Lux = ");
Serial.println(lux);
if(lux >= 400){
digitalWrite(led, HIGH);
}else{
digitalWrite(led, LOW);
}
delay(1000);
}
void temp_check() {
int tempu = analogRead(A1);
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
float celsius = 1 / (log(1 / (1023. / tempu - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature: ");
Serial.print(celsius); // print the temperature in Celsius
Serial.println("°C");
delay(1000); // update sensor reading each one second
}