Arduino Timer count resets at 65 but it should reset at 70

I'm trying to display a timer which counts up to 70seconds however once it reaches 65, it restarts (loop). This happened after I added ' basetime=millis(); ' and ' currtime = millis()-basetime; ' . (It works when I remove those two but I added because I want the millis() to be reset to zero once it hits 70seconds). Anybody able to help me on this? I am using arduino Mega 2560

#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_RA8875.h" 
#include "max6675.h"

// Library only supports hardware SPI at this time
// Connect SCLK to MEGA Digital #52 (Hardware SPI clock)
// Connect MISO to MEGA Digital #50 (Hardware SPI MISO)
// Connect MOSI to MEGA Digital #51 (Hardware SPI MOSI)
#define RA8875_INT 255//Not used #3
#define RA8875_CS 53
#define RA8875_RESET 255//Not used #13 

int ktcSO = 48;
int ktcCS = 47;
int ktcCLK = 46;
MAX6675 ktc(ktcCLK, ktcCS, ktcSO);
int t;



Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET);
uint16_t tx, ty, t2x,t2y;


uint8_t minutes, seconds ,seconds2, hours, basetime, currtime;
char timeline[30];

void setup()
{
  Serial.begin(9600);
  Serial.println("------------------------------------------------");
  Serial.println("RA8875 start");

  /* Initialise the display using 'RA8875_480x272' or 'RA8875_800x480' */
  if (!tft.begin(RA8875_800x480)) {
    Serial.println("RA8875 Not Found!");
    while (1);
    
 }

  Serial.println("Found RA8875");

  tft.displayOn(true);
  tft.GPIOX(true);      // Enable TFT - display enable tied to GPIOX
  tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
  tft.PWM1out(255);

   
  // With hardware accelleration this is instant
  tft.fillScreen(RA8875_WHITE);
  
  tft.textMode();
    //tft.cursorBlink(32);
   
 tft.touchEnable(true);
 pinMode(8, OUTPUT); 

}

void loop()

{
  basetime=millis();
  seconds=0;
  seconds2=0;
  minutes=0;
 tft.drawRect(0, 60, 400, 420, RA8875_GREEN);
 //tft.fillRect(0, 0, 400, 240, RA8875_GREEN); 
 tft.drawRect(400, 60, 400, 420, RA8875_BLUE);
 //tft.fillRect(400, 0, 400, 420, RA8875_BLUE);
 
  
 tft.textSetCursor(400,10);
 tft.textTransparent(RA8875_BLACK);
 tft.textEnlarge(1);
 tft.print("FAN");
 tft.textSetCursor(200,240);
  tft.print("ON");
 tft.textSetCursor(600,240);
  tft.print("OFF");
  
  if (tft.touched())
  {
 tft.touchRead(&t2x, &t2y);
 Serial.print(t2x); Serial.print(", "); Serial.println(t2y);
 if((t2y>=50 && t2y<=1000 )&&(t2x>=0 && t2x <= 490))
 { 
  tft.fillScreen(RA8875_WHITE); 

    for( seconds==0; seconds<=70; seconds++)
   
      { 
            digitalWrite(8, HIGH);
            time();      
       
          }
          digitalWrite(8, LOW);
      tft.touchRead(&tx, &ty);
       tft.fillScreen(RA8875_WHITE); 
       }
       else if ((t2y>=50 && t2y<=1000 )&&(t2x>=500 && t2x <= 1000))
       {
        tft.fillScreen(RA8875_WHITE); 
  for( seconds==0; seconds<=70 ;seconds++)
          { 
           
            time();      
       
          }
         
      tft.touchRead(&tx, &ty);
       tft.fillScreen(RA8875_WHITE); 
       }
  }
}

void time()
{
  currtime = millis()-basetime;
  tft.textSetCursor(100, 100);
  tft.textColor(RA8875_WHITE, RA8875_RED);
  sprintf(timeline,"%02d hours %02d mins %02d secs",hours, minutes, seconds2);
  tft.print(timeline);
  delay(1000);

  tft.textSetCursor(200, 300);
  tft.textColor(RA8875_WHITE, RA8875_RED);
  tft.print("Deg C = "); 
  tft.print(ktc.readCelsius());
  delay(1000);
  
   seconds = currtime /1000;
   minutes = (currtime/1000 ) / 60;
   seconds2 = seconds - (minutes * 60);
   //minutes = minutes - (hours * 60);
   //hours = ( ( millis()/1000 ) / 60 ) / 60;
   
}

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Please post your COMPLETE code.

What model Arduino are you using?
Have you written your code in stages?
If so have you got code that just tests the timer, as this would have been one of the stages as you developed your code?

Thanks.. Tom.. :slight_smile:

Let me know if my full code is needed

It is

UKHeliBob:
It is

I have updated my full codes

      for ( seconds == 0; seconds <= 70; seconds++)

As a start you might want to review your for loops

TomGeorge:
Please post your COMPLETE code.

What model Arduino are you using?
Have you written your code in stages?
If so have you got code that just tests the timer, as this would have been one of the stages as you developed your code?

Thanks.. Tom.. :slight_smile:

I have updated my complete code and am using mega 2560.
I have tried writing my code in stages for example just running the time without the two lines and it runs perfectly fine.

without the two lines

Which two lines ?

UKHeliBob:

      for ( seconds == 0; seconds <= 70; seconds++)

As a start you might want to review your for loops

This is my code thats just for the timer it runs for 70sec and stops. The 2 line that i added are basetime=millis(); and

  currtime = millis()-basetime;
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_RA8875.h"



// Library only supports hardware SPI at this time
// Connect SCLK to MEGA Digital #52 (Hardware SPI clock)
// Connect MISO to MEGA Digital #50 (Hardware SPI MISO)
// Connect MOSI to MEGA Digital #51 (Hardware SPI MOSI)
#define RA8875_INT 255//Not used
#define RA8875_CS 53
#define RA8875_RESET 255//Not used

Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET);
uint16_t tx, ty;



signed short minutes, seconds, seconds2, hours;
char timeline[30];


void setup() {
  Serial.begin(9600);
  Serial.println("------------------------------------------------");
  Serial.println("RA8875 start");

  /* Initialise the display using 'RA8875_480x272' or 'RA8875_800x480' */
  if (!tft.begin(RA8875_800x480)) {
    Serial.println("RA8875 Not Found!");
    while (1);
  }

  Serial.println("Found RA8875");

  tft.displayOn(true);
  tft.GPIOX(true);      // Enable TFT - display enable tied to GPIOX
  tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
  tft.PWM1out(255);
   tft.textMode();

  // With hardware accelleration this is instant
  tft.fillScreen(RA8875_WHITE);
  pinMode(RA8875_INT, INPUT);
  digitalWrite(RA8875_INT, HIGH);
   tft.cursorBlink(32);
  
  tft.touchEnable(true);
    
  Serial.print("Status: "); Serial.println(tft.readStatus(), HEX);
  Serial.println("Waiting for touch events ...");
  tft.textSetCursor(100, 100);

  
  
}

void loop() {
 for( seconds== 0; seconds<=70 ;seconds++)
 {
 
  tft.textSetCursor(100, 100);
  tft.textColor(RA8875_WHITE, RA8875_RED);
  sprintf(timeline,"%0.2d hours %0.2d mins %0.2d secs",hours, minutes, seconds2);
  tft.print(timeline);
  delay(1000);
  
   seconds = millis()/1000;
   minutes = ( millis()/1000 ) / 60;
   seconds2 = seconds - (minutes * 60);
   minutes = minutes - (hours * 60);
   hours = ( ( millis()/1000 ) / 60 ) / 60;
  }
}

Hi,
Look at this link as to how to write a for statement.

Tom... :slight_smile:

TomGeorge:
Hi,
Look at this link as to how to write a for statement.

for - Arduino Reference

Tom... :slight_smile:

Hi Tom,
I've changed my for loop and tried running it again but the results are still the same

for( seconds=0; seconds<=70; seconds++)

I have lost track of which version of the code you are testing and how you know that the loop does not run as expected

Hi,
Is this right, reassigning a value to the for loop variable?

for( seconds== 0; seconds<=70 ;seconds++)
{

tft.textSetCursor(100, 100);
tft.textColor(RA8875_WHITE, RA8875_RED);
sprintf(timeline,"%0.2d hours %0.2d mins %0.2d secs",hours, minutes, seconds2);
tft.print(timeline);
delay(1000);

seconds = millis()/1000;
minutes = ( millis()/1000 ) / 60;
seconds2 = seconds - (minutes * 60);
minutes = minutes - (hours * 60);
hours = ( ( millis()/1000 ) / 60 ) / 60;
}
}

Tom.... :slight_smile:

Hi,
I edited your code to the basics and the loop does not stop at 70.

unsigned long seconds;
unsigned long minutes;
unsigned long seconds2;
unsigned long hours;

void setup() {
  Serial.begin(115200);
  Serial.println("------------------------------------------------");
  Serial.println("RA8875 start");
}

void loop()
{
  for ( seconds = 0; seconds <= 70 ; seconds++)
  {
    delay(1000);
    seconds = millis() / 1000;
    minutes = ( millis() / 1000 ) / 60;
    seconds2 = seconds - (minutes * 60);
    minutes = minutes - (hours * 60);
    hours = ( ( millis() / 1000 ) / 60 ) / 60;
    Serial.print(hours);
    Serial.print(" : ");
    Serial.print(minutes);
    Serial.print(" : ");
    Serial.print(seconds2);
    Serial.print(" =======");
    Serial.println(seconds);
  }
}

Tom... :slight_smile:

TomGeorge:

unsigned long seconds;

unsigned long minutes;
unsigned long seconds2;
unsigned long hours;






Serial.print(hours);
    Serial.print(" : ");
    Serial.print(minutes);
    Serial.print(" : ");
    Serial.print(seconds2);
    Serial.print(" =======");
    Serial.println(seconds);

Hi Tom,

thank you so much. I found out that my problem was not with how i controlled millis(); but was with how I assigned my Variables. I used unsigned integer instead of unsigned long. Another problem was on how i printed my values. I changed my code and it works now. Thank you so much!!

  tft.print(hours);tft.print(" Hours ");
  tft.print(minutes);tft.print(" Minutes ");
  tft.print(seconds2);tft.print(" Seconds");

Have you fixed the syntax of the for loop ?
It is pure luck that it worked with the == instead of =

You may not be so lucky next time

UKHeliBob:
Have you fixed the syntax of the for loop ?
It is pure luck that it worked with the == instead of =

You may not be so lucky next time

Hi Bob,
I've changed all the == into =, thanks for your reminder.