Countdown timer

Hello guys, I am trying to do a countdown timer and what I have done so far is not working.

int second1, minute1, hours1; // declare time variables

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

}

void loop()
{
  unsigned long timer=99999999;
 if(!(timer ==0)){
  timer=--timer;
 seconds1=((timer % (1000*60*60)) % (1000*60)) / 1000;
 minutes1=(timer % (1000*60*60)) / (1000*60);
 hours1=(timer / (1000*60*60));
Serial.print(hours1);
 Serial.print(":");
 Serial.print(minutes1);
 Serial.print(":");
 Serial.println(seconds1);
}
}

Does the problem is caused by the variables?

Basically I want to have a specific value of milliseconds and convert it to HH:MM:SS

Your code won't compile because you have used 2 different names for the variables.
You declare second1 and minute1 as global variables then in the loop() function try to use variables called seconds1 and minutes1. Spot the difference.

As to whether your program works if you rectify this problem I will leave you to discover.

What is    timer=--timer;supposed to do ?

What will the value of timer be each time the loop() function starts ?

I want the value of timer to be incremented!! In each loop to decrease by one.

To decrement timer it is sufficient to say:

    --timer;

-br

If you want to increment it then usetimer++;
If you want to decrement it usetimer--;but you can’t do both.

In any case your  unsigned long timer=99999999;may mess up your plans.

NOTE - there is a difference between these 2 statements

--timer;
timer--;

It is subtle but will make a difference in some circumstances.

I know that the long variable will be annoying what could be able to do it in an alternative way? If it is possible?

Have a look at the BlinkWithoutDelay example in the IDE to see how to use millis() as a non blocking interval timer. It requires the use of unsigned longs but I don't see why that should annoy you.

In any case your
Code:
unsigned long timer=99999999;
may mess up your plans.

What is going to be the problem?

What will the value of timer the first time through the loop() function ?
What will the value of timer the second time through the loop() function ?
What will the value of timer the third time through the loop() function ?

Should the value be the same each time or different ?

Ah sorry! I want the value of that to decrement in each loop by one!

So don't declare and initialise it each time through loop()

But this is what I ve done. Or not?

void loop()
{
  unsigned long timer=99999999;
 if(!(timer ==0)){
  timer=--timer;

Each time through the loop() function

  unsigned long timer=99999999;

Create a new variable called timer and set its value to 99999999. Is that what you want to do ?

What they are saying is move this to the pre-setup area

unsigned long timer=99999999;

other wise it will only ever go down to 99999998, and then be reset to 99999999 when loop starts again.

Or change the if into a while

Got it guys! Changed the code:

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

}

void loop()
{
int seconds1;
int minutes1;
int hours1;
unsigned long timer=99999999;
 while(!(timer ==0)){
 --timer;
 seconds1=((timer % (1000*60*60)) % (1000*60)) / 1000;
 minutes1=(timer % (1000*60*60)) / (1000*60);
 hours1=(timer / (1000*60*60));
Serial.print(hours1);
 Serial.print(":");
 Serial.print(minutes1);
 Serial.print(":");
 Serial.println(seconds1);
}
}

But still I get:

1:0:-3
1:0:-3
1:0:-3
1:0:-3
1:0:-3
1:0:-3
1:0:-3
1:0:-3
1:0:-3
1:0:-3
1:0:-3
1:0:-3
1:0:-3

That's because you didn't get it:

unsigned long timer=99999999;
void setup()
{
Serial.begin(9600);

}

void loop()
{
int seconds1;
int minutes1;
int hours1;
unsigned long timer=99999999;
while(!(timer ==0)){
--timer;
seconds1=((timer % (10006060)) % (100060)) / 1000;
minutes1=(timer % (1000
6060)) / (100060);
hours1=(timer / (10006060));
Serial.print(hours1);
Serial.print(":");
Serial.print(minutes1);
Serial.print(":");
Serial.println(seconds1);
}
}

Actually, it's mostly an issue of overflowing the integers. Integers can only go up to 32767. Your 10006060 overflows that. Just put an L in there so it extends them to longs and you can move on to the next round of debugging.

 seconds1=((timer % (1000L*60*60)) % (1000L*60)) / 1000;
 minutes1=(timer % (1000L*60*60)) / (1000L*60);
 hours1=(timer / (1000L*60*60));