#include <SPI.h>
#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
#define SW_PIN 14
#define DT_PIN 13
#define CLK_PIN 12
int ROTARY_VALUE = 0;
int lastClk=0;
int newClk;
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(115200);
pinMode(SW_PIN, INPUT);
pinMode(DT_PIN, INPUT);
pinMode(CLK_PIN, INPUT);
// attachInterrupt(digitalPinToInterrupt(CLK_PIN), readEncoder, FALLING);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.display();
}
void readEncoder() {
int newClk = digitalRead(CLK_PIN);
if (newClk != lastClk) {
// There was a change on the CLK pin
lastClk = newClk;
ROTARY_VALUE++;
int dtValue = digitalRead(DT_PIN);
if (newClk == LOW && dtValue == HIGH) {
// ROTARY_VALUE++;
// Serial.println("Rotated clockwise ⏩");
}
if (newClk == LOW && dtValue == LOW) {
// Serial.println("Rotated counterclockwise ⏪");
// ROTARY_VALUE--;
}
}
else{
ROTARY_VALUE--;
}
}
void loop() {
readEncoder();
mainDisplay();
Serial.println(ROTARY_VALUE);
}
void mainDisplay(){
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println(F("AMSA"));
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 10);
display.println(F(ROTARY_VALUE));
display.display();
delay(100);
}