Programm bugs out after around 2 min

Hello there

I wrote a programm for a school project. If i turn on my arduino, the display will start to flash alternating between black and white screen after around 2 minutes. It does this even if I dont press any buttons on my screen. My code will only cycle trough a little bit of touchscreen code (edge detection) and will enter one case of the switch. So i'm pretty sure it has to be something in that are of the code.

If i delete the edge detection from the code it doesnt happen. So it probably has to do something with the millis()?

Heres the part of the code that I think has the problem. Have a look at my edge detection inside the loop.

// Include needed libraries
#include <Adafruit_GFX.h>
#include <Adafruit_HX8357.h>
#include <TouchScreen.h>
#include <SD.h>
#include <SPI.h>

// Define pins
#define LCD_CS 47        // LCD CS Pin
#define LCD_DC 45        // LCD D/C Pin
#define SD_CS 46         // SD Card Pin
#define YP A2            // Touchscreen Y+ pin
#define XM A3            // Touchscreen X- pin
#define YM 48            // Touchscreen Y- pin
#define XP 49            // Touchscreen X+ pin

// Giving used pins to the library
Adafruit_HX8357 display = Adafruit_HX8357(LCD_CS, LCD_DC);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

// Variables for touchscreen edge detection
bool TouchDown;
bool TouchTrigger;
bool PressureReading;
bool PressureReadingNoNoise;
unsigned long Debounce;
int px;
int py;

// SETUP*************************************************************************************************************************************************
void setup()
  display.setRotation(1); // Flip XY-Matrix to fit the display orientation
  display.setTextColor(0x7E3D); // 

// LOOP*************************************************************************************************************************************************
void loop()
  TSPoint p = ts.getPoint();                                 // Measures X,Y or Z event
  int xy_swap = p.y;                                         // Because the display is in landscape, we need to swap x and y coordinate
  p.y = map(p.x, TS_BOT, TS_TOP, 0, display.height());       // Calibrate touchscreen
  p.x = map(xy_swap, TS_LEFT, TS_RIGHT, 0, display.width()); // Calibrate touchscreen
  PressureReading = p.z > 100;                               // Reads if the screen is pressed
  if (PressureReading)                                       // Touchscreen edge detection
    PressureReadingNoNoise = true;
    Debounce = millis();
    px = p.x;
    py = p.y;
  if (!PressureReading && millis() - Debounce > 50) // Debounce signal to negate noise
    PressureReadingNoNoise = false;
    TouchTrigger = false;

  if (TouchDown != PressureReadingNoNoise)
    TouchTrigger = true;
    TouchDown = !TouchDown;
    p.x = px;
    p.y = py;


Thanks for the help

EDIT: I know its probably not a beautiful way to do the edge detection for the touchscreen ( butten gets pressed when finger is released) but thats the only way that I have made it to work after 3hrs... So if you have a shorter/better/easier way then please let me know!

Your code is incomplete

If it works for two mins, then falls over, it could be a power problem.

Can you sketch out your schematic / wiring - And include your power sources.

1 Like

Hi lastchancename

Thx for your quick reply.

Its a meanwell Power Supply 12V 4.2 amps. Here is my schematic:
Schematic.pdf (49,6 KB)

Thanks for the schematic. One big issue - never place a component on top of another component. Usually this is a way of signifying that it is a sub-system that is contained entirely within the other (physically). So move those motor drivers off the Mega...

Also... still waiting for the full code.

first guess is that motor power or return is going via the arduino..

In the wrong conditions, the onboard regulator will overheat and shutdown… trying to feed the idle current to the motors & drivers.

Thx for the reply aarg

I cannot remove the drivers as they are soldered to a arduino shield which is ontop of the arduino. I can unplug the shield and connect de display directly to the arduino and without the drivers and see if that makes a difference. Also space was a big limiting factor when I designed all the parts and electronics box, I could've not fitted the drivers anywhere else.

What I also found out is that it does not happen when arduino is plugged in over USB. When I unplug USB and power it over the power supply then it happens. (Motors and endstops arent attached at all in both scenarios)

So it could really have to do with the drivers or unfiltered power supply maybe...... At this point into the project (160h) I cant really go over many things again. I can use max 250hours and need to do other things and write the documentation. I hope I can find an easy fix.

I cannot post the full code at the moment as it has confidental information inside.. Would need to go over the code and change some names...

Okay, I see. It actually does represent the physical layout because the drivers are actually on the shield. Best way to diagram that, would be a separate symbol for the shield and on board drivers, connections to the Mega separate from that. But if you're running out of time, maybe next time.

It does sound like a hardware problem, so images of all your hardware would help. I see that the display is powered from the Mega 5V, perhaps it is drawing too much current. Try powering it from a separate 5V supply. You might end up having to supply 5V from an independent power supply to the display and Mega together, or a step down converter from the 12V.

Which, "unfiltered power supply"? Is that the 12V? I only see a box with a few labels there, need a lot more details.

Can you not redact the confidential information in the code?

1 Like

Im currently testing a few things, will list them here:

-When plugging display to arduinos 3V3 it happens already after 5 seconds. (PowerSource PSU)

-When pluggin display to arduino 3V3 it only shows white screen. (PowerSource USB)

-Unplugged the shield with the drivers on it and wired display directly to arduino. Still works as intended after 10min (PowerSource PSU).

-Unplugged the shield with the drivers on it and wired display directly to arduino, but 3V3 instead of 5V. Screen starts flickering right away. (PowerSource PSU)

-Unplugged the shield with the drivers on it and wired display directly to arduino, but 3V3 instead of 5V. White screen from the start. (PowerSource USB)

If its some kind of frequency or power problem from the drivers then im lost because I have no clue of electronics. If I find some hardware I will try to give the LCD power from seperate 5V

I would be more comfortable if that expression had more parenthesis...

  if ((!PressureReading) && (millis() - Debounce > 50)) 

(I mean, it MIGHT be correct as-is. But I'm not immediately sure, and I want to be!)

1 Like

When it started alternating between black and white screen I put a Noctua Fan over the Arduino and the shield with the drivers on it and 5 seconds later my Mainscreen loaded.

What the heck! Looks like its an overheating problem, even tho motors are not connected...

Started a test now with the fan ontop. Will leave it 2-3hours and check if it happens again. If not I will try to fit a fan inside the electronics box.

Stuff running for 3 hours with no problem. Cooling fan blowing into the side of arduino and shield.

I think that fan is the solution. Thanks for bringing the hardware clues! I would've checked in the code for much longer.

I'll call this problem solved. Thx !


Except… why so much heat?

Could it be a software problem after all?



It happend with empty sketch uploaded.

I dont know why there so much heat. If I still have time at the end I will comeback and investigate a bit moore.

1 Like

Unless you are unusually talented programmer, you've ruled out software. :expressionless:


If you would've read all of the comments you'd know that im in a hurry and have no time for that.

I know you just wanted to help.

No it was supposed to be funny, like Chuck Norris is such good programmer that even his empty sketches burn up the machine.


With 12volt on V-in you can't draw more than 150mA total from the Mega (any pin, including the 5volt pin), without overheating the 5volt regulator.
This (max current draw at higher supply input) should be documented in the specifications of each type of Arduino, which unfortunately it's not.

Try to drop the 12volt supply to V-in to about 9volt with four 1N4004 diodes in series.

Or supply the display 5V with a separate voltage regulator or step down converter.

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