How can I create a countdown using millis() ?


hey guys, trying countdown. now, i'm not using potentiometer set time. ok.
=( =(
my problem countdown doesn't work expecting. seems counting every 1 ms, it's wrong becouse millis()/1000. supposed go every 1 sec.

code: [select]

int min = 0;
int sec = 300;

int starttime = 0;

void setup(){  
 serial.begin(9600);
}
//----------------------------------------------------
void loop(){
 starttime = sec;
 if(sec > 60)
 {
   min = floor(sec/60);
   if(sec%60 != 0){
     sec = sec- min;
   }
   else
   {
     sec = 59;
   }
 }
 while(min >= 0){
   while(millis()/1000 < starttime)
   {
     sec--;
     serial.println(min*100+sec);
   }
   min--;
   sec = 59;
 }
}


it's important know can't stop code becouse i'm trying put time on 4x7segment bcd. so, can't use delay().
does can me ?

express time count down in seconds,

code: (not tested) [select]

uint16_t seconds = 3142;      // max == 65535
uint32_t lasttime;

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

  lasttime = millis();
  while (seconds > 0)
  {
    if (millis() - lasttime >= 1000)
    {
       seconds--;
       lasttime += 1000;
       display(seconds);
        if (seconds == 0) alarm();
     }
  }
}

loop()
{
}

void display (uint16_t sec)
{
   int hr = sec/3600;
   sec = sec - hr * 3600;
   int mi = sec/60;
   sec = sec - mi*60;
   
   // print hr:mi:sec
}

void alarm()
{
}




Arduino Forum > Using Arduino > Programming Questions > How can I create a countdown using millis() ?


arduino

Comments

Popular posts from this blog

Connecting Raspberry Pi 2 to P10(1R)-V706 LED Dot Matrix - Raspberry Pi Forums

TypeError: <unknown> is not a numpy array - Raspberry Pi Forums

datso and removing imagetitle - Joomla! Forum - community, help and support