9 years, 6 months ago.

I want to set an option for delay variable before startrun function? please help!!

it should ask the user to set time: if the user press delay button, it should set delay variable (only increment!), after that user can set his main running time of engine. when press start button, program should delay the startRun function for the set delay variable time. adn then resume startrun();

#include "mbed.h"
#include "TextLCD.h"

TextLCD lcd(A0, A1, A2, A3, A4, A5);
DigitalOut switchengine(PC_2);
DigitalIn pin_up(D6); 
DigitalIn pin_down(D7);
InterruptIn pin_start(D8);
InterruptIn pin_reset(D11);
InterruptIn pin_delay( D9);


Timeout quarter;      //timeout called every 15min when running
int Quarters;    //selected number of quarters
float runtime;            //currently set runtime interval in quarters
float runtimeleft;       //remaining runtime interval in quarters
void runtimeUpdate();
void displayUpdate();
void startRun() ;
bool status = false;
float delay;

//called when 15min has passed

void runtimeUpdate(){
   runtimeleft--;
   displayUpdate();
}
 //void delayfunc() //............delay function.
    
        
//update the display and start/stop the engine
void displayUpdate(){
 
 if (runtimeleft ==0){
   // we are done
   switchengine=0;
 
   //update display
   lcd.cls();
   lcd.locate(2,0);
   lcd.printf("ENGINE OFF");
     lcd.locate(0,1);
    lcd.printf("Rantime: %1.2f Hr", Quarters*15/60.0);
  
 }
 else  {
  //engine should be running
   switchengine=1;
 
  //update display first line
   
   if (runtime < 4){
     lcd.cls();
   lcd.locate(1,0); 
     lcd.printf("Time = %1.2f mn", (runtime*15));
   }
   else{
     lcd.cls();
   lcd.locate(1,0);
     lcd.printf("Time = %1.2f Hr", (runtime*15.0)/60.0);
   }
 
  //update display second line
   if(runtimeleft<4){

       lcd.locate(0,1);
       lcd.printf("Timeleft:%1.1f mn", runtimeleft*15);
       }
      else{ 

       lcd.locate(0,1);
   lcd.printf("Timeleft:%1.2f H", (runtimeleft*15.0)/60.0);
   }
 
  // now start timeout to call again in 15min  
  quarter.attach(&runtimeUpdate, 900);
  }  
}
 
 
// init and start the run
 void startRun() {
 runtime = Quarters;
 runtimeleft = runtime;
 
 lcd.cls();
 lcd.locate(1,0);
 lcd.printf("ENGINE START");
 wait(1);
 
 displayUpdate();
 status = !status;

}
  
       
int main() 
{
  int i = 0;
  float time;
  
  
  pin_up.mode(PullDown);
  pin_down.mode(PullDown);
  pin_start.mode(PullDown);
  pin_delay.mode(PullDown);
  pin_reset.mode(PullDown);
 
  
            lcd.cls();
        lcd.locate(3,0);
        lcd.printf("READY");
        
    do{
          if(pin_delay==1)
           {i= i++;
           wait(0.1);
           if (i>20)
            i=20;
            delay = i;
            if(i<4){
             lcd.cls();
             lcd.locate(1,0);
             lcd.printf("Time= %2.2f mn", delay*15);
           
                          }
             else{
                 lcd.cls();
                 lcd.locate(1,0);
             lcd.printf("Time= %1.2f Hr", delay*15/60);
             
           }
           }
            }while(pin_start==0);
           
             lcd.cls();
        lcd.locate(3,0);
        lcd.printf("SET TIME");
        
        
             do{
          if(pin_up==1)
           {i= i++;
           wait(0.1);
           if (i>200)
            i=200;
            time = i;
            if(i<4){
             lcd.cls();
             lcd.locate(1,0);
             lcd.printf("Time= %2.2f mn", time*15);
           
                          }
             else{
                 lcd.cls();
                 lcd.locate(1,0);
             lcd.printf("Time= %1.2f Hr", time*15/60);
             
           }
            }
                 else if(pin_down==1){
                     i=i--;
                     wait(0.1);
                     if( i<0)
                     i=0;
                     time = i;
                     if(i<4){
                         lcd.cls();
                         lcd.locate(1,0);
             lcd.printf("Time= %1.2f mn", time*15);
             }
              else{      
              lcd.cls();
              lcd.locate(1,0);
              lcd.printf("Time= %1.2f Hr",time*15/60);
              
                     }
                     }
                     } while(pin_start==0);

  Quarters = time; //Initial selected number of quarters 
    
    if(delay>0){
        do{
        lcd.cls();
             lcd.locate(1,0);
             lcd.printf("Delay = %1.2f mn",delay*15);
             delay--;
             }while(delay ==0);
             
    startRun();
}
else{
    startRun();  
    }

  
  pin_start.rise(&startRun);                     // call to (re)start engine run)&& status == true if(runtimeleft == 0 )
   
    // just wait for keypress
   
   while(1);      
   
   
         }   

Question relating to:

A Ton (Timer On) is used to provide an \'on delay\' system so as to avoid using wait() timer, Ton, wait
Be the first to answer this question.