Pages: 1 [2]   Go Down
Author Topic: Trouble with programming logic  (Read 855 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, you need return. You also could use more Serial.print() statements to determine what is going on.

Finally, you need to comment the count() function better (after changing to while statements), and explain what you want the function to do, when.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have to count down from what maximum?
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, in the interest of indirect help see if this makes any sense to you.
TIme has been scaled per your earlier statement of 1 second equals 1 minute (or so I interpreted it as - if not change the value of 'ONE_SECOND').

Code:
const unsigned long ONE_SECOND  = 16UL;
const unsigned long ONE_MINUTE  = 60 * ONE_SECOND;
const unsigned long ONE_HOUR    = 60 * ONE_MINUTE;

unsigned long msTarget;

unsigned long hms2millisecs(uint8_t const hours, uint8_t const minutes, uint8_t const seconds)
{
    return ((hours * ONE_HOUR) + (minutes * ONE_MINUTE) + (seconds * ONE_SECOND));
}

void millisecs2hms(unsigned long milliseconds, uint8_t* hours, uint8_t* minutes, uint8_t* seconds)
{
    uint8_t h = milliseconds / ONE_HOUR;        milliseconds -= h * ONE_HOUR;       *hours      = h;
    uint8_t m = milliseconds / ONE_MINUTE;      milliseconds -= m * ONE_MINUTE;     *minutes    = m;
    uint8_t s = milliseconds / ONE_SECOND;      milliseconds -= s * ONE_SECOND;     *seconds    = s;
}

void loop()
{
    uint8_t     hours;
    uint8_t     minutes;
    uint8_t     seconds;
    long        remaining = msTarget - millis();
    millisecs2hms(remaining, &hours, &minutes, &seconds);

    Serial.print((short)hours);
    Serial.print(":");
    Serial.print((short)minutes);
    Serial.print(":");
    Serial.println((short)seconds);

    if ( remaining <= 0 )
    {
        // we've reached zero time
        while (true)
        {    }
    }
}

void setup()
{
    Serial.begin(9600);
    
    msTarget = millis() + hms2millisecs(1, 50, 54); // hours, minutes, seconds
}
« Last Edit: January 21, 2013, 01:36:14 am by lloyddean » Logged

Pages: 1 [2]   Go Up
Jump to: