#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define encoderClk 2
#define encoderDt 4
#define encoderSw 3
int encoderPos = 0;
int encoderLast = 0;
bool encoderChanged = false;
bool encoderButtonPressed = false;
bool rotated = true; //info of the rotation
bool ButtonPressed = false; //info of the button
int ButtonCounter = 0; //counts the button clicks
int RotateCounter = 0; //counts the rotation clicks
int rotateMenuSayac=4;
//Statuses
int CLKNow;
int CLKPrevious;
int DTNow;
int DTPrevious;
// Timers
float TimeNow1;
float TimeNow2;
//statuses (1/true: ON, 0/false: OFF)
bool whiteLEDStatus = false;
bool blueLEDStatus = false;
bool greenLEDStatus = false;
bool yellowLEDStatus = false;
bool redLEDStatus = false;
int saat, dakika, saniye;
unsigned long son_saniye = millis();
long bir_saniye = 1000;
//https://maxpromer.github.io/LCD-Character-Creator/
byte CursorIcon[8] = {B10000, B01000, B00100, B00010, B00010, B00100, B01000, B10000}; // >
byte CheckIcon[8] = {B00000, B00000, B00001, B00011, B10110, B11100, B11000, B00000}; // ✓
byte CheckBoxIcon[8] = {B11111, B10000, B10001, B00011, B10110, B11101, B01001, B11111}; // 🗹
byte UncheckBoxIcon[8] = {B11111, B10001, B10001, B10001, B10001, B10001, B10001, B11111}; //☐
int menu = 0;
void setup() {
Serial.begin(9600);
// RTC_DS1302.setDS1302Time(00, 32, 23, 6, 24, 01, 2023); // İlk zaman ayarı
Wire.begin();
lcd.begin(16, 2);
lcd.backlight();
pinMode(encoderClk, INPUT_PULLUP);
pinMode(encoderDt, INPUT_PULLUP);
pinMode(encoderSw, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(encoderClk), rotate, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderSw), buttonPressed, FALLING);
TimeNow1 = millis(); //Start timer 1
lcd.setCursor(13, 0); // Defining positon to write from second row, first column .
lcd.print(RotateCounter);
lcd.createChar(0, CursorIcon);
lcd.setCursor(12, 0);
lcd.write(byte(0));
updateMenu();
}
void loop() {
unsigned long currentTime = millis();
if (currentTime - son_saniye > bir_saniye) {
//saatiGuncelle();
son_saniye = currentTime;
}
ButtonChecker();
lcd.setCursor(13, 0); // Defining positon to write from second row, first column .
lcd.print(RotateCounter);
if(menu!=RotateCounter){
menu = RotateCounter;
updateMenu();
}
// updateMenu();
}
void buttonPressed() {
//This timer is a "software debounce". It is not the most effective solution, but it works
TimeNow2 = millis();
if (TimeNow2 - TimeNow1 > 500) {
ButtonPressed = true;
}
TimeNow1 = millis(); //"reset" timer; the next 500 ms is counted from this moment
}
void rotate() {
CLKNow = digitalRead(encoderClk); //Read the state of the CLK pin
// If last and current state of CLK are different, then a pulse occurred
if (CLKNow != CLKPrevious && CLKNow == 1) {
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so increase
if (digitalRead(encoderDt) != CLKNow) {
RotateCounter++;
if (RotateCounter > rotateMenuSayac) {
RotateCounter = 0;
}
} else {
RotateCounter--;
if (RotateCounter < 0) {
RotateCounter = rotateMenuSayac;
}
}
}
CLKPrevious = CLKNow; // Store last CLK state
rotated = true;
}
/*
void saatiGuncelle() {
RTC_DS1302.updateTime(); //RTC den zaman bilgisini oku
// Serial.print("Tarih: ");
// Serial.print(RTC_DS1302.dayofmonth);
// Serial.print("/");
// Serial.print(RTC_DS1302.month);
// Serial.print("/");
// Serial.print(RTC_DS1302.year);
// Serial.print(" Saat: ");
// Serial.print(RTC_DS1302.hours);
// Serial.print(":");
// Serial.print(RTC_DS1302.minutes);
// Serial.print(":");
// Serial.println(RTC_DS1302.seconds);
saat = RTC_DS1302.hours;
dakika = RTC_DS1302.minutes;
saniye = RTC_DS1302.seconds;
saatiEkranaYazdir(saat, dakika, saniye);
}
void saatiEkranaYazdir(int saat, int dakika, int saniye) {
lcd.setCursor(0, 0);
lcd.print(saat, DEC);
lcd.print(":");
lcd.print(dakika, DEC);
lcd.print(":");
lcd.print(saniye, DEC);
lcd.setCursor(0, 1);
}
*/
void ButtonChecker() //this is basically the menu part. keep track of the buttonpressed and rotatecounter for navigation
{
if (ButtonPressed == true) {
Serial.print("RotateCounter: ");
Serial.println(RotateCounter);
lcd.setCursor(13, 0); // Defining positon to write from second row, first column .
lcd.print(RotateCounter);
executeAction();
//updateMenu();
}
ButtonPressed = false; //reset this variable
}
void updateMenu() {
switch (menu) {
case 0:
menu = 0;
lcd.clear();
lcd.print("Saat");
break;
case 1:
lcd.clear();
lcd.print(">Aile Saati");
lcd.setCursor(0, 1);
lcd.print(" Sure");
break;
case 2:
lcd.clear();
lcd.print(" Aile Saati");
lcd.setCursor(0, 1);
lcd.print(">Sure");
break;
case 3:
lcd.clear();
lcd.print(">Kapak Ac");
lcd.setCursor(0, 1);
lcd.print(" Baslat");
break;
case 4:
lcd.clear();
lcd.print(" Kapak Ac");
lcd.setCursor(0, 1);
lcd.print(">Baslat");
break;
case 5:
menu = rotateMenuSayac;
break;
}
}
void executeAction() {
switch (menu) {
case 1:
action1();
break;
case 2:
action2();
break;
case 3:
action3();
break;
case 4:
action4();
break;
}
}
void action1() {
rotateMenuSayac=11;
menu=11;
lcd.clear();
lcd.print("Baslangic Zamani");
lcd.setCursor(0, 1);
lcd.print("00:00");
delay(1500);
}
void action2() {
lcd.clear();
lcd.print(">Executing #2");
delay(1500);
}
void action3() {
lcd.clear();
lcd.print(">Executing #3");
delay(1500);
}
void action4() {
lcd.clear();
lcd.print(">Kapak Kapaniyor");
RotateCounter=0;
delay(1500);
}