# Timer after button pressed

please format code with </> button * homework policy * asking questions

After I run a program, I want my timer to start. Currently I am using millis()/1000 just for the seconds but I want to make the timer so that it gives the minutes too. I know how to display it but i just need to know how i can make the millis update every 60 seconds so it goes back to 0. Thank you.

60 seconds = 6000 milliseconds so divide your milliseconds by 1000. Note you could take the modulus of millais ie if(millis%1000==0)doSomething(= but note you will have to take the starting time away or the initial time the button was pressed.

I have this currently:

``````int tElapsed =0;
int tStart = 0;
tElapsed = (millis()/1000)-tStart;
if (tElapsed> millis()/1000+60) {tElapsed = tStart;};
``````

im trying to set tElapsed to 0 every time millis() reaches 60 seconds

Here is the code I made : P

Code
``````void setup() {} void draw() {
int m = millis(), sec = floor(millis()/1000) % 60, min = floor(floor(millis()/1000)/60) % 60, hr = floor(floor(floor(millis()/1000)/60)/60) % 24, d = floor(floor(floor(floor(millis()/1000)/60)/60/24)),y = floor(floor(floor(floor(millis()/1000)/60)/60/24)/365);
println("Days:",nf(d,2),("(Y:"+y+",D:"+d%365+")"),"Hr:Min:Sec",nf(hr,2)+":"+nf(min,2)+":"+nf(sec,2));
}
`````` Days: [total number] (Y:[years],D:[days (%365)]) Hr:Min:Sec [hrs]:[min]:[sec]

1 Like

Thank you so much!!! This is EXACTLY what I needed!

Would you happen to know how I would be able to include the milliseconds too? Not just the full milliseconds, the second or second and third value of it? Because m/1000 gives seconds but m/100 wouldn’t give me what I want.

to print normal milliseconds just use `println(millis() % 1000);`

If I may explain further. For example: millis() = 1234.
The 1 is the seconds, the 2 is how fast the 1 changes, the 3 is how fast the 2 changes etc correct/? I would just like to obtain the value of 2.

``````  //12345/100 = 123
//123 - (123/10)*10 = 3

println( 12345/(10*10) - (12345/(10*10)/ 10)*10 );
``````

I took this basic idea and wrote a function to extract any digit.

Give it a try!

`:)`

1 Like

Thank you very much! It worked perfectly :))