#include <Adafruit_SSD1306.h>
#include <Encoder.h>
// RotaryEncoder pins
#define CLK_PIN 5
#define DT_PIN 4
#define SW_PIN 3
// OLED display object
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// RotaryEncoder object
Encoder encoder(CLK_PIN, DT_PIN);
// Variables
float current_temp = 0.0;
float set_temp = 70.0;
int hysteresis = 5;
void setup() {
Serial.begin(9600);
// Initialize display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
// Initialize encoder
encoder.write(0);
}
void loop() {
// Read temperature sensor
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
int analogValue = analogRead(A0);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
// Handle encoder rotation
int16_t encoder_pos = encoder.read();
if (encoder_pos > 0) {
set_temp++;
encoder.write(0);
} else if (encoder_pos < 0) {
set_temp--;
encoder.write(0);
}
// Display temperatures on OLED
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print("Set:");
display.print(set_temp);
display.setTextSize(1);
display.setCursor(0,20);
display.print("Current:");
display.print(current_temp);
display.display();
}