4 years, 2 months ago.

when I call the function StartRun(), it does not recognize wait for 1 sec and counts in us?

Timer

// The startRun function when called does not perform same. The controller is not recognizing wait(1) adn counts in micro seconds I cannot find the reason. it should do the same whhen called again by interrupt.

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

TextLCD lcd(A0, A1, A2, A3, A4, A5);
DigitalOut switchengine(D2);
DigitalIn pin_up(D10);
DigitalIn pin_down(D9);
InterruptIn pin_start(D11);
DigitalIn pin_delay(D12);
InterruptIn Pin_rain(D3);
DigitalIn pin_mainpower(D6);

Timer timer;
int Quarters;
void displayUpdate();
void startRun() ;
void startRun1() ;
void callstart();
void timerf();
void pumpoff();
bool status ;
float delay;
int i;
int d;
float del;
int s;
int m;
float reverse= 0.0;
float h;
float tim;
int begin;
void rainsense();
time_t seconds = time(NULL);
//called when 15min has passed
void startRun1()
    {
    //engine should be running
status = false;
    lcd.cls();
    lcd.locate(3,0);
    lcd.printf("PUMP START");
    wait(1);
    displayUpdate();
  
    }
void pumpoff()
{
    switchengine=0;
    status = true;
    //update display
    lcd.cls();
    lcd.locate(3,0);
    lcd.printf("PUMP OFF!");
    lcd.locate(0,1);
    lcd.printf("Ran T:%1.2f Hr",i*0.25);

    pin_start.rise(&timerf);
    pin_start.fall(&callstart);
    Pin_rain.rise(&rainsense);

}
void rainsense()
{
    switchengine=0;
    lcd.cls();
    lcd.locate(0,0);
    lcd.printf("%s",ctime(&seconds));
    lcd.locate(0,1);
    lcd.printf("Rantime:%1.2f Hr ",reverse);

    while(1) {
        switchengine=0;
    }
}

void timerf()
{
    timer.reset();
    timer.start();
    begin = timer.read();
}

void callstart()
{
    int end = timer.read();
    timer.stop();
    int t = end - begin;

    if(status==true) {
        if(t==2) {
           displayUpdate();
            
        }
    }
}
void displayUpdate()
{
    //engine should be running

    if (i==0) {
        pumpoff();
    } else {
        switchengine=1;

        if(i<4) {
            m = i*2;
            s = 60;
            while(m > 0) {
                 wait(1);
                lcd.cls();
                lcd.locate(3,0);
                lcd.printf("Time= %d min ",i*2);
                lcd.locate(2,1);
                lcd.printf(" %d min %d",m-1, s);
               
                s--;
                if(s==0) {
                    m--;
                    s = 60;
                }
                if(m==0) {
                    pumpoff();
                }

                Pin_rain.rise(&rainsense);
            }
        }

        else {
            s = 60;
            h = i*0.25;

            while(h > 0) {
                 wait(1);
                if(h<1.1) {
                    lcd.cls();
                    lcd.locate(0,0);
                    lcd.printf("Time= %1.2f Hr ",i*0.25);
                    lcd.locate(1,1);
                    lcd.printf(" %1.1f min  %d",(h-0.01)*100, s);
                } else {
                    lcd.cls();
                    lcd.locate(0,0);
                    lcd.printf("Time =%1.2f Hr ",i*0.25);
                    lcd.locate(1,1);
                    lcd.printf(" %1.2f Hr  %d",h-0.01, s);
                }
                s--;
                if(s==0) {
                    h= h-0.01;
                    reverse = reverse+0.01;
                    s = 60;
                }
                if(h==0) {
                    pumpoff();
                }

                Pin_rain.rise(&rainsense);
            }

        }
    }
}

//update the display and start/stop the engine

// init and start the run
void startRun()
{

    status = false;
    lcd.cls();
    lcd.locate(3,0);
    lcd.printf("PUMP START");
    wait(1);
    lcd.cls();
    displayUpdate();
}

int main()
{
     pin_up.mode(PullDown);
    pin_down.mode(PullDown);
    pin_start.mode(PullDown);
    pin_delay.mode(PullDown);
   pin_mainpower.mode(PullDown);

    set_time(1458986114);

if(pin_mainpower == 1){
    lcd.cls();
    lcd.locate(3,0);
    lcd.printf("SET TIME");
    }else{
        lcd.cls();
        lcd.locate(2,0);
    lcd.printf("Pump Off");
    lcd.locate(0,1);
    lcd.printf("No MainPower");
        }
do{

   while(pin_delay==1) {
        d++;
        wait(0.3);
        if (d>100)
            d=100;

        if(d<4) {
            del = d ;
            lcd.cls();
            lcd.locate(1,0);
            lcd.printf("Delay= %1.2f mn", del*15);

        } else {
            del = d ;
            lcd.cls();
            lcd.locate(1,0);
            lcd.printf("Delay= %1.2f Hr", del*15/60);

        }
    }

    if(pin_up==1) {
        i++;
        wait(0.1);
        if (i>200)
            i=200;
        tim = i;
        if(i<4) {
            lcd.cls();
            lcd.locate(1,0);
            lcd.printf("Time= %1.2f mn", tim*15);

        } else {
            lcd.cls();
            lcd.locate(1,0);
            lcd.printf("Time= %1.2f Hr", tim*15/60);

        }
    } else if(pin_down==1){
        i--;
        wait(0.1);
        if( i<0)
            i=0;
        tim = i;
        if(i<4) {
            lcd.cls();
            lcd.locate(1,0);
            lcd.printf("Time= %1.2f mn", tim*15);
        } else {
            lcd.cls();
            lcd.locate(1,0);
            lcd.printf("Time= %1.2f Hr",tim*15/60);

        }
    }
} while(pin_start==0);

if(i==0)
{
    {
        lcd.cls();
        lcd.locate(3,0);
        lcd.printf("Error!!!");
        lcd.locate(1,1);
        lcd.printf("Time not set");
        wait(3);
    }
}

tim = Quarters; //Initial selected number of quarters

if(d>0)
{

    int sec = 60;
    int min = d*15;
    float hr = d*0.25;
    while(min>0) {
        sec--;
        if(sec==0) {
            min--;
            sec = 60;
        }
        if(min==0) {
            d=0;
            Pin_rain.rise(&rainsense);
        }
        if(min<60) {
            lcd.cls();
            lcd.locate(0,0);
            lcd.printf("Delay= %1.1f min",del*15);
            lcd.locate(2,1);
            lcd.printf("%d min %d",min-1, sec);
            wait(1);
            
        } else {

            lcd.cls();
            lcd.locate(0,0);
            lcd.printf("Delay= %1.1f Hr",(del*15)/60);
            lcd.locate(2,1);
            lcd.printf("%1.2f Hr %d",hr-0.01, sec);
            wait(1);

           
        }

    }

}

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

}


Question relating to:

Affordable and flexible platform to ease prototyping using a STM32L031K6T6 microcontroller.

1 Answer

4 years, 2 months ago.

You should write a small routine to prove to yourself that the "wait(xx)" function does in fact wait xxx seconds. Blink the led with the blinky routine or something similar. Then if that works..what could it be? It seems that TextLCD is probably inheriting the Serial class which is a buffered print as of a few releases ago. Perhaps try having it inherit RawSerial instead and see if the problem persists?

Accepted Answer

How can I inherit Rawserial. Can you please explain. I tried timer.read function but it does not detect anything

posted by jayendra mishra 01 Sep 2016