/*********************************************
Autor: Marlon Nardi Walendorff
Projeto: Dry Fila Box - Caixa para armazena e secar filamentos para impressoras 3D - Versão 2 - Agora com novo sistema de aquecimento e controle PWM e PID
Detalhes do projeto: https://marlonnardi.com/?p=1529
/**********************************************/
//Fica de ideia para quem quiser adicionar uma mensagem de erro no display caso haja problema de comunicação com o DHT22
// Volume da caixa de 60 litros. Medidas 60 x 36.5 x 31 cm = 0.06789 m³ ou 67,89 cm³ ou 67.89 Litros
// Quantos gramas de água no ar no máximo à 45ºC à umidade relativa de 100R% cabem na caixa de isopor já que seu volume é de 0.06789m³: Total de umidade absoluta 65.35 g/m³, com isso caberiam 4.4366 gramas de água no ar no máximo no volume da caixa de isopor
//==================== Inclusão de Bibliotecas =================//
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include "DHT.h"
#include <PID_v1.h>
//==================== Mapeamento de Hardware ==================//
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>
#include <FreeDefaultFonts.h>
#define TEMPERATURAALVO 45.00 // ºC
#define pin_heater 11
#define pin_fan 10
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define GREETEMP 0x5fe0
#define BLUEUMIDADER 0x073f
#define VIOLETUMIDADEA 0xe01f
#define ARDUINOCOLOR 0x07ff
#define DHTPIN 12
// ---------------- Constante Matemática de Euler
#define e 2.718281828459045235360287471352
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//==================== Variáveis Globais ==================//
float temp = 0;
float hum = 0;
float humAbs = 0;
float pretemp = 0;
float prehum = 0;
float prehumAbs = 0;
float currtemp = 0;
float currhum = 0;
float currhumAbs = 0;
float i = 0;
float j = 0;
double Setpoint, Input, Output;
double Kp = 2, Ki = 5, Kd = 1;
//==================== Instânciando Objetos ====================//
MCUFRIEND_kbv tft;
DHT dht(DHTPIN, DHTTYPE);
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
void setup(void)
{
Serial.begin(9600);
pinMode(pin_heater, OUTPUT);
pinMode(pin_fan, OUTPUT);
analogWrite(pin_fan, 25);//Controla a velocidade do Cooler/FAN
Setpoint = TEMPERATURAALVO;
myPID.SetMode(AUTOMATIC);
uint16_t ID = tft.readID();
Serial.print("found ID = 0x");
Serial.println(ID, HEX);
if (ID == 0xD3D3) ID = 0x9481; //force ID if write-only display
tft.begin(ID);
tft.setRotation(1);
tft.fillScreen(BLACK);
dht.begin();
//-------------- Texto Inicial
tft.drawRect(0, 0, 320, 240, ARDUINOCOLOR);
tft.setTextSize(7);
tft.setTextColor(ARDUINOCOLOR, BLACK);
tft.setCursor(15, 30);
tft.print("MESTRES");
tft.setCursor(120, 95);
tft.print("DO");
tft.setCursor(15, 160);
tft.print("ARDUINO");
delay(3000);
tft.fillScreen(BLACK);
//-------------- Tela inicial Fixa Display
// Título
tft.setTextSize(3);
tft.setTextColor(YELLOW, BLACK);
tft.setCursor(60, 5);
tft.print("Dry Fila Box");
tft.setTextSize(2);
tft.setCursor(75, 30);
tft.print("marlonnardi.com");
//Temperatura
tft.setTextSize(2);
tft.setTextColor(GREETEMP, BLACK);
tft.setCursor(24, 170);
tft.print("0");
tft.setCursor(62, 170);
tft.print("50");
tft.setTextSize(1);
tft.setCursor(18, 100);
tft.print("Temperatura");
tft.setCursor(45, 110);
tft.print("*C");
//Umidade Relativa
tft.setTextSize(2);
tft.setTextColor(BLUEUMIDADER, BLACK);
tft.setCursor(24 + 110, 170);
tft.print("0");
tft.setCursor(58 + 110, 170);
tft.print("100");
tft.setTextSize(1);
tft.setCursor(20 + 110, 100);
tft.print("Umidade R.");
tft.setCursor(45 + 110, 110);
tft.print("R%");
//Umidade Absoluta
tft.setTextSize(2);
tft.setTextColor(VIOLETUMIDADEA, BLACK);
tft.setCursor(24 + 220, 170);
tft.print("0");
tft.setCursor(62 + 220, 170);
tft.print("80");
tft.setTextSize(1);
tft.setCursor(20 + 220, 100);
tft.print("Umidade A.");
tft.setCursor(38 + 220, 110);
tft.print("g/m3");
}
void loop(void)
{
controleTemperatura();
// ---------------------- Exibe temperatura com elementos gráficos
temp = dht.readTemperature();
hum = dht.readHumidity();
humAbs = calculaUmidadeAbsoluta();
tft.setTextColor(GREETEMP, BLACK);
tft.setTextSize(2);
tft.setCursor(27, 125);
tft.print(temp, 1);
tft.setTextColor(BLUEUMIDADER, BLACK);
tft.setTextSize(2);
tft.setCursor(27 + 110, 125);
tft.print(hum, 1);
tft.setTextColor(VIOLETUMIDADEA, BLACK);
tft.setTextSize(2);
tft.setCursor(27 + 220, 125);
tft.print(humAbs, 1);
// ---------------------- Mostra no displays parâmetros de configuração
tft.setTextSize(1.5);
tft.setTextColor(GREETEMP, BLACK);
tft.setCursor(0, 220);
tft.print("Temp. Alvo:");
tft.setCursor(68, 220);
tft.print(TEMPERATURAALVO, 0);
tft.setCursor(81, 220);
tft.print("*C");
currtemp = temp;
currhum = hum;
currhumAbs = humAbs;
i = map(pretemp, 0, 50, 0, 300);
j = map(currtemp, 0, 50, 0, 300);
for (i; i <= j; i = i + 0.1)
{
float j = i - 150 ;
float angle = (j / 57.2958) - 1.57;
float x1 = 50 + cos(angle) * 40;
float y1 = 120 + sin(angle) * 40;
float x2 = 50 + cos(angle) * 50;
float y2 = 120 + sin(angle) * 50;
tft.drawLine(x1, y1, x2, y2, GREETEMP);
}
for (i - 2; i >= j; i = i - 0.05)
{
float j = i - 150 ;
float angle = (j / 57.2958) - 1.57;
float x1 = 50 + cos(angle) * 40;
float y1 = 120 + sin(angle) * 40;
float x2 = 50 + cos(angle) * 50;
float y2 = 120 + sin(angle) * 50;
tft.drawLine(x1, y1, x2, y2, BLACK);
}
// ---------------------- FIM Exibe temperatura com elementos gráficos
// ---------------------- Exibe Umidade Relativa com elementos gráficos
i = map(prehum, 0, 100, 0, 300);
j = map(currhum, 0, 100, 0, 300);
for (i; i <= j; i = i + 0.1)
{
float j = i - 150 ;
float angle = (j / 57.2958) - 1.57;
float x1 = 160 + cos(angle) * 40;
float y1 = 120 + sin(angle) * 40;
float x2 = 160 + cos(angle) * 50;
float y2 = 120 + sin(angle) * 50;
tft.drawLine(x1, y1, x2, y2, BLUEUMIDADER);
}
for (i - 2; i >= j; i = i - 0.05)
{
float j = i - 150 ;
float angle = (j / 57.2958) - 1.57;
float x1 = 160 + cos(angle) * 40;
float y1 = 120 + sin(angle) * 40;
float x2 = 160 + cos(angle) * 50;
float y2 = 120 + sin(angle) * 50;
tft.drawLine(x1, y1, x2, y2, BLACK);
}
// ---------------------- FIM Exibe Umidade Relativa com elementos gráficos
// ---------------------- Exibe Umidade Absoluta com elementos gráficos
i = map(prehumAbs, 0, 100, 0, 300);
j = map(currhumAbs, 0, 100, 0, 300);
for (i; i <= j; i = i + 0.1)
{
float j = i - 150 ;
float angle = (j / 57.2958) - 1.57;
float x1 = 270 + cos(angle) * 40;
float y1 = 120 + sin(angle) * 40;
float x2 = 270 + cos(angle) * 50;
float y2 = 120 + sin(angle) * 50;
tft.drawLine(x1, y1, x2, y2, VIOLETUMIDADEA);
}
for (i - 2; i >= j; i = i - 0.05)
{
float j = i - 150 ;
float angle = (j / 57.2958) - 1.57;
float x1 = 270 + cos(angle) * 40;
float y1 = 120 + sin(angle) * 40;
float x2 = 270 + cos(angle) * 50;
float y2 = 120 + sin(angle) * 50;
tft.drawLine(x1, y1, x2, y2, BLACK);
}
// ---------------------- FIM Exibe Umidade Absoluta com elementos gráficos
pretemp = currtemp;
prehum = currhum;
prehumAbs = currhumAbs;
delay(100);
}
float calculaUmidadeAbsoluta(void)
{
float UA = ((6.112 * (pow(e, ((17.67 * temp) / (temp + 243.5)))) * hum * 2.1674) / (273.15 + temp));
return UA;
}
void controleTemperatura(void)
{
//----------------------------------- Controle PID ----------------------------//
Input = temp;
myPID.Compute();
analogWrite(pin_heater, Output);
Serial.print("Temperatura: ");
Serial.print(temp);
Serial.print(" PWM ");
Serial.println(Output);
}
HEATER
FAN