#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
RTC_DS1307 RTC;
#define Photoresistor 13
#define CurtainsServo 9
Servo CServo;
LiquidCrystal_I2C lcd(0x27, 20, 4);
int ButtonPlus = 9;
int ButtonMinus = 8;
int ButtonSave = 7;
int oraagg;
int minagg;
int annoagg;
int meseagg;
int dayagg;
int currentState = 0;
int sensorPIR = 12;
int state = LOW;
int valC = 0;
int StreetLED = 2;
int NightLED = 3;
const int pinR = 4;
const int pinG = 5;
const int pinB = 6;
const int potR = A0;
const int potG = A1;
const int potB = A2;
void setup(){
lcd.init();
lcd.backlight();
lcd.clear();
pinMode(ButtonPlus, INPUT);
pinMode(ButtonMinus, INPUT);
pinMode(ButtonSave, INPUT);
pinMode(sensorPIR, INPUT);
pinMode(Photoresistor, INPUT);
pinMode(potR, INPUT);
pinMode(potG, INPUT);
pinMode(potB, INPUT);
pinMode(StreetLED, OUTPUT);
pinMode(NightLED, OUTPUT);
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
Serial.begin(9600);
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
RTC.adjust(DateTime(__DATE__, __TIME__));
}
int currentState = 0;
CServo.attach(CurtainsServo);
CServo.write(0);
}
int readPot(int pin) {
return map(analogRead(pin), 0, 1023, 0, 255);
}
void loop(){
//Зміна кольорів освітлення
analogWrite(pinR, readPot(potR));
analogWrite(pinG, readPot(potG));
analogWrite(pinB, readPot(potB));
DateTime now = RTC.now();
//Включення вуличних ліхтарів у темний час
if (digitalRead(Photoresistor) == LOW) {
digitalWrite(StreetLED, LOW);
} else {
digitalWrite(StreetLED, HIGH);
}
//Автоматичне відкриття/закриття штор
CServo.write(valC);
if (digitalRead(Photoresistor) == LOW && now.hour()>23 && now.hour()<6) {
valC=90;
} else {
valC=0;
}
//Налаштування нічників
if (now.hour()>1 && now.hour()<6){
digitalWrite(NightLED, HIGH);
} else {
digitalWrite(NightLED, LOW);
}
//Автоматичне регулювання вуличних ліхтарів
if (digitalRead(Photoresistor) == LOW && digitalRead(sensorPIR)== 1) {
delay(100);
digitalWrite(StreetLED, HIGH);
} else {
digitalWrite(StreetLED, LOW);
}
//Налаштування часу
if (!digitalRead(ButtonSave) == HIGH) {
currentState = currentState + 1;
}
if (currentState == 0) {
DisplayDateTime();
}
if (currentState == 1) {
DisplaySetHour();
}
if (currentState == 2) {
DisplaySetMinute();
}
if (currentState == 3) {
DisplaySetYear();
}
if (currentState == 4) {
DisplaySetMonth();
}
if (currentState == 5) {
DisplaySetDay();
}
if (currentState == 6) {
StoreAgg();
delay(500);
currentState = 0;
}
delay(100);
}
void DisplayDateTime() {
DateTime now = RTC.now();
lcd.setCursor(0, 0);
if (now.hour() <= 9) {
lcd.print("0");
}
lcd.print(now.hour(), DEC);
oraagg = now.hour();
lcd.print(":");
if (now.minute() <= 9) {
lcd.print("0");
}
lcd.print(now.minute(), DEC);
minagg = now.minute();
lcd.print(":");
if (now.second() <= 9) {
lcd.print("0");
}
lcd.print(now.second(), DEC);
lcd.setCursor(10, 0);
if (now.day() <= 9) {
lcd.print("0");
}
lcd.print(now.day(), DEC);
dayagg = now.day();
lcd.print("/");
if (now.month() <= 9) {
lcd.print("0");
}
lcd.print(now.month(), DEC);
meseagg = now.month();
lcd.print("/");
lcd.print(now.year(), DEC);
annoagg = now.year();
}
void DisplaySetHour() {
lcd.clear();
DateTime now = RTC.now();
int ora = now.hour();
if (digitalRead(ButtonPlus) == HIGH) {
if (oraagg == 23) {
oraagg = 0;
}
else {
oraagg = oraagg + 1;
}
}
if (digitalRead(ButtonMinus) == HIGH) {
if (oraagg == 0) {
oraagg = 23;
}
else {
oraagg = oraagg - 1;
}
}
lcd.setCursor(0, 0);
lcd.print("Set time:");
lcd.setCursor(0, 1);
lcd.print(oraagg, DEC);
delay(200);
}
void DisplaySetMinute() {
lcd.clear();
if (digitalRead(ButtonPlus) == HIGH) {
if (minagg == 59) {
minagg = 0;
}
else {
minagg = minagg + 1;
}
}
if (digitalRead(ButtonMinus) == HIGH) {
if (minagg == 0) {
minagg = 59;
}
else {
minagg = minagg - 1;
}
}
lcd.setCursor(0, 0);
lcd.print("Set Minutes:");
lcd.setCursor(0, 1);
lcd.print(minagg, DEC);
delay(200);
}
void DisplaySetYear() {
lcd.clear();
if (digitalRead(ButtonPlus) == HIGH) {
annoagg = annoagg + 1;
}
if (digitalRead(ButtonMinus) == HIGH) {
annoagg = annoagg - 1;
}
lcd.setCursor(0, 0);
lcd.print("Set Year:");
lcd.setCursor(0, 1);
lcd.print(annoagg, DEC);
delay(200);
}
void DisplaySetMonth() {
lcd.clear();
if (digitalRead(ButtonPlus) == HIGH) {
if (meseagg == 12) {
meseagg = 1;
}
else {
meseagg = meseagg + 1;
}
}
if (digitalRead(ButtonMinus) == HIGH) {
if (meseagg == 12) {
meseagg = 12;
}
else {
meseagg = meseagg - 1;
}
}
lcd.setCursor(0, 0);
lcd.print("Set Month:");
lcd.setCursor(0, 1);
lcd.print(meseagg, DEC);
delay(200);
}
void DisplaySetDay() {
lcd.clear();
if (digitalRead(ButtonPlus) == HIGH) {
if (dayagg == 31) {
dayagg = 1;
}
else {
dayagg = dayagg + 1;
}
}
if (digitalRead(ButtonMinus) == HIGH) {
if (dayagg == 1) {
dayagg = 31;
}
else {
dayagg = dayagg - 1;
}
}
lcd.setCursor(0, 0);
lcd.print("Set Day:");
lcd.setCursor(0, 1);
lcd.print(dayagg, DEC);
delay(200);
}
void StoreAgg() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SAVING");
lcd.setCursor(0, 1);
lcd.print("IN PROGRESS");
RTC.adjust(DateTime(annoagg, meseagg, dayagg, oraagg, minagg, 0));
delay(1000);
lcd.clear();
}