#include <Wire.h>
#include <TEA5767Radio.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
TEA5767Radio radio = TEA5767Radio();
#define CLK 6
#define DT 7
#define SW 5
bool precise = false;
float frequency = 76.0;
int currentStateCLK;
int lastStateCLK;
String currentDir ="";
unsigned long lastButtonPress = 0;
void setup() {
Wire.begin();
radio.setFrequency(frequency); // pick your own frequency
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
lastStateCLK = digitalRead(CLK);
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(3, 0);
lcd.print("Frekvencia");
lcd.setCursor(4, 1);
lcd.print(String(frequency) + " FM");
}
void loop() {
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
// If last and current state of CLK are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK) {
if(frequency < 108){
if(precise){
frequency = frequency + 0.1;
}
else{
frequency++;
}
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Frekvencia");
lcd.setCursor(4, 1);
lcd.print(String(frequency) + " FM");
radio.setFrequency(frequency);
}
} else {
if(frequency > 76){
if(precise){
frequency = frequency - 0.1;
}
else{
frequency--;
}
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Frekvencia");
lcd.setCursor(4, 1);
lcd.print(String(frequency) + " FM");
radio.setFrequency(frequency);
}
}
Serial.println(frequency);
}
// Remember last CLK state
lastStateCLK = currentStateCLK;
// Read the button state
int btnState = digitalRead(SW);
//If we detect LOW signal, button is pressed
if (btnState == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
Serial.println("Precise enabled!");
precise = !precise;
}
// Remember last button press event
lastButtonPress = millis();
}
}