#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED display configuration
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//Rotary encoder configuration
#define ENCODER_CLK 15
#define ENCODER_DT 16
#define ENCODER_SW 17
double Kp = 2.0, Ki = 5.0, Kd = 1.0;
double counter = 0; // Initial setpoint
double counterLast = 0;
int currentPage = 0;
// Rotary encoder variables
volatile int encoderPos = 0;
int lastEncoderPos = 0;
bool buttonPressed = false;
//bool setpointConfirmed = false; // Track whether the setpoint has been confirmed
unsigned long lastButtonPressTime = 0; // For debouncing
//bool previousHeaterState = false; // Store previous heater state
void IRAM_ATTR readEncoderISR() {
// Check the direction of rotation by comparing CLK and DT
if (digitalRead(ENCODER_DT) != digitalRead(ENCODER_CLK)) {
encoderPos++; // Clockwise
} else {
encoderPos--; // Counterclockwise
}
}
void setup() {
Serial.begin(115200);
// Initialize OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Loop forever
}
display.clearDisplay();
display.display();
// Initialize encoder pins
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_SW, INPUT_PULLUP);
// Attach an interrupt to the CLK pin
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoderISR, CHANGE);
updateDisplay();
}
void loop() {
// Handle rotary encoder for setting setpoint
handleButtonPress();
rotation();
updateDisplay();
delay(200); // Small delay for smooth behavior
}
void rotation() {
if (encoderPos != lastEncoderPos) {
if (encoderPos > lastEncoderPos) {
counter++; // Increase setpoint
currentPage = 1;
} else {
counter--; // Decrease setpoint
currentPage = 0;
}
lastEncoderPos = encoderPos;
Serial.print("Counter: ");
Serial.println(counter); // Debugging output to monitor setpoint change
Serial.print("Page: ");
Serial.println(currentPage);
}
}
void handleButtonPress() {
static bool lastButtonState = HIGH;
bool currentButtonState = digitalRead(ENCODER_SW);
if (currentButtonState == LOW && lastButtonState == HIGH && (millis() - lastButtonPressTime > 100)) {
// Debounce and toggle setpoint change mode
lastButtonPressTime = millis();
buttonPressed = true;
counter = 0;
Serial.println("Button Pressed");
Serial.print("Counter set to:");
Serial.println(counter);
}
if (currentButtonState == HIGH && lastButtonState == LOW && (millis() - lastButtonPressTime > 100)) {
// Debounce and toggle setpoint change mode
lastButtonPressTime = millis();
buttonPressed = false;
Serial.println("Button Released");
}
lastButtonState = currentButtonState;
}
void updateDisplay() {
display.clearDisplay();
if(currentPage == 0) {
display.fillCircle(61,61, 2, WHITE);
display.drawCircle(68,61, 2, WHITE);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("PAGE-1");
}
if(currentPage == 1) {
display.drawCircle(61,61, 2, WHITE);
display.fillCircle(68,61, 2, WHITE);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(30, 0);
display.print("PID-settings");
display.setCursor(0, 15);
display.print("Kp: ");
display.print(Kp);
display.setCursor(0, 27);
display.print("Ki: ");
display.print(Ki);
display.setCursor(0, 39);
display.print("Kd: ");
display.print(Kd);
}
display.display();
}