#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SCREEN_I2C_ADDRESS 0x3C
#define OLED_RESET_PIN -1
#define CLK 2
#define DT 3
#define SW 4
Adafruit_SSD1306 screen(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET_PIN);
int potenial_meter_value;
int old_potenial_value = 0;
int new_potenial_value = 0;
int rotary_encoder_baar = 0;
int currentStateCLK;
int lastStateCLK;
void lcd_data(){
screen.clearDisplay();
screen.setCursor(0,0);
screen.print("Potenial Meter ");
screen.drawRect(0,16,128,15,WHITE);
screen.fillRect(0,16,old_potenial_value,15,WHITE);
screen.setCursor(0,32);
screen.print("Rotary Encoder ");
screen.drawRect(0,48,128,15,WHITE);
screen.fillRect(0,48,rotary_encoder_baar,15,WHITE);
screen.display();
}
void setup() {
Serial.begin(115200);
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW, INPUT_PULLUP);
lastStateCLK = digitalRead(CLK);
rotary_encoder_baar = 0;
// put your setup code here, to run once:
screen.begin(SSD1306_SWITCHCAPVCC, SCREEN_I2C_ADDRESS);
pinMode(A0, INPUT);
screen.setTextSize(1);
screen.setTextColor(WHITE);
lcd_data();
}
void loop() {
currentStateCLK = digitalRead(CLK);
potenial_meter_value = analogRead(A0);
old_potenial_value = potenial_meter_value /7.99;
if(new_potenial_value != old_potenial_value){
lcd_data();
new_potenial_value = old_potenial_value;
}
// if(potenial_meter_value == potenial_meter_value + 8)
// delay(100);
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
if (digitalRead(DT) != currentStateCLK) {
rotary_encoder_baar = rotary_encoder_baar - 16;
if(rotary_encoder_baar == -16){
rotary_encoder_baar = 0;
}
}else{
rotary_encoder_baar = rotary_encoder_baar + 16;
if(rotary_encoder_baar == 144){
rotary_encoder_baar = 128;
}
}
lcd_data();
Serial.println(rotary_encoder_baar);
}
lastStateCLK = currentStateCLK;
// put your main code here, to run repeatedly:
}