#include <Wire.h>
#include <TEA5767Radio.h>
#include <LiquidCrystal_I2C.h>
#include <MD_REncoder.h>
TEA5767Radio radio = TEA5767Radio();
LiquidCrystal_I2C lcd(0x27, 20, 4);
MD_REncoder R = MD_REncoder(2, 3);
const int REsw = 4;
int level;
int freq;
int numberfreq = 8700;
float num;
#define SCALE_CLEAR0 0
#define SCALE_CLEAR1 1
#define SCALE_CLEAR2 2
#define SCALE_CLEAR3 3
#define SCALE_CLEAR4 4
#define SCALE_CLEAR5 5
int list;
int station[6] = {8750, 8925, 9575, 9800, 10500, 9525};
//=======================================================//
void setup()
{
//Serial.begin(9600);
Wire.begin();
pinMode(REsw, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.clear();
freq = numberfreq;
lcd.setCursor(1, 1);
lcd.print("FM: ");
/*lcd.setCursor(6, 1);
lcd.print(freq * 0.01);
lcd.print(" MHz ");*/
R.begin();
radio.setFrequency(freq * 0.01);
byte scaleChar0[8] = {
0b10101,
0b10101,
0b10100,
0b10000,
0b10000,
0b10000,
0b10000,
0b00000
};
lcd.createChar(SCALE_CLEAR0, scaleChar0);
byte scaleChar1[8] = {
0b11101,
0b11101,
0b01100,
0b01000,
0b01000,
0b01000,
0b01000,
0b00000
};
lcd.createChar(SCALE_CLEAR1, scaleChar1);
byte scaleChar2[8] = {
0b10101,
0b10101,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b00000
};
lcd.createChar(SCALE_CLEAR2, scaleChar2);
byte scaleChar3[8] = {
0b10111,
0b10111,
0b00110,
0b00010,
0b00010,
0b00010,
0b00010,
0b00000
};
lcd.createChar(SCALE_CLEAR3, scaleChar3);
byte scaleChar4[8] = {
0b10101,
0b10101,
0b00101,
0b00001,
0b00001,
0b00001,
0b00001,
0b00000
};
lcd.createChar(SCALE_CLEAR4, scaleChar4);
byte scaleChar5[8] = {
0b10101,
0b10101,
0b00100,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
lcd.createChar(SCALE_CLEAR5, scaleChar5);
displayfreq();
displaybackscale();
displaySCALE();
/*lcd.setCursor(0, 0);
for (byte i = 0; i < 16; i ++) {
lcd.write(SCALE_CLEAR5);
}*/
}
//=======================================================//
void displaybackscale() {
lcd.setCursor(0, 0);
for (byte i = 0; i < 16; i ++) {
lcd.write(SCALE_CLEAR5);
}
}
//=======================================================//
void loop()
{
if (digitalRead(REsw) == LOW) {
list += 1;
if (list > 5)
list = 0;
delay(250);
numberfreq = station[list];
freq = numberfreq;
num = (((numberfreq - 8750) / 25) * 10) / 5;
int numlevel = num;
level = (numlevel % 10) / 2;
radio.setFrequency(freq * 0.01);
/*lcd.setCursor(5, 1);
if (numberfreq < 10000)
lcd.print(" ");
lcd.print(freq * 0.01);
lcd.print(" MHz ");*/
//Serial.println(freq);
displayfreq();
displaybackscale();
displaySCALE();
}
uint8_t x = R.read();
if (x)
{
if (x == DIR_CW ) {
numberfreq += 25;
if (numberfreq > 10800)
numberfreq = 10800;
}
else {
numberfreq -= 25;
if (numberfreq < 8700)
numberfreq = 8700;
}
num = (((numberfreq - 8750) / 25) * 10) / 5;
int numlevel = num;
level = (numlevel % 10) / 2;
freq = numberfreq;
radio.setFrequency(freq * 0.01);
/*lcd.setCursor(5, 1);
if (numberfreq < 10000)
lcd.print(" ");
lcd.print(freq * 0.01);
lcd.print(" MHz ");*/
//Serial.println(freq);
displayfreq();
displaybackscale();
displaySCALE();
}
}
//=======================================================//
void displaySCALE() {
byte y = num * 0.1;
if (freq < 8750)
{
y = 0;
level = 0;
lcd.setCursor(y, 0);
lcd.write(level);
}
else if (freq > 10725)
{
y = 15;
level = 4;
lcd.setCursor(y, 0);
lcd.write(level);
}
else
{
lcd.setCursor(y, 0);
lcd.write(level);
}
}
//=======================================================//
void displayfreq() {
lcd.setCursor(5, 1);
if (numberfreq < 10000)
lcd.print(" ");
lcd.print(freq * 0.01);
lcd.print(" MHz ");
}
//=======================================================//