#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const int pinA = 35;
const int pinB = 34;
const int pinButton = 32;
bool lastStateA;
bool lastButtonState = HIGH;
int currentMenu = -1;
const int numMenus = 6;
const String menu1 = "menu 1" ;
const String menu2 = "menu 2" ;
const String menu3 = "menu 3" ;
const String menu4 = "menu 4" ;
const String menu5 = "menu 5" ;
const String menu6 = "menu 6" ;
void encoderRead() {
bool currentStateA = digitalRead(pinA);
bool currentStateB = digitalRead(pinB);
if (currentStateB != currentStateA) {
currentMenu = (currentMenu + 1) % numMenus; //ke menu berikutnya
} else {
currentMenu = (currentMenu - 1 + numMenus) % numMenus; //ke menu sebelumnya
}
Serial.println(currentMenu);
}
void buttonClick() {
select(currentMenu);
}
void setup() {
Serial.begin(115200);
pinMode(pinA, INPUT);
pinMode(pinB, INPUT);
pinMode(pinButton, INPUT_PULLUP);
lastStateA = digitalRead(pinA);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
delay(500);
display.clearDisplay();
display.setTextColor(WHITE);
cover();
}
void loop() {
bool currentStateA = digitalRead(pinA);
bool currentStateB = digitalRead(pinB);
if (currentStateA > lastStateA) {
if (currentStateB != currentStateA) {
currentMenu = (currentMenu + 1) % numMenus; //ke menu berikutnya
} else {
currentMenu = (currentMenu - 1 + numMenus) % numMenus; //ke menu sebelumnya
}
menu(currentMenu);
Serial.println(currentMenu);
}
lastStateA = currentStateA;
bool buttonState = digitalRead(pinButton);
//Serial.println(buttonState);
if (buttonState < lastButtonState) {
select(currentMenu);
}
lastButtonState = buttonState;
}
void cover() {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(12, 3);
display.println("MENU LIST");
display.setTextSize(1);
display.setCursor(59, 30);
display.println("by");
display.setCursor(15, 40);
display.println("Alditya - 492752");
display.display();
}
void menu(int num) {
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(2);
display.println("Select:");
switch (num) {
case 0:
display.setTextSize(2);
display.println(menu1 + " <-");
display.setTextSize(1);
display.println(menu2);
display.setTextSize(1);
display.println(menu3);
break;
case 1:
display.setTextSize(1);
display.println(menu1);
display.setTextSize(2);
display.println(menu2 + " <-");
display.setTextSize(1);
display.println(menu3);
break;
case 2:
display.setTextSize(1);
display.println(menu1);
display.setTextSize(1);
display.println(menu2);
display.setTextSize(2);
display.println(menu3 + " <-");
break;
case 3:
display.setTextSize(2);
display.println(menu4 + " <-");
display.setTextSize(1);
display.println(menu5);
display.setTextSize(1);
display.println(menu6);
break;
case 4:
display.setTextSize(1);
display.println(menu4);
display.setTextSize(2);
display.println(menu5 + " <-");
display.setTextSize(1);
display.println(menu6);
break;
case 5:
display.setTextSize(1);
display.println(menu4);
display.setTextSize(1);
display.println(menu5);
display.setTextSize(2);
display.println(menu6 + " <-");
break;
default:
Serial.println("error");
display.println("error");
break;
}
display.display();
}
void select(int numSelect) {
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(2);
display.println("Selected:");
switch (numSelect) {
case 0:
display.println(menu1);
break;
case 1:
display.println(menu2);
break;
case 2:
display.println(menu3);
break;
case 3:
display.println(menu4);
break;
case 4:
display.println(menu5);
break;
case 5:
display.println(menu6);
break;
default:
display.clearDisplay();
Serial.println("error: no menu selected");
display.println("Please\nrotate\nthe knob!");
break;
}
display.display();
}