Making A Timer with Millis

Hello, i am trying to make a timer. Its simply record a time until it starts.
There are two variable one of them x and other one y.
'x' will be second and 'y' will be minutes.
When 'x' comes to 60, it will add to 'y' variable 1 value and also it will reset hisself and will begin from 0 again.
This is my code. I cant make it happen to other part of it..

unsigned long sMillis;
unsigned long cMillis;
const unsigned long px = 1000;
int x = 0;
int y = 0;
void setup() {
  Serial.begin(9600);  
  sMillis = millis();
}
 
void loop() {
cMillis = millis();
if (cMillis - sMillis >= px) {
  Serial.println(x);
  x++;
  sMillis = cMillis;  
  } 
}

when you read your description, you should see, that nothing is done for "y" - except set to 0.
Your sketch counts the seconds, that's all ...

Good luck to fulfill your description!

SlimeDont5:
Hello, i am trying to make a timer. Its simply record a time until it starts.
There are two variable one of them x and other one y.
'x' will be second and 'y' will be minutes.
When 'x' comes to 60, it will add to 'y' variable 1 value and also it will reset hisself and will begin from 0 again.
This is my code. I cant make it happen to other part of it..

Why not call "x" something more meaningful? Like maybe "seconds".
Why not call "y" something more meaningful? Like maybe "minutes".
Why is "x" declared as int? Does it ever get bigger than 60? Is it ever less than zero? It could be unsigned byte (also known by other names).
Why is "y" declared as int? Is it ever less than zero? It could be unsigned int (but that is a LOT of minutes!).

"I cant make it happen to other part of it." WHAT can you not make happen to WHAT other part of WHAT?

"When 'x' comes to 60, it will add to 'y' variable 1 value and also it will reset hisself and will begin from 0 again." No, it doesn't.

Well, you're close.

unsigned long sMillis;
unsigned long cMillis;
const unsigned long px = 1000;
int x = 0;
int y = 0;

void setup() 
{
  Serial.begin(9600); 
  sMillis = millis();
}
 
void loop() 
{
    cMillis = millis();
    if (cMillis - sMillis >= px) 
    {        
        x++;
        if( x == 60 )
        {
            y++;
            x=0;
        }
        sMillis = cMillis; 

        Serial.print(y); Serial.print(":");Serial.println(x);
    }
}

Its clearly seen which part I CANT. This is why i wrote I COULDNT. If i could, i wouldnt want to help.

By the way, thank you so much Blackfin. Thats what i am trying to do. :slight_smile:

SlimeDont5:
Its clearly seen which part I CANT. This is why i wrote I COULDNT.

Sorry. It is probably very clear in your mind but it is not at all clear in your description.

Using single-letter variable names is poor practice. It is almost impossible to identify all the instances of a variable with a single-letter name because the search tool just finds every instance of that letter.

The demo Several Things at a Time illustrates the use of millis() to manage timing. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

Robin2:
Sorry. It is probably very clear in your mind but it is not at all clear in your description.

Using single-letter variable names is poor practice. It is almost impossible to identify all the instances of a variable with a single-letter name because the search tool just finds every instance of that letter.

The demo Several Things at a Time illustrates the use of millis() to manage timing. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

Thank you for your respond. I am looking at them too. I am trying to understand the logic behind it.