#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define OLED_ADDR 0x3C
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define SSD1306_128_64
// #define SSD1306_128_32
// #define SSD1306_96_16
// #if (SSD1306_LCDHEIGHT != 64)
// #error("Height incorrect, please fix Adafruit_SSD1306.h!");
// #endif
/*
Code downoladed from https://www.hackster.io/yilmazyurdakul/arduino-oled-encoder-simple-menu-system-f9baa1
Original code have bug (little arrow changing continuously) was fixed and slightly improved ...
*/
#define encoder0PinA 2
#define encoder0PinB 3
volatile unsigned int encoder0Pos = 0;
int valA;
int valB;
int valC;
const byte encoder_switch = 4;
byte clk;
byte menuCount = 1;
byte dir = 0;
bool runState = false;
void setup() {
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(encoder_switch,INPUT_PULLUP);
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.display();
attachInterrupt(0, doEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2
}
void loop() {
if (digitalRead(encoder_switch) == LOW){clk = LOW;}
if (digitalRead(encoder_switch) == HIGH){clk = HIGH;}
Serial.println(clk);
ledControl();
menuCheck();
staticMenu();
display.clearDisplay();
delay(50);
}
void staticMenu() {
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(10, 10);
display.println("Value A:");
display.setCursor(60, 10);
display.println(valA);
display.setCursor(10, 20);
display.println("Value B:");
display.setCursor(60, 20);
display.println(valB);
display.setCursor(10, 30);
display.println("Value C:");
display.setCursor(60, 30);
display.println(valC);
display.setCursor(10, 40);
if (encoder0Pos > 4 && menuCount == 4) {
display.println("Push = Clear & OFF");
runState = true;
} else {
display.println("Turn right = ON");
runState = false;
}
display.setCursor(2, (menuCount * 10));
display.println(">");
display.display();
}
void ledControl() {
if (runState == true) {
analogWrite(6, valA);
analogWrite(9, valB);
analogWrite(10, valC);
}
}
void menuCheck() {
if (clk == LOW && menuCount < 5)
{
menuCount++;
encoder0Pos = 0;
}
if (clk == LOW && menuCount >= 5) {
valA =0, valB =0, valC =0;
analogWrite(6, valA);
analogWrite(9, valB);
analogWrite(10,valC);
menuCount = 1;
}
if (menuCount == 1) {
valA = encoder0Pos;
}
if (menuCount == 2) {
valB = encoder0Pos;
}
if (menuCount == 3) {
valC = encoder0Pos;
}
}
void doEncoder() {
if (digitalRead(encoder0PinA) == HIGH) {
if (digitalRead(encoder0PinB) == LOW && encoder0Pos > 0) {
encoder0Pos = encoder0Pos - 1;
// dir = 0;
}
else {
encoder0Pos = encoder0Pos + 1;
// dir = 1;
}
}
else
{
if (digitalRead(encoder0PinB) == LOW ) {
encoder0Pos = encoder0Pos + 1;
// dir = 1;
}
else {
if (encoder0Pos > 0) {
encoder0Pos = encoder0Pos - 1;
// dir = 0;
}
}
}
}