Making A countup timer upto 999 minute

Hello everybody As I'm new to programming i'm trying to make 999 minute countup timer here is my code here i'm just trying to add minute after every 59th second as u can see there is if loop which increment the minute but the problem is at the 59th second the loop execute but fails to increment the minute.

unsigned long start, finished, elapsed;

void setup()
{
Serial.begin(9600);
}

void result()
{
float s, ms;
int m=0;
unsigned long over;
elapsed = finished;
elapsed = elapsed % 60000;
s = int(elapsed / 1000);
if(s == 59)
{
m = m++;
Serial.print(m);
Serial.print(" M ");
}
Serial.print(s,0);
Serial.println(" S");
}
void loop()
{

finished = millis();
result();

}

Firstly you are declaring m each time the result() function is called. If you want to do this then declare it static to preserve its value between calls

  static int m = 0;
    m = m++;

There is no telling what the result of this will be. Use
m++;instead

In addition, you are only printing the minute value if s equals 59
Why not print the minute value unconditionally ?

For extra bonus points, change teh code so that it only prints when the value of s changes

I also notice that m will be rapidly incremented whilst s is 59. You should only increment m when s becomes 59. Actually I think it should be when s becomes zero,

yes the m is rapidly incrementing. and if i increment the value of m when s becomes zero the problem will be at the very first when program run it will execute the loop without completing the 60 second cycle

if i increment the value of m when s becomes zero the problem will be at the very first when program run it will execute the loop without completing the 60 second cycle

Think about that. If s is declared with a value of zero and you only increment m when s becomes zero then that problem will not arise.

Do you understand the difference between s becoming zero and s being zero ?

You should use int variables. And not declare variables that you do not use.

Try to understand this example (not tested :confused: ):

unsigned long chrono = 0;
int seconds = 0;
int minutes = 0;

void setup() {
  chrono = millis();
  Serial.begin (115200);
}

void loop() {
  if (millis() - chrono >= 1000) {
    chrono = millis();
    result();
  }
  // you can do other things here
}

void result() {
  seconds++;
  if (seconds == 60) {
    seconds = 0;
    minutes ++;
  }
  Serial.print (minutes);
  Serial.print (" M ");
  Serial.print (seconds);
  Serial.println(" S");
}

And set your serial monitor to 115200 bits/s.

@OP

Try the following Time Counter which has been tested in Arduino UNO. This sketch uses 1-sec TimeTick generated by TC1 in CTC Mode.

byte timeTick;
byte arrayTime[] = {0x00, 0x00, 0x00}; //highByte, ..., lowByte
int elapsedTime;

void setup()
{
  Serial.begin(115200);
  TCCR1A = 0x00;
  OCR1A = 0xF424;   //1-sec time parameter at clkTC1 = 16 MHz/256
  TCNT1 = 0x0000;
  TCCR1B = 0x0C;   //CTC Mode; Compare Match-A occurs at every 1-sec interval
}

void loop()
{
  do
  {
    ;    //wait until match occurs between TCNt1 and OCR1A by 1-sec time
  }
  while (bitRead(TIFR1, 1) != HIGH);
  bitSet(TIFR1, 1);   //reset OCF1A flag
  timeTick++;
  if (timeTick == 60) //1-min has elapsed
  {
    timeTick = 0;
    arrayTime[2]++;
    if (arrayTime[2] == 10)
    {
      arrayTime[2] = 0;
      arrayTime[1]++;
      if (arrayTime[1] == 10)
      {
        arrayTime[1] = 0;
        arrayTime[0]++;
      }
      if (arrayTime[0] == 10)
      {
        arrayTime[0] = 0;
      }
    }
  }
  elapsedTime = arrayTime[0] << 8 | arrayTime[1] << 4 | arrayTime[2];
  if (arrayTime[0] == 0x00)
  {
    Serial.print('0');   //print leading 0
    if (arrayTime[1] == 0x00)
      Serial.print('0');
  }
  Serial.println(elapsedTime, HEX);  //0 to 9 is the subset of hex.
}

sm119.png

sm119.png

Thank You All :slight_smile: