/*
Rotary Encoder volume control example
Usage: rotate the knob to control the value,
click the knob to change between volume/bass/treble.
https://wokwi.com/arduino/projects/304919215794553409
Released under the MIT license.
Copyright (C) 2021, Uri Shaked.
*/
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSansBold9pt7b.h>
#include <Fonts/Picopixel.h>
#define ENCODER_CLK 2
#define ENCODER_DT 3
#define ENCODER_SW 4
uint8_t volume = 0;
uint8_t bass = 0;
uint8_t treble = 0;
typedef enum {
SET_VOLUME,
SET_BASS,
SET_TREBLE,
} Mode;
Mode mode = SET_VOLUME;
Adafruit_SSD1306 display(128, 64, &Wire, -1);
int frequncy = 87;
int numfreq;
int delta_scroll;
int value = 870;
void setup()
{
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_SW, INPUT_PULLUP);
updateDisplay();
}
long int modeLastChanged = 0;
int prevClk = LOW;
void loop()
{
if (digitalRead(ENCODER_SW) == LOW && millis() - modeLastChanged > 300)
{
modeLastChanged = millis();
nextMode();
updateDisplay();
}
int clk = digitalRead(ENCODER_CLK);
if (clk != prevClk && clk == 1)
{
int dt = digitalRead(ENCODER_DT);
int delta;
if (dt != clk)
{
delta += 4;
delta_scroll += 4;
value += 1;
if (delta_scroll > 20)
delta_scroll = 4;
} else {
delta -= 4;
delta_scroll -= 4;
value -= 1;
if (delta_scroll < -20)
delta_scroll = -4;
}
//Serial.println(delta);
Serial.println(delta_scroll);
Serial.println(numfreq);
updateValue(delta);
updateDisplay();
}
prevClk = clk;
}
void nextMode()
{
switch (mode)
{
case SET_VOLUME:
mode = SET_BASS;
break;
case SET_BASS:
mode = SET_TREBLE;
break;
case SET_TREBLE:
mode = SET_VOLUME;
break;
}
}
void updateValue(int delta) {
switch (mode) {
case SET_VOLUME:
volume = volume + delta;
break;
case SET_BASS:
bass = constrain(bass + delta, 0, 100);
break;
case SET_TREBLE:
treble = constrain(treble + delta, 0, 100);
break;
}
}
void updateDisplay()
{
display.clearDisplay();
display.setFont();
display.setTextColor(1);
switch (mode)
{
case SET_VOLUME:
volume_display();
break;
case SET_BASS:
bass_display();
break;
case SET_TREBLE:
treble_display();
break;
}
}
void volume_display()
{
display.setCursor(5, 2);
display.print("FM Radio");
display.setCursor(65, 2);
display.print((value+1)*0.1);
display.drawRoundRect(0, 15, 128, 42, 2, WHITE);
display.drawLine(60, 55, 60, 15, WHITE);
//********************************************************************
int temp = value -10;
for (int i = 0; i < 22; i++)
{
if ((temp % 10) == 0)
{
display.drawLine(i * 5, 40, i * 5, 50, WHITE);
display.drawLine((i * 5) + 5, 30, (i * 5) + 5, 50, WHITE);
display.setCursor(i * 5, 20);
display.print(temp/10);
}
else if ((temp % 5) == 0 && (temp % 10) != 0)
{
display.drawLine(i * 5, 40, i * 5, 50, WHITE);
display.drawLine((i * 5) + 5, 30, (i * 5) + 5, 50, WHITE);
}
else
{
display.drawLine(i * 5, 40, i * 5, 50, WHITE);
}
temp = temp + 1;
}
//********************************************************************
//display.setCursor(numfreq+20 ,20);
//display.print(frequncy);
display.display();
}
void bass_display()
{
display.setCursor(48, 22);
display.print("Bass");
display.drawRoundRect(10, 32, 102, 9, 2, WHITE);
display.fillRect(11, 33, bass, 7, WHITE);
if (mode == SET_BASS)
{
display.setCursor(38, 22);
display.print(">");
}
display.display();
}
void treble_display()
{
display.setCursor(42, 42);
display.print("Treble");
display.drawRoundRect(10, 52, 102, 9, 2, WHITE);
display.fillRect(11, 53, treble, 7, WHITE);
if (mode == SET_TREBLE)
{
display.setCursor(32, 42);
display.print(">");
}
display.display();
}