简体   繁体   中英

mbed os 5 c++ programming

void rise_handler() { timer.start();}
void fall_handler() { timer.stop(); }
void signal() {
     while(1) {
              if (num > 0.5) {
                 rise_handler();
              } 
              else {
                 fall_handler();
              }
     }
}

I have an input from the sensor and it changes with time. What should I do so when num is greater than 0.5 will only be called once in the while loop?

Add a boolean flag. When the condition is true you set the flag, and when it's false you clear the flag.

Use this flag in combination with the condition to know when to call which function.

Example:

bool flag = false;

while (1)
{
    if (num > 0.5)
    {
        if (!flag)
        {
            // First time above the threshold
            flag = true;
            rise_handler();
        }
    }
    else
    {
        if (flag)
        {
            // First time below the threshold
            flag = false;
            fall_handler();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM