Counter/timer questions (solved)


I searched the net for simple counter for arduino mega and touchscreen display. Found some for LCD displays but did not understand them very well. So I came up with a simple code of my own. Counters/timers work but I I would like to add the function that if there is 5 minutes left, then the background for that timer would go red. I tried if time < 5 minutes, it kind of works but it flickers. Another problem is that after 10 minutes comes 9, so 9 gets printed in place of 1 and 0 stays there displaying 90 minutes. I tried drawing filled black rectangles but again results were flickery. Using if time is bigger than 0 and only then print text I achieved that it does not start counting minus minutes but for that there is also better solution. Any help is appreciated.

Its only my second arduino project so dont hit in the face if the code is terrible.

#include <UTFT.h>
#include <UTouch.h>
extern uint8_t BigFont[];

UTFT        myGLCD(ITDB32S, 38,39,40,41);   // Remember to change the model parameter to suit your display module!
UTouch      myTouch(6,5,4,3,2);
int x, y;
long timer1;
long aeg1; 
long timer2;
long aeg2; 
long timer3;
long aeg3; 
long timer4;
long aeg4; 
long timer5;
long aeg5;
long timer6;
long aeg6; 

void setup()

   myGLCD.print("I LIIN",10,12);
   myGLCD.print("II LIIN",10,52);
   myGLCD.print("III LIIN",10,92);
   myGLCD.print("IV LIIN",10,132);
void loop()
    if (0 <= aeg1){
    if (0 <= aeg2){
    if (0 <= aeg3){
    if (0 <= aeg4){
    if (0 <= aeg5){
    if (0 <= aeg6){
    if (myTouch.dataAvailable())
      if ((x>=0) && (x<=150))
      if ((y>=0) && (y<=40)){
      if ((x>=0) && (x<=150))
      if ((y>=40) && (y<=80)){
      if ((x>=0) && (x<=150))
      if ((y>=80) && (y<=120)){
      if ((x>=0) && (x<=150))
      if ((y>=120) && (y<=160)){
          if ((x>=0) && (x<=150))
      if ((y>=160) && (y<=200)){
      if ((x>=0) && (x<=150))
      if ((y>=200) && (y<=240)){


  aeg1=timer1-millis();As you’ve not given timer1 a value it will be 0 by default. millis() is the number of milliseconds since the Arduino was powered up/reset. So you’re taking an almost random number away from zero which will leave aeg1 as a negative number. Therefore,:   if (0 <= aeg1) will always be false. What, exactly, are you trying to do here?

Thank you for interest.

Pushing a button gives timer1 value:
if (0 <= aeg1) then means that when i start the program all the timers are 0 and will not show -1 in a minute if buttons are not pressed.

How to make the program do something once then the timer reaches a certain value? Lets say that this action should happen when timer goes under 5 minutes.
I wrote something like this:
if (0 <= aeg1 <= 300000) then draw red rectangle behind the timer
But this means that the program will draw the red rectangle over and over again, would be better if it drew it only once.

estmote: Thank you for interest.

Pushing a button gives timer1 value: timer1=millis()+3600000;

That's not in the code you posted above. I'm not a mind reader. I suggest you look at the 'blink without delay' example in the IDE and also 'Finite State Machine'.

Sorry, but I posted the whole code and it is in the code I posted, so I am not assuming that anybody is a mind reader. Thanks for advice will look at these examples once more.

I got the code doing what I needed.