#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define encoderClk 8 //Pin 1 from rotary encoder
#define encoderDt 9 //Pin 2 from rotary encoder
#define encoderSw 3
int encoderPos = 0;
int encoderLast = 0;
//Vraiables for rotary encoder state detection
int clk_State;
int Last_State;
bool dt_State;
float set_temperature = 0.0;
float elapsedTime, Time, timePrev;
int menu_activated =1;
void setup() {
Serial.begin(9600);
Time = millis();
Last_State = (PINB & B00000001); //Detect first state of the encoder
PCICR |= (1 << PCIE0); //enable PCMSK0 scan
PCMSK0 |= (1 << PCINT0); //Set pin D8 trigger an interrupt on state change.
PCMSK0 |= (1 << PCINT1); //Set pin D9 trigger an interrupt on state change.
PCMSK0 |= (1 << PCINT3); //Set pin D11 trigger an interrupt on state change.
pinMode(11, INPUT);
pinMode(9, INPUT);
pinMode(8, INPUT);
lcd.init();
lcd.backlight();
}
void loop() {
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set I value ");
lcd.setCursor(0, 1);
lcd.print(set_temperature);
}
//The interruption vector for push button and rotary encoder
ISR(PCINT0_vect) {
if (menu_activated == 1)
{
clk_State = (PINB & B00000001); //pin 8 state? It is HIGH?
dt_State = (PINB & B00000010);
if (clk_State != Last_State) {
// If the data state is different to the clock state, that means the encoder is rotating clockwise
if (dt_State != clk_State) {
set_temperature = set_temperature + 1 ;
}
else {
set_temperature = set_temperature -1;
}
}
Last_State = clk_State; // Updates the previous state of the clock with the current state
}
//Push button was pressed!
if (PINB & B00001000) //Pin D11 is HIGH?
{
// button_pressed = 1;
Serial.print("Butona basıldı");
}
}