//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <LiquidCrystal_I2C.h>
#include <MD_REncoder.h>

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
MD_REncoder R = MD_REncoder(2, 3);

int count;
int level;
int freq;
int numberfreq = 8700;

#define SCALE_CLEAR0   0
#define SCALE_CLEAR1   1
#define SCALE_CLEAR2   2
#define SCALE_CLEAR3   3
#define SCALE_CLEAR4   4
#define SCALE_CLEAR5   5

void setup()
{
  Serial.begin(9600);
  lcd.init(); 
  lcd.backlight();

  freq = numberfreq;
  lcd.setCursor(3,0);
  lcd.print(freq * 0.01);
  lcd.print(" MHz ");

  R.begin();
  
  byte scaleChar0[8] = {
    0b10000,
    0b10000,
    0b10000,
    0b10000,
    0b10000,
    0b10100,
    0b10101,
    0b10101
  };
lcd.createChar(SCALE_CLEAR0, scaleChar0);

byte scaleChar1[8] = {
    0b01000,
    0b01000,
    0b01000,
    0b01000,
    0b01000,
    0b01100,
    0b11101,
    0b11101
  };
lcd.createChar(SCALE_CLEAR1, scaleChar1);

byte scaleChar2[8] = {
    0b00100,
    0b00100,
    0b00100,
    0b00100,
    0b00100,
    0b00100,
    0b10101,
    0b10101
  };
lcd.createChar(SCALE_CLEAR2, scaleChar2);

byte scaleChar3[8] = {
    0b00010,
    0b00010,
    0b00010,
    0b00010,
    0b00010,
    0b00110,
    0b10111,
    0b10111
  };
lcd.createChar(SCALE_CLEAR3, scaleChar3);

byte scaleChar4[8] = {
    0b00001,
    0b00001,
    0b00001,
    0b00001,
    0b00001,
    0b00101,
    0b10101,
    0b10101
  };
lcd.createChar(SCALE_CLEAR4, scaleChar4);

byte scaleChar5[8] = {
    0b00000,
    0b00000,
    0b00000,
    0b00000,
    0b00000,
    0b00100,
    0b10101,
    0b10101
  };
  lcd.createChar(SCALE_CLEAR5, scaleChar5);

  lcd.setCursor(0, 1);
  for (byte i= 0; i < 16; i ++) {  
      lcd.write(SCALE_CLEAR5);
  }  
 displaySCALE();
}


void loop()
{
  uint8_t x = R.read();
  
  if (x) 
  {
    if (x == DIR_CW ){
      numberfreq += 25;
      
      if (numberfreq > 8750){
          count++;
          level++;
         if (count > 79)
          count = 79;
         if (level > 4)
          level = 0;
         }
      
      if (numberfreq > 10800)
          numberfreq = 10800;     
      
    }

     else {
      numberfreq -= 25;

      if (numberfreq < 10725){ 
        count--;
        level--;    
      if (count < 0)
         count = 0;
      if (level < 0)
         level = 4;
          }
     if (numberfreq < 8700)
         numberfreq = 8700;
     }
    
    freq = numberfreq;
    lcd.setCursor(3,0);
    lcd.print(freq*0.01);
    lcd.print(" MHz ");

    Serial.println(freq);
    Serial.println(count);
    Serial.println(level);
    delay(100);

    displaySCALE();

  }    
}


void displaySCALE(){

byte y = count/5; 

    if (freq <= 8750)
     {
      y = 0;
      level = 0;
      lcd.setCursor(y, 1);
      lcd.write(level);
     } 
     else if (freq >= 10725)
     {
      y = 15;
      level = 4;
      lcd.setCursor(y, 1);
      lcd.write(level); 
     }

     else 
     {
      lcd.setCursor((y-1), 1);
      lcd.write(SCALE_CLEAR5);
      lcd.write(level); 
      lcd.write(SCALE_CLEAR5); 
     }
  
}