convert value to hour minute second form , decrement and display it.

how to convert a value to hour minute second form , and than display it with decrement the second, minute, and hour.

here is my code

please correct me , its not working properly.

hr = total_time_in_secs/3600; t = total_time_in_secs%3600; minn = t/60; secs = t%60;

for( secs; secs >0; secs--){

tft.setTextSize(3); tft.setTextColor(GREEN); tft.setCursor (240, 133); tft.print(secs); tft.setCursor (280, 133); tft.print("S"); Serial.println(secs); delay(1000); }

for (minn; minn >0; minn--) {

tft.setTextSize(3); tft.setTextColor(GREEN); tft.setCursor (150, 133); tft.print(minn); tft.setCursor (187, 133); tft.print("M"); Serial.println(minn); delay(60000); } for ( hr; hr >0; hr--) {

tft.setTextSize(3); tft.setTextColor(GREEN); tft.setCursor (100, 133); tft.print(hr); tft.setCursor (130, 133); tft.print("H"); Serial.println(hr); delay(3200000); }

its not working properly.

It's not complete. It's not posted properly. It does something that you haven't explained. You want it to do something that you haven't explained.

please correct me

Not likely.


You need to get your calculator fixed.

this is my logic, but :( it did not work.

i have to display down counter for any value, value is in second and i have to display that value in form of hour, minute and second form and it should decrement, every second.

the value( seconds) i got is the total time to run stepper motor. when the counter will stop , i mean when hour = 00; minute = 00 and second = 00; then stepper motor should stop.

but :( it did not work.

It did something. What did it do?

You expected it to do something. What did you expect it to do?

After delaying for one second, showing new data until the seconds count down to 0, THINK about what should happen then. Here's a hint. You do NOT delay for one minute. When the minutes count down to 0, THINK about what should happen then. Here's a hint. You do NOT delay for one hour.

Instead of breaking it apart and then trying to decrement it, decrement the original value and break it apart only as you display it.

i tried many times and with different logic , with for loop, while, do while, all of that , but still it did not work.

please guide me.

As said above, please post ALL your code. This is not all your code.

Also, please read the “How to post to the forum” thread. You need to post your code inside “code” tags. That thread teaches you how to do that. When you do it correctly, your post should look like this:

void loop()
   ... more code here

this is my code .
i have something in function " time_remaining()" so that it display dectement the value in form of hour,minute and second.

Final_Syringe_Pump_Program_v2.ino (50.1 KB)

Thank you for posting the whole code. That is good.

So, the first thing I notice is that you are not really using the loop() functionality. You have code in loop, but at the end you say "exit". If you are going to code this way, all of your code should be in setup() instead. You may want to re-think your design such that it makes use of the idea of "loop".

The idea of "loop" is that you keep coming back though that section. So, you often use it for things like, waiting until some time has passed before running a function. This can happen in parallel with other waiting tasks, as each one is waiting for a different period. Look at the use of the "millis" function in the Tutorials. You can also use it for waiting for keys while doing other things, etc.

Your code as it is in "loop" is very sequential. I assume some parts work correctly. What line of the loop is the part where it does not work as you expect. What does it do? What do you want it to do?

Ahh. time_remaining(). This is not a good way to do this.

Remove your hr, t, min, secs variables entirely from your sketch, and remove the time_remaining() function. When you want to know the time from startup, just call millis() and calculate the h/m/s in realtime. This will be more accurate, and remove your delays, which would be causing problems in your design as well. See the "millis" function, and examples.

thanks very much .

can you give me an example.. i dont know how to do with it. i used milli() but it still not show hh:mm:ss form.

can you give me an example.. i dont know how to do with it. i used milli() but it still not show hh:mm:ss form.

It is really trivially simple. You have some value, that represents the time remaining. Periodically, you check to see if it is time to decrement the value. Periodically means "on every pass through loop()". Checking the time means knowing what time it is now (millis() is used for that) and what time it was the last time you decremented (millis() is used for that, too, recorded in a variable when that happened).

Each time you decrement the value, you need to update the display.

Suppose that the value is 5423. 5423 / 3600 is 1 and 5423 % 3600 is 1823. 1823 / 60 is 30, and 1823 % 60 is 23. So, 5423 means 1 hour, 30 minutes, and 23 seconds.

One second later, the value decrements to 5422, which means 1 hour, 30 minutes, and 22 seconds.

22 seconds later, the value is 5400, which means 1 hour, 30 minutes, and 0 seconds.

One second later, the value is 5399, which means 1 hour, 29 minutes, and 59 seconds.

Hopefully, you can see that you do not just keep subtracting seconds until the seconds value is 0, and then start subtracting minutes, then hours, when minutes gets to 0 (as your posted code does).

i modify my code by this , i got the conversion of hh:mm:ss but that is not correct, no matter the value it show every time 18:12:14, and the amazing thing is that it will stop after correct value.

Final_Syringe_Pump_Program_v2.ino (53.2 KB)

    if (key == 'A')
      if (key == 'A')

I wonder what the chances are that the second if statement will ever evaluate to false.

  minutes = t/60;
  minutes = t%60;

Hmmm... You need to explain what you think you are doing here.

  Clock = (hours * 3600) + (minutes * 60) + (seconds % 60);

Same here.

Seems to me you have WAY to much code dealing with time. Create a SIMPLE sketch that does not 427 variables related to time, that illustrates the problem you have having.

Here's a hint:

18:12:14 = 65536 seconds

And, your code would be much easier to follow and debug if you used parameters instead of global variables. Look up parameter passing and start to use them.

I was playing with PaulS idea of how to covert a number in seconds in to a count down clock and it works

how you enter a time would have to be converted in to seconds but that’s not to hard. (even updated one of my sketchs to this as its seems better then the way I was approaching it)

example code

unsigned long previousMillis = 0;
unsigned long interval = 1000;
unsigned long count_down = 7200;
unsigned long hours;
unsigned long mins;
unsigned long seconds;
byte hold = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin (9600);

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long currentMillis = millis();//one second into timers
  if (currentMillis - previousMillis > interval) {
    if (hold != 1) {
    previousMillis = currentMillis;

  hours = count_down / 3600;
  mins = ((count_down % 3600) / 60);
  seconds = ((count_down % 3600) % 60);

  if ((hours == 0) && (mins == 0) && (seconds == 0)) {
    Serial.println (" done ");
    hold = 1;
  else {
    if (hours < 10) {
      Serial.print(" ");
    Serial.print (hours);
    Serial.print (":");
    if (mins < 10) {
    Serial.print (mins);
    Serial.print (":");
    if (seconds < 10) {
    Serial.println (seconds);

yes you are right @arduinodlb , i have taken this countdown code from arduino website, and it will not take seconds as my value, what should i do so that the program will take my value as second , instead 65536 seconds??

hey @ gpop1

i have modify my code as your suggestion , but it still not work , the time is not going to decrement , like if time is 1:39:67 then after second it will remain same as 1:39:67 . no change.

i am attaching my code.

Final_Syringe_Pump_Program_v2.ino (53.2 KB)

Honestly, your code is extremely difficult to follow and debug.

This will be a lot easier to solve for you and us if you do a few things: 1) Move all variables that are not needed by more than one function into the function itself.

The problem with global variables is that anyone can access them, so when we are trying to find problems, we think: oh, anyone can change that. So, remove as many global variables as possible and make them local variables.

For example:

You have a global variable called "_micro". It is only used in the function "CountDownTimer". So, delete it, and declare it in the function "CountDownTimer" like this:

boolean CountDownTimer()
    unsigned long _micro;

Do this for every variable that is not accessed by more than one function.

  1. Use parameter passing. Use parameter passing a LOT. Even when you have a global variable, it is often better to pass it as a parameter so you can check the function separately from the rest of the program. And it makes debugging easier. Also, use "return" values from functions in the same way, or declare your functions as void if there are no return values.

For example:

unsigned long time_remaining()

should be:

void time_remaining(int time_in_secs)
  hours = time_in_secs/3600;
  t = time_in_secs%3600;
  minutes = t/60;
  minutes = t%60;

  1. Use function names that do what they say.

For example, ShowHours() does not print anything. It should be called GetHours().

  1. Remove unused functions.

The less code you and we have to look at, the quicker it is to find the problem.

  1. Use prints to find out where the actual problem is.

Are you sure your Count variable is actually counting down correctly? Print it out every time it gets decremented to be sure. Keep using prints until you work out where the problem actually is.

Please spend some time going through and cleaning up your code like this. You will benefit in understanding how to write well structured code, and it will make it much easier for us to help you.

thanks a lot man ,

i use your idea of millis() , it worked , but not for more value , when the value is more like 4400 seconds the counter will not decrement and the hh:mm:ss value will wrong.

it is working for lower value like 10 seconds or 2 minutes .

Final_Syringe_Pump_Program_v2.ino (53.2 KB)