Execute function in given time period

Hi,
I’m new to mbed programming. I want to design a firmware that touch the touch button 4 times at given time (6s) , then execute a function. If the user can’t do the 4 button press in 6s nothing happens. Can someone help me how to do that?

Hello,

InterruptIn - API references and tutorials | Mbed OS 6 Documentation
Timeout - API references and tutorials | Mbed OS 6 Documentation

A basic example

#include "mbed.h" //MbedOS 6.12 & Nucleo-F767ZI

InterruptIn button(BUTTON1);
Timeout timeOut;

volatile bool isPressed = false;
volatile bool timeIsOut = false;
volatile bool timeOutRun = false;

void Pressed(){
    if(!isPressed){
        isPressed = true;
    }
}
void TimeIsOut(){
    if(!timeIsOut){
        timeIsOut = true;
    }
}

int main(){
    printf("Start\n");
    int pressCounter = 0;
    button.fall(callback(&Pressed));
    while (true) {
        if(isPressed){
            printf("Pressed %d\n", ++pressCounter);
            if(!timeOutRun){
                timeOut.attach(callback(&TimeIsOut), 6s);
                timeOutRun = true;
            }
            if(pressCounter == 4 && timeIsOut == false){
                printf("Do something\n");
                timeIsOut = false;
                timeOut.detach();
                timeOutRun = false;
                pressCounter = 0;
            }
            thread_sleep_for(100); // for a basic button debouncing
            isPressed = false;
        }
        if(timeIsOut){
            printf("Timeout\n");
            pressCounter = 0;
            timeOutRun = false;
            timeIsOut = false;
        }
        thread_sleep_for(100);
    }
}

BR, Jan

1 Like

Thank you