/*
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 BLDC 10
#define BLDC2 11
#define ENCODER_CLK 8
#define ENCODER_DT 9
#define ENCODER_SW 4
static const unsigned char PROGMEM logo_bmp[] =
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00,
0x00, 0x18, 0x00, 0x7f, 0x8c, 0x3f, 0xfc, 0x3f, 0x98, 0x00, 0x01, 0x8c, 0x00, 0x00, 0x01, 0x9e,
0x00, 0x01, 0x8c, 0x0c, 0x30, 0x01, 0x98, 0x18, 0x01, 0x8c, 0x0c, 0x30, 0x3f, 0x98, 0x18, 0x03,
0x0c, 0x0c, 0x30, 0x30, 0x1e, 0x00, 0x07, 0x0c, 0x0c, 0x30, 0x30, 0x18, 0x00, 0x0e, 0x0c, 0x3f,
0xfc, 0x3f, 0xd8, 0x00, 0x1c, 0x0c, 0x01, 0x80, 0x00, 0x18, 0x00, 0x78, 0x0c, 0x01, 0x80, 0x07,
0xf0, 0x18, 0x20, 0x0c, 0x01, 0x80, 0x0c, 0x18, 0x18, 0x00, 0x0c, 0x7f, 0xfe, 0x18, 0x18, 0x00,
0x00, 0x0c, 0x00, 0x00, 0x0c, 0x18, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x07, 0xf0, 0x00, 0x00, 0x0c,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00};
uint8_t volume = 0;
uint8_t bass = 25;
uint8_t treble = 66;
typedef enum {
SET_VOLUME,
SET_BASS,
SET_TREBLE,
} Mode;
Mode mode = SET_VOLUME;
Adafruit_SSD1306 display(128, 64, &Wire, -1);
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 = constrain(volume + delta, 0, 58);
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);
display.setCursor(1, 39);
display.drawBitmap (1, 32 ,logo_bmp,56 ,21 , WHITE) ;
display.drawRoundRect(59, 35, 60, 15, 1, WHITE);
display.fillRect(60, 36, volume, 13, WHITE);
if (mode == SET_VOLUME) {
display.setCursor(32, 2);
display.print(">");
}
//display.setCursor(48, 22);
//display.print("Bass");
//display.drawRoundRect(10, 32, 102, 15, 1, WHITE);
//display.fillRect(11, 33, bass, 7, WHITE);
// if (mode == SET_BASS) {
// display.setCursor(38, 22);
// display.print(">");
//}
//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();
}
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
Serial.begin(9600);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_SW, INPUT_PULLUP);
pinMode(BLDC, OUTPUT);
updateDisplay();
}
long int modeLastChanged = 0;
int prevClk = HIGH;
void loop() {
//if (digitalRead(ENCODER_SW) == LOW && millis() - modeLastChanged > 300) {
// modeLastChanged = millis();
// nextMode();
// updateDisplay();
// }
int clk = digitalRead(8);
if (clk != prevClk && clk == LOW) {
int dt = digitalRead(9);
int delta = dt == HIGH ? 10 : -10;
updateValue(delta);
updateDisplay();
}
prevClk = clk;
int val_a =
int val_b = digitalRead(9);
int count = 0;
int prev_a_status = LOW;
if ( ( val_a == LOW ) && (prev_a_status == HIGH ) )
{
if (val_b == HIGH)
{
count=count+10;
}
else
{
count=count-10;
}
if(count <= 0){count = 0;}
if(count >= 255){count = 255;}
if(count >= 0){
analogWrite(BLDC, count);
analogWrite(BLDC2, 0);
}else{
analogWrite(BLDC, 0);
analogWrite(BLDC2, count);
}
prev_a_status = val_a;
Serial.print(count);
}}