[SOLVED] need help with second counting algorithm

I'm wanting to display seconds on a 4x7 segment display that normally shows HH:MM. I only want to display the seconds for 30 seconds. There is no delay in the loop and I don't want one because the ATmega328P needs to be zooming along doing other things.

So from the main loop() I wanted to call this function showSeconds() (many, many times since there is no delay) until 30 seconds have been displayed, then stop calling the function. I'll trigger the call to showSeconds() with an external event, that part I have no problem with.

But the algorithm to show then stop showing the seconds is evading me. I'm willing to use global variables or whatever is required.

I cannot simply increment/decrement a counter, because the showSeconds() function will be called many times per second. Maybe I could use some global counters?

The following code won't work, but it could serve as a starting point:

void showSeconds()
{
  int currentSecond=0;
  int duration=0;

  getDateDs1307();  // gets the current time, populates global 'second' with current second, 0 to 59

  currentSecond = second;
  duration = currentSecond - 30;  // want to display seconds for 30 seconds
  
  while(duration++ <= currentSecond) {
     displaySeconds(); // an external function that shows current seconds on the display
  }

}

Thanks for any help.

Jake

There is no delay in the loop

Yes, there is:

  while(duration++ <= currentSecond) {
     displaySeconds(); // an external function that shows current seconds on the display
  }

You want to make showSeconds() just show the current time, as seconds. The call to that function should be dependent on the time. Exactly how to do that depends on code you didn't post.

As I wrote in my original post, "The following code won't work, but it could serve as a starting point:"

I don't have any additional code to show. My objective to to stop calling the showSeconds() function after it has displayed digits for 30 seconds.

Fine. So, you need to determine the time (currentSeconds and currentHours and currentMinutes).

If you are going to show seconds for 30 seconds, that would be when the value is between 0 and 30. It really should not be too difficult to determine whether you need to call showSeconds(), or not.

Of course, expressed this way, perhaps you can see the flaw in your logic to display seconds for 30 seconds at a time.

This is some pseudo code. This should work I think:

if(event triggered) {
  showUntil = current hour, current minute, current second + 30;
}

if( showUntil >= (current hour, current minute, current second) ) {
	displaySeconds();
}

If I can get the seconds elapsed since the epoch from the DS1307, the above should be easy enough to code.

I might be missing something, but, it seems to me that it makes more sense to display hour and minute for some time, then display second for some time. If you swap back and forth every 250 milliseconds, it's easy to use millis() to see if it's time to display the other value.

My clock normally displays HH:MM all of the time. I wanted to be able and have an event that would completely clear the display and show the seconds tick off for some pre-determined duration (currently 30 seconds).

I may be able come up with some simpler code by using/referencing millis(), i.e. working it into the pseudo code above in place of current hour, current minute, current second.

I wanted to be able and have an event that would completely clear the display and show the seconds tick off for some pre-determined duration (currently 30 seconds).

OK. I was thinking that you wanted to show hours and minutes for 30 seconds, then show seconds (counting up) for 30 seconds.

I've got it figured out. I'm justing recording the millis() at the time of the event, adding 30000 and comparing that to the current millis() so I know when to stop showing seconds.