Ok so I have a common cathode 4 digit 7 segment LED display. (No colon). I want to make it a countdown timer. For example if it was set to 10 minutes and 30 seconds it would be 10.30. I downloaded a library to control the display. The thing is, is that when it counts down the seconds start at 99 instead of 60 and I have no idea how to change it.
#include "SevSeg.h"
SevSeg sevseg;
unsigned long timer;
int CentSec=500; //100 is one second
int led = A2;
Posting your code correctly would be good.
Posting a link to the library you are using would be good.
Defining what you are setting to 10.30 would be good.
It appears as though your code doesn't understand that some value represents, in YOUR mind, time in minutes and seconds.
So, when it subtracts one from the value, it thinks it's doing the right thing. Clearly, you think that when the value is a multiple of 100, it should subtract 41. Otherwise, it should subtract 1.
The easiest modification to understand is to keep your seconds and minutes in separate variables. When seconds gets decremented to 0, you reset it to 59 and decrement the minutes. You can put them together for the 7seg library to display with some multiplication (displaynum = min*100 + sec;)
Even easier - keep your tens_minutes, ones_minutes, tens_seconds, ones_seconds in seperate variables. Then the data is all set for sending to the drivers for the separate digits.
Here is a section of code I have that does that:
// ***********************************************************************************************
// Loop here endlessly, checking if time or score needs updating, if wireless message came in,
// if touch lights are on
// ***********************************************************************************************
void loop()
{
// check if time needs updating
if ((time_running == 1) || (time_running == 2)) // fencing time is counting down or delay time is counting down
{
unsigned long currentMillis = millis(); // see how long its been
if (currentMillis - previousMillis >= interval) // more than our quarter second interval?
{
// save the last time we okayed time updates
previousMillis = currentMillis;
quarter_interval = quarter_interval+1;
// cycle the colon state
if (colon == 0x80)
{
colon = 0x00;
}
else
{
colon = 0x80;
}
update_time = 1; // enable time display to be updated
if (quarter_interval == 4) // we hit the one second update time
{
quarter_interval = 0;
// update the time digits
// cases:
// 0:01, final second - stop time, disable touch lights, sound buzzer
// Tens of seconds rollover: time = x:50, x:40, x:30, x:20, x:10: decrement tens of seconds, rollover seconds to 9
// Minutes rollover: time = 9:00, 8:00, etc. 2:00, 1:00: decrement ones of minutes, rollover tens of
// seconds to 5, ones of seconds to 9
// 10:00: Roll all the digits over
// otherwise: just roll over the seconds
// Case: Final Second
if ((minutes_ones == 0) && (seconds_tens == 0) && (seconds_ones == 1)) // don't need minutes_tens, can't have 10:01
{
touchlight_enable = 0; // score touches are locked out
time_running = 0; // stop time running
seconds_ones = 0; // clear the last second
updated = 1; // fake a Case complete flag
buzzer = 1; // add a buzzer for this: end of time buzzer sounds
if ((time_running == 1) && (period>0) && (period<9)) { // update period counter if was fencing time, not 1:00 or 10:00 break
period = period +1;
update_period=1; // enable period display to be updated
}
} // end of if final second
// Case: x:50, x:40, x:30, x:20, x:10
if ((seconds_tens >0) && (seconds_ones == 0)) // case for the last tens of seconds
{
seconds_tens = seconds_tens - 1; // decrement the tens
seconds_ones = 9; // rollover the ones
updated = 1;
} // end of if 10 of seconds rollover
// Case: 9:00, 8:00, etc 2:00, 1:00
if ((minutes_ones > 0) && (seconds_tens == 0) && (seconds_ones == 0)) // case for the last ones of minutes
{
minutes_tens = 0x00; //
minutes_ones = minutes_ones - 1; // decrement the minutes
seconds_tens = 5; // rollover the tens of seconds;
seconds_ones = 9; // rollover the ones of seconds;
updated = 1;
} // end of if minutes rollover
// Case: starting from 10:00
if (minutes_tens == 0x01) // roll over all digits
{
minutes_tens = 0x00; // rollover the tens of minutes
minutes_ones = 9; // rollover the ones of mints;
seconds_tens = 5; // rollover the tens of seconds;
seconds_ones = 9; // rollover the ones of seconds;
updated = 1;
} // end of if 10:00 rollover
// General Case: just decrement the seconds
if (updated == 0) // nothing else updated - but don't decrement if = 0.
{
seconds_ones = seconds_ones - 1;
}
updated = 0; // reset for next pass thru
} // end of if quarter_interval
} // end of reaching our interval
} // end of if time_running
else
{
update_time = 0; // no time update this time around - probably don't need this
}