/*###################################################################################################################################
* Program made by Damian Martinez Sanchez for the printable filament dryer.
* Modify <MANUAL CONFIGURATION> variables depending on the PCB system and sensors used for the dryer.
* Designed for Arduino NANO v.3
###################################################################################################################################*/
#include <EEPROM.h>
#include <SPI.h>
#include <Wire.h>
#include <DHT.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1); // Parametros pantalla Oled
const unsigned char bmp1 [] PROGMEM = {
0x00, 0x00, 0x07, 0xe0, 0x07, 0xe0, 0x01, 0x80, 0x01, 0x80, 0x30, 0x0c, 0x30, 0x0c, 0x3d, 0xbc,
0x3d, 0xbc, 0x30, 0x0c, 0x31, 0x8c, 0x01, 0x80, 0x07, 0xe0, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00
};
const unsigned char bmp2 [] PROGMEM = {
0x00, 0x00, 0x08, 0x10, 0x18, 0x18, 0x38, 0x1c, 0x7c, 0x3e, 0x0c, 0x30, 0x00, 0x00, 0x01, 0x80,
0x01, 0x80, 0x00, 0x00, 0x0c, 0x30, 0x7c, 0x3e, 0x38, 0x1c, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00
};
const int DHTPIN = 4; //DHT Sensor Readings
const int relay = 2; // Relay of the thermal resistance of 12v
const int fan = 5; // Ventilador PCB 2
const int coldled = 8; //Led Cold - B
const int warmled = 7; //Led Hot - A
const int store = 11; //Store Button - G
const int Bmenos = 10; //Minus button - F
const int Bmas = 9; //Plus button - E
//########################################################################################
//################################ <CONFIGURACION MANUAL> ################################
//########################################################################################
#define DHTTYPE DHT22 // Set sensor type DHT11 or DHT22
int AONOFF = 0; // 1 = Fan on 100% // 0 = Fan on at 50%
int Tmax = 55; // Maximum temperature (exceeding the maximum temperature could damage components exposed to direct heat).
int Tmin = 23; // Minimum temperature (not less than room temperature).
/////////////////////////////// <FIN CONFIGURACION MANUAL> ///////////////////////////////
DHT dht(DHTPIN, DHTTYPE);
float TMP = dht.readTemperature();
float HUM = dht.readHumidity();
int Hcontrol = 12; // Default drying humidity (The filament must be between 10 and 13% humidity)
float T = 2.55; // 12v fan speed multiplier.
int pulso;
int ultimo_pulso;
int Tcontrol = EEPROM.read(3);
int Tbucle = 0; // main loop control
int letargo = 0; // secondary loop control
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(fan, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(coldled, OUTPUT);
pinMode(warmled, OUTPUT);
pinMode(DHTPIN, INPUT);
pinMode(store, INPUT);
pinMode(Bmenos, INPUT);
pinMode(Bmas, INPUT);
// Start Oled, if fail, show error. repeat loop. Alarm led 2 flashes per second.
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("Display not found.");
for(;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 16);
display.println("Loading EEPROM...");
display.display();
delay(500);
// Check if the starting control is recorded (position 0 of the memory), if not, record variables in memory.
if (EEPROM.read(0) != 1) { // Startup control to save the variables for the first and only time when the program is installed
EEPROM.write(0, 0);
EEPROM.put(1, AONOFF);
EEPROM.put(3, 30);
}
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 25);
display.println("Reading DHT...");
display.display();
delay(500);
//Check DHT readings. If it fails to show error, repeat loop. Alarm led 1 flash per second.
if (isnan(dht.readTemperature()) || isnan(dht.readHumidity())) {
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 35);
display.println("Error Reading DHT.");
display.display();
for(;;); // If the reading fails, it stops the program.
} else {
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 35);
display.println("DHT OK.");
display.display();
delay(1000);
}
}
void loop() {
display.clearDisplay();
//###########################################################################
//################ START READINGS ###############################
//###########################################################################
TMP = dht.readTemperature();
HUM = dht.readHumidity();
//////////////////// END COUNTER READINGS /////////////////////
//##########################################################################################
//############## STARTING MAIN SCREEN OLED 128x64 2 Bicolor zones ##################
//##########################################################################################
if (HUM >= Hcontrol) {
displaymenu();
secando1();
}
else {
displaymenu();
secado();
}
///////////////////////////// END Part.1 MAIN SCREEN /////////////////////////////
//##########################################################################################
//######################### STARTING DRY CONTROL #########################
//##########################################################################################
if (HUM >= Hcontrol) {
digitalWrite(coldled, HIGH);
if (AONOFF == 1) { // Determines the fan speed, 100% or 50%, depending on the voltage applied to the BC337.
digitalWrite(fan, HIGH);
} else {
digitalWrite(fan, LOW);
}
}
else if (HUM <= Hcontrol) {
digitalWrite(coldled, LOW);
digitalWrite(warmled, HIGH);
digitalWrite(relay, LOW);
}
if (TMP >= Tmax) {
digitalWrite(relay, LOW);
} else {
digitalWrite(relay, HIGH);
}
Serial.println(HUM);
Serial.println(EEPROM.read(1));
///////////////////////////// END AUTOMATIC DRYING CONTROL /////////////////////////////////
//#######################################################################################
//######################### MENU ENTRY CONTROL #################################
//#######################################################################################
if (digitalRead(Bmas) == 1) { Tbucle = 1; }
if (digitalRead(Bmenos) == 1) { Tbucle = 1; }
if (digitalRead(store) == 1) { Tbucle = 3; }
///////////////////// RISE TEMPERATURE ////////////////////////////
if (Tbucle == 1) { // Loop hold control of on-screen temperature menus
while (Tbucle == 1) {
display.clearDisplay();
if (digitalRead(Bmas) == 1) {
while (digitalRead(Bmas) == 1) { letargo = 2; }
while (letargo == 2) { // Loop hold menu raise temperature
if (digitalRead(Bmas) == 1) {
while(digitalRead(Bmas) == 1){
display.clearDisplay();
display.fillTriangle(30, 18, 10, 58, 50, 58, SSD1306_WHITE);
displaytemp();
display.display();
}
if (Tcontrol < Tmax) {
++Tcontrol;
}
}
else if (digitalRead(Bmas) == 0) {
display.clearDisplay();
display.drawTriangle(30, 18, 10, 58, 50, 58, SSD1306_WHITE);
displaytemp();
display.display();
}
if (digitalRead(Bmenos) == 1) { letargo = 1; } // If set, exit loop up and jump to loop down
if (digitalRead(store) == 1) {
grabardatos();
letargo = 0;
Tbucle = 0;
}
} // End loop hold menu raise temperature
}
///////////////////// LOWER TEMPERATURE ////////////////////////////
if (digitalRead(Bmenos) == 1) {
while (digitalRead(Bmenos) == 1) { letargo = 1; }
while (letargo == 1) {
if (digitalRead(Bmenos) == 1) {
while(digitalRead(Bmenos) == 1){
display.clearDisplay();
display.fillTriangle(10, 18, 50, 18, 30, 58, SSD1306_WHITE);
displaytemp();
display.display();
}
if (Tcontrol > Tmin) {
--Tcontrol;
}
}
else if (digitalRead(Bmenos) == 0) {
display.clearDisplay();
display.drawTriangle(10, 18, 50, 18, 30, 58, SSD1306_WHITE);
displaytemp();
display.display();
}
if (digitalRead(Bmas) == 1) { letargo = 2; }
if (digitalRead(store) == 1) {
grabardatos();
letargo = 0;
Tbucle = 0;
}
}
}
}
}
///////////////////// ENDTEMPERATURE CONTROL MENU ////////////////////////////
////////////////////////// FAN CONFIGURATION MENU //////////////////////////////
if (Tbucle == 3) {
while (digitalRead(store) == 1) {}
while (Tbucle == 3) { // Loop maintain menu FAN onscreen
display.clearDisplay();
menufan();
display.display();
if (digitalRead(Bmas) == 1) {
while (digitalRead(Bmas) == 1) {} // button return
if (AONOFF < 1) {
++AONOFF;
}
else { --AONOFF; }
Serial.println(AONOFF);
}
if (digitalRead(store) == 1) {
while(digitalRead(store) == 1) {} // button return
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(5,50);
display.println("Writing EEPROM...");
display.display();
EEPROM.update(1, AONOFF);
delay(2000);
display.clearDisplay();
Tbucle = 0;
}
}
}
////////////////////////////// END FAN MENU //////////////////////////////////
if (HUM >= Hcontrol) {
displaymenu();
secando2();
}
else {
displaymenu();
secado();
}
display.clearDisplay();
} // FIN DEL PROGRAMA
void grabardatos() {
while (digitalRead(store) == 1) {}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("EEPROM.");
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 20);
display.print("Stored ");
display.print("Temp. ");
display.print(Tcontrol);
display.cp437(true);
display.write(167);
display.display();
EEPROM.update(3, Tcontrol);
delay(2000);
display.clearDisplay();
}
void displaytemp() {
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Menu Temp.");
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
display.setCursor(70, 30);
display.println(Tcontrol);
}
void displaymenu() {
display.clearDisplay();
display.drawRect(0, 16, 128, 48, SSD1306_WHITE);
display.drawLine(0, 39, 128, 39, SSD1306_WHITE);
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(5,20);
display.print("Temp ");
display.print(TMP);
// display.cp437(true);
// display.write(167);
display.setCursor(5,43);
display.print("Humi ");
display.print(HUM);
// display.cp437(true);
// display.write(37);
}
void secando1() {
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Drying");
display.drawBitmap(112, 0, bmp1, 16, 16, SSD1306_WHITE);
display.display();
delay(250);
}
void secando2() {
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Drying");
display.drawBitmap(112, 0, bmp2, 16, 16, SSD1306_WHITE);
display.display();
delay(250);
}
void secado() {
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("DRY OK.");
display.display();
delay(10);
}
void menufan() {
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("FAN Menu");
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,25);
display.print("Speed ");
if (AONOFF == 1) {
display.print("50");
display.cp437(true);
display.write(37);
}
else {
display.print("100");
display.cp437(true);
display.write(37);
}
}