#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RotaryEncoder.h>

#define OLED_RESET 4

#define PIN_IN1 2
#define PIN_IN2 3

float freq = 101.5;
int vol = 10;
int rssi = 54;
char buffer[6];
char buffer2[6];
char buffer3[6];

Adafruit_SSD1306 oled(128, 64);
RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);

void setup()
{
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  delay(200);
  oled.clearDisplay();
  updateLCD();
}

void loop()
{
  readEncoder();
}

void updateLCD()
{
  oled.setTextColor(SSD1306_WHITE, SSD1306_BLACK);

  if (freq > 100.0) {
    oled.setCursor(0, 10);
    oled.clearDisplay();
  }
  else {
    oled.setCursor(18, 10);
    oled.clearDisplay();
  }

  oled.drawRect(0, 1, 127, 2, SSD1306_WHITE);

  dtostrf(freq, 2, 1, buffer);
  oled.setTextSize(3);
  oled.print(buffer);

  oled.drawRect(0, 38, 127, 2, SSD1306_WHITE);

  oled.setCursor(97, 10);
  oled.setTextSize(2);
  oled.print("FM");

  oled.setCursor(92, 25);
  oled.setTextSize(1);
  oled.print("STEREO");

  sprintf(buffer2, "Level:%3d", rssi);
  oled.setCursor(4, 48);
  oled.print(buffer2);

  sprintf(buffer3, "Vol:%2d", vol);
  oled.setCursor(80, 48);

  oled.print(buffer3);
  oled.write(37);

  oled.fillRect(0, 45, 127, 14, SSD1306_INVERSE);

  oled.display();
}

void readEncoder() {

  static int pos = 0;
  encoder.tick();

  int newPos = encoder.getPosition();
  if (pos != newPos) {

    if (newPos > pos)
      freq = freq - 0.1;
    if (newPos < pos)
      freq = freq + 0.1;

    pos = newPos;

    if (freq > 108.0)freq = 108.0;
    if (freq < 87.0)freq = 87.0;

    updateLCD();
  }
}
Loading
ssd1306