Creating a button on/off to control the timer - Arduino Mega 2560 - DWIN display

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;
    }
  }
}

Which development system are you using?

don't see any button code in the code you posted

here's an example for reading/debouncing a button press. it simply toggles an LED on/off

// simple button processing

const byte butPin = A1;
byte       ledPin = 13;

byte butState;

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void loop (void)
{
    byte but = digitalRead (butPin);

    if (butState != but)  {     // check that button state changed
        butState = but;
        delay (10);         // debounce

        if (LOW == but)     // button pressed
            digitalWrite (ledPin, ! digitalRead (ledPin));
    }
}

// -----------------------------------------------------------------------------
void setup (void)
{
    digitalWrite (ledPin, Off);
    pinMode      (ledPin, OUTPUT);

    pinMode     (butPin, INPUT_PULLUP);
    butState  = digitalRead (butPin);
}

it's also probably easier to track time by just counting seconds and converting the seconds to hrs/mins/secs

unsigned long secs;

void
loop (void)
{
    secs++;

    int hr   =  secs / 3600;
    int min  = (secs % 3600) / 60;
    int sec  = (secs % 3600) % 60;

    char s [20];
    sprintf (s, "%2d:%02d:%02d", hr, min, sec);
    Serial.println (s);
}

void
setup (void)
{
    Serial.begin (9600);
}

Hi DrDiettrich, I’m using DGUS to create the display features, but I don’t know how to configure the button and how to code in Arduino to add the button function to control the timer.

Hi gcjr,
I tried several ways but none worked and so I thought it would be better to show the code without the function and ask for help creating it from scratch.
The main issue is that I'm using a DWIN display that uses DGUS software to configure everything on the display. The interaction between the Arduino and the display depends a lot on the way the display is configured and the way the Arduino is coded to read and write. As I have never done anything in this area, it is all new to me. Regarding the timer, I will try to use your solution. It's much simpler and cleaner. thanks

Then ask related questions in a DGUS forum.

Arduino buttons typically are mechanical switches, not buttons painted on a display.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.