#include <RotaryEncoder.h>
#include <LiquidCrystal.h>
// initialise the rotary encoder
#define PIN_IN1 8
#define PIN_IN2 9
RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);
// initialise the LCD
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int frequency = 0;
int volume = 0;
bool editMode = false;
bool frequencyEdit = true;
const int buttonPin = 10;
void printDisplay(int frequency, int volume);
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
frequency = 875;
volume = 0;
printDisplay(frequency, volume);
}
void loop()
{
static int pos = 0;
// Check if the rotary encoder has been turned
encoder.tick();
int newPos = encoder.getPosition();
if (pos != newPos)
{
// Update the LCD with the current frequency and volume
printDisplay(frequency, volume);
if (true){
lcd.setCursor(15,0);
lcd.blink();
int newFrequency = frequency + int(encoder.getDirection());
frequency = constrain(newFrequency, 875, 1080);
}
}
pos = newPos;
/*
if (editMode)
{
if (frequencyEdit)
{
lcd.setCursor(15,0);
lcd.blink();
int newFrequency = frequency + int(encoder.getDirection());
frequency = constrain(newFrequency, 875, 1080);
}
else
{
lcd.setCursor(15,1);
lcd.blink();
int newVolume = volume + int(encoder.getDirection());
volume = constrain(newVolume, 0, 100);
}
pos = newPos;
Serial.print("frequency:");
Serial.print(frequency);
Serial.print("position:");
Serial.println((int)(newPos));
Serial.print("edit mode?: ");
Serial.println(editMode ? "ON" : "OFF");
}
else
{
lcd.noBlink();
} */
}
/*
// Check if the button has been pressed
if (digitalRead(buttonPin) == LOW)
{
// If the button is pressed, toggle the edit mode
editMode = !editMode;
// Wait for the button to be released before continuing
while (digitalRead(buttonPin) == LOW)
{
delay(10);
}
if(!editMode)
{
frequencyEdit = !frequencyEdit;
}
} */
void printDisplay(int frequency, int volume)
{
lcd.setCursor(0, 0);
lcd.print("FM: "); lcd.print(frequency / 10.0); lcd.print(" MHz");
lcd.setCursor(0, 1);
lcd.print("Vol: "); lcd.print(volume); lcd.print(" %");
}