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.begin();
display.setRotation(1); // Flip XY-Matrix to fit the display orientation
display.fillScreen(HX8357_BLACK);
display.setTextColor(0x7E3D); //
display.setTextSize(2);
SD.begin(SD_CS);
}
// 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
Anxodia
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!
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...
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?
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
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.
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.
Leo..