Go Down

Topic: Trouble with programming logic (Read 1 time) previous topic - next topic

PaulS

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.

lloyddean

You have to count down from what maximum?

lloyddean

#17
Jan 21, 2013, 03:16 am Last Edit: Jan 21, 2013, 07:36 am by lloyddean Reason: 1
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: [Select]

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
}

Go Up