Need help guys with Millis() and Tft conflict

Hi guys, I've got my first job as an arduino dev, I've been always just an hobbist but luckly I've found a paid job.
My first task is to create a timer (in minutes) with a GUI to use a DC motor.
Everythings were going fine untill I've tried to implement the Timer logic, if I try to change something on my screen after doing things with millis() my gui blow up.

As an example, this is my normal GUI, default page:

normal GUI, everything's okay.

If I set the timer to (exemple) 2 minutes, when it ends, instead of redraw the standard White play button (when play it's on, the play button is BLUE with the triangle in white), it fucked up everything!

this is what i get when the timer expires:

LCD when the timer expires

this is the code:

(hope the link is ok)

I don't understand why this happen, when the timer expires it just does this:

`if(round((Scadenza/60)/1000) == round((millis()/60)/1000)){// IL TEMPO E' SCADUTO`

`Serial.println("TEMPO SCADUTOOOOOOOOO");`

`tft.fillRoundRect(48,40,96,40,3,WHITE); // TASTI PLAY`


ps: the comments are in italian :D, Scadenza stays for "deadline"

Thank you in advance for your help, I'm in a dead end road and I don't know how to move on!

Your getPoint() code is

  TSPoint p = ts.getPoint(); //Prendiamo le coordinate del nostro tocco
  pixel_x = map(p.y, TS_LEFT, TS_RT, 0, 480); //CALIBRAZIONE TOUCH SCREEN
  pixel_y = map(p.x, TS_TOP, TS_BOT, 0, 320);
  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) { //Se ha la pressione giusta lo prendiamo in considerazione
     Serial.print("X = "); Serial.print(pixel_x);
     Serial.print("\tY = "); Serial.print(pixel_y);
     Serial.print("\tPressure = "); Serial.println(p.z);
     pinMode(YP, OUTPUT);

you should call pinMode() after every getPoint() e.g.

  TSPoint p = ts.getPoint(); //Prendiamo le coordinate del nostro tocco
     pinMode(YP, OUTPUT);
  pixel_x = map(p.y, TS_LEFT, TS_RT, 0, 480); //CALIBRAZIONE TOUCH SCREEN

If you ZIP up your project, I will build it.
i.e. I need the "digi60.h" and "SFONDO.bmp"

I am guessing that you have a 320x480 Blue shield on a MEGA2560


Thanks! I will try this fix, in the meantime if you want to try my code and build it up here's your link:

I'm using a Arduino MEGA2560 with this shield

ZIP is a lot easier than RAR. But anyway, I copied your BMP file onto SD card and added digi60.h to the sketch tab.

I have made several changes marked with //.kbv comments

  1. SdFat.h library v2.x.x has changed the way that "Software SPI" is used.
  2. added my calibration values
  3. altered pinMode() and map() after ts.getPoint(). Note that calibration is for Portrait left, rt, top, bot. Hence the map() functions for Landscape.

The screen seems to respond to touch but I was expecting "second counter" to be displayed.

Life is a lot simpler with Adafruit_GFX_Button class but your program seems to work.

If you have several photo BMP files it is worth displaying from SD card. Your single BMP is quicker to draw with regular GFX graphics methods.

David. (24.5 KB)

WOAH! Thank you so much! IDK why but the old library was breaking everything...literally as I introduced millis() all the problems started, until that moment I didn't noticed anythig!!! not a single problem.
Anyway, one last question, I would like to use other graphic libraries but I don't know how to setup em, I mean MCUFRIEND was for me "plug and play", I don't know how to setup pins and how understand which pin should i define for which var.
Thank you, you saved my day and probably my new carrer!!!

I am sure that SdFat v1.x.x was working fine. After all, I did test it before the MCUFRIEND_kbv v2.9.8 version (released in May 2018).
It looks as if I did not check SdFat versions when v2.9.9 was released in July 2020.
You can see the Beta version of showBMP_not_Uno.ino at

I don't think that millis() was breaking anything. You were only calling pinMode() if there had been a touch.

Regarding Shield pins. They are fixed. The Shield plugs into Arduino headers in only one way.

If you want external LEDs, sensors, switches, ... you have plenty of spare GPIO pins on a MEGA2560.

Regarding graphics libraries. Adafruit_GFX does most things that you ever need. It has intuitive methods. Most TFT, OLED, GLCD libraries use the "GFX" methods. So you write your application once. And it will work with a different library.

Weird methods like TFT.h or UTFT.h are unwise for any new applications.


Thank you again, now it's clear! I'm new into arduino but as I see it has a great community.
Have a nice day!

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