#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27,20,4); //LiquidCrystal_I2C lcd(0x20,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display or 20 chars and 4 line display
#define right digitalRead(7)
#define enter digitalRead(6)
#define left digitalRead(5)
#define led1 8
#define led2 9
#define led3 10
#define led4 11
int menuCounter = 0;
int unitLimiter = 0;
int buttonState = 0;
int enterState = 0;
int choiceCounter = 0;
int choiceValue = 0;
float value0 = 0;
float value1 = 0;
float value2 = 0;
float value3 = 0;
float value4 = 0;
float save1;
char myChar = 62;
bool isLock;
bool isSet;
String ch1 = "YES";
String ch2 = "NO";
float freqLimitUp = 108;
float freqLimitDown = 80;
float freq = 80;
float f_freq;
byte *ptr_val0;
void setup() {
lcd.init();
lcd.backlight();
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}
void def(){
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
void state0(){
if(enter == LOW && menuCounter == 0){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Frequency Setup");
lcd.setCursor(17,2);
lcd.print("MHZ");
enterState = 3;
buttonState = 2;
while(enterState == 3){
if(right == LOW && menuCounter == 0){
if(freq >= 108.0){
freq = freqLimitDown;
}
else{
freq = freq + 0.1;
}
}
if(left == LOW && menuCounter == 0){
if(freq <= 80.0){
freq = freqLimitUp;
}
else{
freq = freq - 0.1;
}
}
lcd.setCursor(0,2);
lcd.print(freq, 1);
delay(150);
if(enter == LOW && enterState == 3){
enterState = 4;
buttonState = 3;
while(enterState == 4){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Save the Value ?");
lcd.setCursor(2,3);
lcd.print("YES");
lcd.setCursor(16,3);
lcd.print("NO");
delay(150);
switch(choiceCounter){
case 0:
lcd.setCursor(0,3);
lcd.print(myChar);
delay(250);
break;
case 1:
lcd.setCursor(14,3);
lcd.print(myChar);
delay(250);
break;
}
if(right == LOW && menuCounter == 0){
choiceCounter++;
if(choiceCounter > 1){
choiceCounter = 0;
}
}
if(left == LOW && menuCounter == 0){
choiceCounter--;
if(choiceCounter < 0){
choiceCounter = 1;
}
}
if(enter == LOW && enterState == 4 && choiceCounter == 0){
enterState = 5;
buttonState = 0;
while(enterState == 5){
f_freq = freq;
return menu();
}
}
if(enter == LOW && enterState == 4 && choiceCounter == 1){
enterState = 5;
buttonState = 0;
while(enterState == 5){
return menu();
}
}
}
}
}
}
}
void freqs(){
lcd.clear();
lcd.setCursor(2,1);
lcd.print("Frequency Setup");
lcd.setCursor(5,3);
ptr_val0 = (byte *)&f_freq;
EEPROM.write(1, *ptr_val0++);
EEPROM.write(2, *ptr_val0++);
EEPROM.write(3, *ptr_val0++);
EEPROM.write(4, *ptr_val0);
ptr_val0 = (byte *)&value0;
*ptr_val0++ = EEPROM.read(1);
*ptr_val0++ = EEPROM.read(2);
*ptr_val0++ = EEPROM.read(3);
*ptr_val0 = EEPROM.read(4);
lcd.print(value0);
lcd.setCursor(12,3);
lcd.print("MHZ");
delay(150);
if(value0 > 87.60 && value0 <87.80){
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
else if(value0 > (float)86.99 && value0 <87.01){
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
}
else if(value0 > (float)89.99 && value0 <90.01){
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
}
else if(value0 > (float)99.99 && value0 <100.01){
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
}
else{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
}
void state1(){
if(enter == LOW && menuCounter == 1){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Frequency Setup");
lcd.setCursor(17,2);
lcd.print("MHZ");
enterState = 3;
buttonState = 2;
while(enterState == 3){
if(right == LOW && menuCounter == 1){
if(freq >= 108.0){
freq = freqLimitDown;
}
else{
freq = freq + 0.1;
}
}
if(left == LOW && menuCounter == 1){
if(freq <= 80.0){
freq = freqLimitUp;
}
else{
freq = freq - 0.1;
}
}
lcd.setCursor(0,2);
lcd.print(freq, 1);
delay(150);
if(enter == LOW && enterState == 3){
enterState = 4;
buttonState = 3;
while(enterState == 4){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Save the Value ?");
lcd.setCursor(2,3);
lcd.print("YES");
lcd.setCursor(16,3);
lcd.print("NO");
delay(150);
switch(choiceCounter){
case 0:
lcd.setCursor(0,3);
lcd.print(myChar);
delay(250);
break;
case 1:
lcd.setCursor(14,3);
lcd.print(myChar);
delay(250);
break;
}
if(right == LOW && menuCounter == 1){
choiceCounter++;
if(choiceCounter > 1){
choiceCounter = 0;
}
}
if(left == LOW && menuCounter == 1){
choiceCounter--;
if(choiceCounter < 0){
choiceCounter = 1;
}
}
if(enter == LOW && enterState == 4 && choiceCounter == 0){
enterState = 5;
buttonState = 0;
while(enterState == 5){
value1 = freq;
return menu();
}
}
if(enter == LOW && enterState == 4 && choiceCounter == 1){
enterState = 5;
buttonState = 0;
while(enterState == 5){
return menu();
}
}
}
}
}
}
}
void state2(){
if(enter == LOW && menuCounter == 2){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Frequency Setup");
lcd.setCursor(17,2);
lcd.print("MHZ");
enterState = 3;
buttonState = 2;
while(enterState == 3){
if(right == LOW && menuCounter == 2){
if(freq >= 108.0){
freq = freqLimitDown;
}
else{
freq = freq + 0.1;
}
}
if(left == LOW && menuCounter == 2){
if(freq <= 80.0){
freq = freqLimitUp;
}
else{
freq = freq - 0.1;
}
}
lcd.setCursor(0,2);
lcd.print(freq, 1);
delay(150);
if(enter == LOW && enterState == 3){
enterState = 4;
buttonState = 3;
while(enterState == 4){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Save the Value ?");
lcd.setCursor(2,3);
lcd.print("YES");
lcd.setCursor(16,3);
lcd.print("NO");
delay(150);
switch(choiceCounter){
case 0:
lcd.setCursor(0,3);
lcd.print(myChar);
delay(250);
break;
case 1:
lcd.setCursor(14,3);
lcd.print(myChar);
delay(250);
break;
}
if(right == LOW && menuCounter == 2){
choiceCounter++;
if(choiceCounter > 1){
choiceCounter = 0;
}
}
if(left == LOW && menuCounter == 2){
choiceCounter--;
if(choiceCounter < 0){
choiceCounter = 1;
}
}
if(enter == LOW && enterState == 4 && choiceCounter == 0){
enterState = 5;
buttonState = 0;
while(enterState == 5){
value2 = freq;
return menu();
}
}
if(enter == LOW && enterState == 4 && choiceCounter == 1){
enterState = 5;
buttonState = 0;
while(enterState == 5){
return menu();
}
}
}
}
}
}
}
void state3(){
if(enter == LOW && menuCounter == 3){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Frequency Setup");
lcd.setCursor(17,2);
lcd.print("MHZ");
enterState = 3;
buttonState = 2;
while(enterState == 3){
if(right == LOW && menuCounter == 3){
if(freq >= 108.0){
freq = freqLimitDown;
}
else{
freq = freq + 0.1;
}
}
if(left == LOW && menuCounter == 3){
if(freq <= 80.0){
freq = freqLimitUp;
}
else{
freq = freq - 0.1;
}
}
lcd.setCursor(0,2);
lcd.print(freq, 1);
delay(150);
if(enter == LOW && enterState == 3){
enterState = 4;
buttonState = 3;
while(enterState == 4){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Save the Value ?");
lcd.setCursor(2,3);
lcd.print("YES");
lcd.setCursor(16,3);
lcd.print("NO");
delay(150);
switch(choiceCounter){
case 0:
lcd.setCursor(0,3);
lcd.print(myChar);
delay(250);
break;
case 1:
lcd.setCursor(14,3);
lcd.print(myChar);
delay(250);
break;
}
if(right == LOW && menuCounter == 3){
choiceCounter++;
if(choiceCounter > 1){
choiceCounter = 0;
}
}
if(left == LOW && menuCounter == 3){
choiceCounter--;
if(choiceCounter < 0){
choiceCounter = 1;
}
}
if(enter == LOW && enterState == 4 && choiceCounter == 0){
enterState = 5;
buttonState = 0;
while(enterState == 5){
value3 = freq;
return menu();
}
}
if(enter == LOW && enterState == 4 && choiceCounter == 1){
enterState = 5;
buttonState = 0;
while(enterState == 5){
return menu();
}
}
}
}
}
}
}
void state4(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Frequency Setup");
lcd.setCursor(17,2);
lcd.print("MHZ");
if(enter == LOW && menuCounter == 4){
enterState = 3;
buttonState = 2;
while(enterState == 3){
if(right == LOW && menuCounter == 4){
if(freq >= 108.0){
freq = freqLimitDown;
}
else{
freq = freq + 0.1;
}
}
if(left == LOW && menuCounter == 4){
if(freq <= 80.0){
freq = freqLimitUp;
}
else{
freq = freq - 0.1;
}
}
lcd.setCursor(0,2);
lcd.print(freq, 1);
delay(150);
if(enter == LOW && enterState == 3){
enterState = 4;
buttonState = 3;
while(enterState == 4){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Save the Value ?");
lcd.setCursor(2,3);
lcd.print("YES");
lcd.setCursor(16,3);
lcd.print("NO");
delay(150);
switch(choiceCounter){
case 0:
lcd.setCursor(0,3);
lcd.print(myChar);
delay(250);
break;
case 1:
lcd.setCursor(14,3);
lcd.print(myChar);
delay(250);
break;
}
if(right == LOW && menuCounter == 4){
choiceCounter++;
if(choiceCounter > 1){
choiceCounter = 0;
}
}
if(left == LOW && menuCounter == 4){
choiceCounter--;
if(choiceCounter < 0){
choiceCounter = 1;
}
}
if(enter == LOW && enterState == 4 && choiceCounter == 0){
enterState = 5;
buttonState = 0;
while(enterState == 5){
value4 = freq;
return menu();
}
}
if(enter == LOW && enterState == 4 && choiceCounter == 1){
enterState = 5;
buttonState = 0;
while(enterState == 5){
return menu();
}
}
}
}
}
}
}
void setting1(){
lcd.clear();
lcd.setCursor(5,1);
lcd.print("Setting 1");
lcd.setCursor(5,3);
lcd.print(value1);
lcd.setCursor(12,3);
lcd.print("MHZ");
delay(150);
}
void setting2(){
lcd.clear();
lcd.setCursor(5,1);
lcd.print("Setting 2");
lcd.setCursor(5,3);
lcd.print(value2);
lcd.setCursor(12,3);
lcd.print("MHZ");
delay(150);
}
void setting3(){
lcd.clear();
lcd.setCursor(5,1);
lcd.print("Setting 3");
lcd.setCursor(5,3);
lcd.print(value3);
lcd.setCursor(12,3);
lcd.print("MHZ");
delay(150);
}
void setting4(){
lcd.clear();
lcd.setCursor(5,1);
lcd.print("Setting 4");
lcd.setCursor(5,3);
lcd.print(value4);
lcd.setCursor(12,3);
lcd.print("MHZ");
delay(150);
}
void menu(){
switch(menuCounter){
case 0:
freqs();
break;
case 1:
setting1();
break;
case 2:
setting2();
break;
case 3:
setting3();
break;
case 4:
setting4();
break;
}
if(left == LOW && buttonState == 0){
menuCounter--;
if(menuCounter < 0){
menuCounter = 4;
}
}
if(right == LOW && buttonState == 0){
menuCounter++;
if(menuCounter > 4){
menuCounter = 0;
}
}
}
void loop(){
menu();
if(enter == LOW && menuCounter == 0){
enterState = 1;
buttonState = 1;
while(enterState == 1){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("It's Locked");
lcd.setCursor(0,1);
lcd.print("Unlock it ?");
lcd.setCursor(2,3);
lcd.print("YES");
lcd.setCursor(16,3);
lcd.print("NO");
delay(150);
switch(choiceCounter){
case 0:
lcd.setCursor(0,3);
lcd.print(myChar);
delay(250);
break;
case 1:
lcd.setCursor(14,3);
lcd.print(myChar);
delay(250);
break;
}
if(right == LOW && menuCounter == 0){
choiceCounter++;
if(choiceCounter > 1){
choiceCounter = 0;
}
}
if(left == LOW && menuCounter == 0){
choiceCounter--;
if(choiceCounter < 0){
choiceCounter = 1;
}
}
if(enter == LOW && enterState == 1 && choiceCounter == 0){
enterState = 2;
buttonState = 0;
while(enterState == 2){
choiceValue = choiceCounter;
return state0();
}
}
if(enter == LOW && enterState == 1 && choiceCounter == 1){
enterState = 2;
buttonState = 0;
while(enterState == 2){
return menu();
}
}
}
}
//
if(enter == LOW && menuCounter == 1){
enterState = 1;
buttonState = 1;
while(enterState == 1){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("It's Locked");
lcd.setCursor(0,1);
lcd.print("Unlock it ?");
lcd.setCursor(2,3);
lcd.print("YES");
lcd.setCursor(16,3);
lcd.print("NO");
delay(150);
switch(choiceCounter){
case 0:
lcd.setCursor(0,3);
lcd.print(myChar);
delay(250);
break;
case 1:
lcd.setCursor(14,3);
lcd.print(myChar);
delay(250);
break;
}
if(right == LOW && menuCounter == 1){
choiceCounter++;
if(choiceCounter > 1){
choiceCounter = 0;
}
}
if(left == LOW && menuCounter == 1){
choiceCounter--;
if(choiceCounter < 0){
choiceCounter = 1;
}
}
if(enter == LOW && enterState == 1 && choiceCounter == 0){
enterState = 2;
buttonState = 0;
while(enterState == 2){
choiceValue = choiceCounter;
return state1();
}
}
if(enter == LOW && enterState == 1 && choiceCounter == 1){
enterState = 2;
buttonState = 0;
while(enterState == 2){
return menu();
}
}
}
}
//
if(enter == LOW && menuCounter == 2){
enterState = 1;
buttonState = 1;
while(enterState == 1){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("It's Locked");
lcd.setCursor(0,1);
lcd.print("Unlock it ?");
lcd.setCursor(2,3);
lcd.print("YES");
lcd.setCursor(16,3);
lcd.print("NO");
delay(150);
switch(choiceCounter){
case 0:
lcd.setCursor(0,3);
lcd.print(myChar);
delay(250);
break;
case 1:
lcd.setCursor(14,3);
lcd.print(myChar);
delay(250);
break;
}
if(right == LOW && menuCounter == 2){
choiceCounter++;
if(choiceCounter > 1){
choiceCounter = 0;
}
}
if(left == LOW && menuCounter == 2){
choiceCounter--;
if(choiceCounter < 0){
choiceCounter = 1;
}
}
if(enter == LOW && enterState == 1 && choiceCounter == 0){
enterState = 2;
buttonState = 0;
while(enterState == 2){
choiceValue = choiceCounter;
return state2();
}
}
if(enter == LOW && enterState == 1 && choiceCounter == 1){
enterState =2;
buttonState = 0;
while(enterState == 2){
return menu();
}
}
}
}
//
if(enter == LOW && menuCounter == 3){
enterState = 1;
buttonState = 1;
while(enterState == 1){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("It's Locked");
lcd.setCursor(0,1);
lcd.print("Unlock it ?");
lcd.setCursor(2,3);
lcd.print("YES");
lcd.setCursor(16,3);
lcd.print("NO");
delay(150);
switch(choiceCounter){
case 0:
lcd.setCursor(0,3);
lcd.print(myChar);
delay(250);
break;
case 1:
lcd.setCursor(14,3);
lcd.print(myChar);
delay(250);
break;
}
if(right == LOW && menuCounter == 3){
choiceCounter++;
if(choiceCounter > 1){
choiceCounter = 0;
}
}
if(left == LOW && menuCounter == 3){
choiceCounter--;
if(choiceCounter < 0){
choiceCounter = 1;
}
}
if(enter == LOW && enterState == 1 && choiceCounter == 0){
enterState = 2;
buttonState = 0;
while(enterState == 2){
choiceValue = choiceCounter;
return state3();
}
}
if(enter == LOW && enterState == 1 && choiceCounter == 1){
enterState = 2;
buttonState = 0;
while(enterState == 2){
return menu();
}
}
}
}
//
if(enter == LOW && menuCounter == 4){
enterState = 1;
buttonState = 1;
while(enterState == 1){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("It's Locked");
lcd.setCursor(0,1);
lcd.print("Unlock it ?");
lcd.setCursor(2,3);
lcd.print("YES");
lcd.setCursor(16,3);
lcd.print("NO");
delay(150);
switch(choiceCounter){
case 0:
lcd.setCursor(0,3);
lcd.print(myChar);
delay(250);
break;
case 1:
lcd.setCursor(14,3);
lcd.print(myChar);
delay(250);
break;
}
if(right == LOW && menuCounter == 4){
choiceCounter++;
if(choiceCounter > 1){
choiceCounter = 0;
}
}
if(left == LOW && menuCounter == 4){
choiceCounter--;
if(choiceCounter < 0){
choiceCounter = 1;
}
}
if(enter == LOW && enterState == 1 && choiceCounter == 0){
enterState = 2;
buttonState = 0;
while(enterState == 2){
choiceValue = choiceCounter;
return state4();
}
}
if(enter == LOW && enterState == 1 && choiceCounter == 1){
enterState = 2;
buttonState = 0;
while(enterState == 2){
return menu();
}
}
}
}
}