8 years, 2 months ago.

i am not able to understand the error in code.

#include "mbed.h"
#include "TextLCD.h"
TextLCD lcd(A0, A1, A2, A3, A4, A5);
DigitalIn pin_delay( D11);                       //...........i want to set delaytime by a button press

Timer delaytime;

//float delaytim;

int main() 
{
  int time = 0;
  
  pin_delay.mode(PullDown);


        lcd.cls();
        lcd.locate(3,0);
        lcd.printf("SET DELAY");
        
       do{
            delaytime.start();
            time = delaytime.read();
            lcd.cls();
             lcd.locate(1,0);
             lcd.printf("DELAY = %1.2f mn", delaytime.read()*10);
              
              }while(pin_delay==1);
              delaytime.stop();  
           return delaytime.read();
             
            while (time > 0){
                lcd.cls();
             lcd.locate(1,0);
             lcd.printf("DELAY = %1.2f mn", time*10);
                wait(60);
                time--;
            
            }
                delaytime.reset();
            
        }
        
        

Question relating to:

Timer demo

1 Answer

8 years, 2 months ago.

The ''return delaytime.read();'' will probably end your main() and stop the whole program.

Im not sure what you try to do, but you probably want to set a timer value by pressing and holding the button and then start the timer to wait for the set delay time.

Something like this

while (true) {
  time = 0;
  // increment the delaytime in steps of 1 min while holding the button
  do{
        time++;
        lcd.cls();
        lcd.locate(1,0);
        lcd.printf("SET = %d min", time);
        wait(1);                   
   } while(pin_delay == 1);

   //wait for set delay time
   while (time > 0){
       lcd.cls();
       lcd.locate(1,0);
       lcd.printf("DELAY = %d min", time);
       wait(60);
       time--;           
     }
 }           

You can replace the while (time>0) loop by code that uses the mbed Timer instead of wait(). This could allow you to do some other work instead of just waiting.

Accepted Answer

thank you very much. though I change do while to while to make it work fine.

posted by jayendra mishra 18 Feb 2016