#define BLYNK_TEMPLATE_ID "TMPL64ZffXxdf"
#define BLYNK_TEMPLATE_NAME "Wokwi"
#define BLYNK_AUTH "3WfHOBhLmfucCULspYiUbpy98CY57g5i"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <BlynkSimpleEsp32.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define OLED_RESET -1
Adafruit_SSD1306 display(OLED_RESET);
#define POTENTIOMETER_PIN 36
#define ENCODER_CLK_PIN 34
#define ENCODER_DT_PIN 35
BlynkTimer timer;
int potValue = 0;
int encoderPos = 0;
int encoderPinALast = LOW;
void readPotentiometer()
{
int newValue = analogRead(POTENTIOMETER_PIN);
if (newValue != potValue)
{
potValue = newValue;
Blynk.virtualWrite(V1, potValue);
}
}
void checkEncoder()
{
int encoderPinA = digitalRead(ENCODER_CLK_PIN);
if ((encoderPinALast == LOW) && (encoderPinA == HIGH))
{
if (digitalRead(ENCODER_DT_PIN) == LOW)
{
encoderPos--;
}
else
{
encoderPos++;
}
Blynk.virtualWrite(V2, encoderPos);
}
encoderPinALast = encoderPinA;
}
BLYNK_CONNECTED() {
Blynk.syncAll();
}
void setup()
{
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH, WIFI_SSID, WIFI_PASSWORD);
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
pinMode(POTENTIOMETER_PIN, INPUT);
pinMode(ENCODER_CLK_PIN, INPUT_PULLUP);
pinMode(ENCODER_DT_PIN, INPUT_PULLUP);
timer.setInterval(100L, readPotentiometer);
timer.setInterval(100L, checkEncoder);
}
void loop()
{
Blynk.run();
timer.run();
}