/*
Filament dryer for FDM 3D printer modified from ChatGPT 3.5
I design this during office time because office job is boring.
Replace LED with your heater/nichrome wire, put the heter infront of the 5v/12v PC fan.
Still working on the KY-040 that skip CW motion sometime.
Graphic editor for ESP32, Arduino, FlippersZero, etc.: https://lopaka.app/
*/
#include <DHT.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 OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Thermometer sign
static const unsigned char PROGMEM image_paint_0_bits[] = {0x1c,0x00,0x22,0x00,0x2b,0x00,0x2a,0x00,0x2b,0x00,0x2a,0x00,0x2b,0x00,0x2a,0x00,0x2a,0x00,0x49,0x00,0x9c,0x80,0xbe,0x80,0xbe,0x80,0x9c,0x80,0x41,0x00,0x3e,0x00};
#define DHTPIN 7 // Pin connected to DHT22 sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
#define HEATER_PIN 11 // Pin connected to heater. In this example, I using LED
#define CLK 2 // Pin connected to the KY-040
#define DT 3 // Pin connected to the KY-040
#define SW 4 // Pin connected to the KY-040
byte lastStateCLK;
String currentDir = "";
byte buttonPress = LOW;
float temperature = 30.00;
float humidity = 0.0;
//Set temperature
int temperature_SET = 40; // Initial temperature criteria
int previousTemp_SET = temperature_SET;
const int temp_MIN = 20; // Set minimum temperature
const int temp_MAX = 80; // Set maximum temperature
bool heaterState = false; // State of the heater
bool blink_sign = false; // State of the blinksign
unsigned long previousMillis = 0; // Store the last time temperature was read
const long interval = 1000; // Interval at which to read temperature (milliseconds)
void setup() {
Serial.begin(115200);
pinMode(HEATER_PIN, OUTPUT);
pinMode(CLK, INPUT); // Set encoder pins as inputs
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(CLK), settingUpdate, FALLING);
dht.begin();
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.display(); // Clear the display buffer
delay(2000); // Pause for 2 seconds
display.clearDisplay(); // Clear the display buffer
}
void settingUpdate() {
int dtValue = digitalRead(DT);
if (dtValue == HIGH) {
temperature_SET++;
}
if (dtValue == LOW) {
temperature_SET--;
}
if (temperature_SET < temp_MIN) {
temperature_SET = temp_MIN;
} else if (temperature_SET > temp_MAX) {
temperature_SET = temp_MAX;
}
}
/*
void settingUpdate() {
byte buttonState = digitalRead(SW);
if (buttonState != buttonPress) {
buttonPress = buttonState;
if (!buttonState) {
Serial.println("clicked");
}
}
byte currentStateCLK = digitalRead(CLK); // Read the current state of CLK
if (currentStateCLK != lastStateCLK) {
// There was a change on the CLK pin
lastStateCLK = currentStateCLK;
byte dtValue = digitalRead(DT);
if (currentStateCLK == LOW && dtValue == HIGH) {
temperature_SET++;
}
if (currentStateCLK == LOW && dtValue == LOW) {
temperature_SET--;
}
// Ensure temperature criteria stays within sensible bounds
if (temperature_SET < temp_MIN) {
temperature_SET = temp_MIN;
} else if (temperature_SET > temp_MAX) {
temperature_SET = temp_MAX;
}
screenUpdate();
}
}
*/
void screenUpdate() {
// Display information on OLED screen
display.clearDisplay();
//Current temperature and Humidity section
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(5,2);
display.print("Temp:");
display.setCursor(5,25);
display.print("Humd:");
display.setCursor(40,0);
display.setTextSize(2);
display.print(temperature);
display.print(" C");
display.drawCircle(107, 2, 2, WHITE);
display.setCursor(40,20);
display.setTextSize(2);
display.print(humidity);
display.print(" %");
//Set temperature section
display.fillRect(0, 39, 128, 25, WHITE);
display.setTextColor(BLACK);
display.setTextSize(1);
display.setCursor(5,44);
display.print(" Set");
display.setCursor(5,52);
display.print("Temp:");
display.setCursor(40,45);
display.setTextSize(2);
display.print(temperature_SET);
display.print(" C");
display.drawCircle(71, 46, 2, BLACK);
if(heaterState && blink_sign) {
const int x_sign = 100;
const int y_sign = 41;
display.drawBitmap(x_sign + 7, y_sign + 6, image_paint_0_bits, 9, 16, BLACK);
display.drawLine(x_sign + 22, y_sign + 18, x_sign + 11, y_sign + 0, BLACK);
display.drawLine(x_sign + 0, y_sign + 18, x_sign + 11, y_sign + 0, BLACK);
display.drawLine(x_sign + 1, y_sign + 19, x_sign + 5, y_sign + 19, BLACK);
display.drawLine(x_sign + 17, y_sign + 19, x_sign + 21, y_sign + 19, BLACK);
}
display.display();
}
void heaterUpdate() {
// Read the temperature and humidity from the DHT sensor
temperature = dht.readTemperature();
humidity = dht.readHumidity();
// Check if the temperature reading is valid
if (!isnan(temperature)) {
// Check if the temperature exceeds the criteria
if (temperature <= temperature_SET ) {
digitalWrite(HEATER_PIN, HIGH); // Turn on the heater
heaterState = true;
} else {
// Turn off the heater if it's not already off
if (heaterState) {
digitalWrite(HEATER_PIN, LOW);
heaterState = false;
}
}
} else {
Serial.println("Nah mate! your sensor sucks ass lol");
}
}
void loop() {
// Check if it's time to read the temperature
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
heaterUpdate();
blink_sign = !blink_sign;
screenUpdate();
}
if(previousTemp_SET != temperature_SET) {
previousTemp_SET = temperature_SET;
screenUpdate();
}
}