I want to make it clear that I have never done anything like this, I have no experience in coding. I'm trying to build a filament dryer for my 3D printer. I know there are many on the market, but I wanted something different, more complete. I created the display interface and I can read the temperature and humidity and set the timer to work and appear on the screen. The display is connected to the Arduino's RX118 and TX119. My problem is that I can't create a button to start and stop the timer. I defined several buttons and at the moment I have two on the display, one Variables Icon VP5012 with the Return Key Code function, and another, Bit icon display VP5030 with Incremental adjustment. As I said, I've tried several configurations, but I don't really know which one to use. The timer is divided into 3 parts because I want to be able to set the hours and minutes individually. I'm using an Arduino Mega 2560 and a DWIN display. I would like to know if anyone can help me unlock this function. I believe that once I can associate the display button with the timer on/off function, the rest I can solve. But I've tried so many things, but I'm lost. The information provided by DWIN is not easy to understand for those just starting out. I appreciate any help you can give me and if you need any more information I am available.
#include <DHT.h>
#include <DWIN.h>
#define DHT22_PIN 2
#define DGUS_BAUD 9600
DWIN hmi(18, 19, DGUS_BAUD);
DHT dht22(DHT22_PIN, DHT22);
const unsigned long interval1 = 2000; // Interval for Loop 1 in milliseconds
const unsigned long interval2 = 1000; // Interval for Loop 2 in milliseconds
int initialHours = 2; // Initial value for hours
int initialMinutes = 0; // Initial value for minutes
int initialSeconds = 0; // Initial value for seconds
int hours = initialHours; // Current value for hours
int minutes = initialMinutes; // Current value for minutes
int seconds = initialSeconds; // Current value for seconds
bool timerStarted = false; // Flag to track if the timer has started
void setup() {
Serial.begin(9600);
dht22.begin(); // initialize the DHT22 sensor
hmi.setBrightness(10); // Set the brightness of the display
timerStarted = true; // Set the flag indicating timer has started
}
void resetTimer() {
// Reset the timer values to initial values
hours = initialHours;
minutes = initialMinutes;
seconds = initialSeconds;
// Update display for initial values
updateDisplay(hours, minutes, seconds);
}
void updateDisplay(int h, int m, int s) {
// Update display for hours
char hoursArray[3];
sprintf(hoursArray, "%02d", h); // Format hours with leading zeros
hmi.setText(0x2030, hoursArray);
// Update display for minutes
char minutesArray[3];
sprintf(minutesArray, "%02d", m); // Format minutes with leading zeros
hmi.setText(0x2040, minutesArray);
// Update display for seconds
char secondsArray[3];
sprintf(secondsArray, "%02d", s); // Format seconds with leading zeros
hmi.setText(0x2050, secondsArray);
}
void loop() {
// Loop 1: Update humidity and temperature every 2 seconds
static unsigned long previousMillis1 = 0;
unsigned long currentMillis1 = millis();
if (currentMillis1 - previousMillis1 >= interval1) {
previousMillis1 = currentMillis1;
// Read humidity
int humi = dht22.readHumidity();
// Read temperature as Celsius
int tempC = dht22.readTemperature();
// Read temperature as Fahrenheit
int tempF = dht22.readTemperature(true);
// Convert humidity into character buffer
char humiArray[10];
itoa(humi, humiArray, 10);
hmi.setText(0x3010, humiArray);
// Convert temperature into character buffer
char tempArray[10];
itoa(tempC, tempArray, 10);
hmi.setText(0x2010, tempArray);
// Check if any reads failed
if (isnan(humi) || isnan(tempC) || isnan(tempF)) {
Serial.println("Failed to read from DHT22 sensor!");
} else {
Serial.print("LEOTE3 | Humidity: ");
Serial.print(humi);
Serial.print("% | ");
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.print("°C - ");
Serial.print(tempF);
Serial.println("°F");
}
}
// Loop 2: Update countdown timer every second
static unsigned long previousMillis2 = 0;
unsigned long currentMillis2 = millis();
if (currentMillis2 - previousMillis2 >= interval2 && timerStarted) {
previousMillis2 = currentMillis2;
// Update display for hours
char hoursArray[3];
sprintf(hoursArray, "%02d", hours); // Format hours with leading zeros
hmi.setText(0x2030, hoursArray);
// Update display for minutes
char minutesArray[3];
sprintf(minutesArray, "%02d", minutes); // Format minutes with leading zeros
hmi.setText(0x2040, minutesArray);
// Update display for seconds
char secondsArray[3];
sprintf(secondsArray, "%02d", seconds); // Format seconds with leading zeros
hmi.setText(0x2050, secondsArray);
// Decrement seconds
seconds--;
// If seconds becomes negative, decrement minutes and reset seconds to 59
if (seconds < 0) {
seconds = 59;
minutes--;
// If minutes becomes negative, decrement hours and reset minutes to 59
if (minutes < 0) {
minutes = 59;
hours--;
// If hours becomes negative, reset to initial values
if (hours < 0) {
// Reset the timer when hours, minutes, and seconds all reach zero
resetTimer();
// Stop the timer
timerStarted = false;
}
}
}
// Start the timer if it's not started and the timer is reset
if (!timerStarted && hours != 0 && minutes != 0 && seconds != 0) {
timerStarted = true;
}
}
}