Calling different funcion from same interrupt pin

Hello everyone,I want to know if it’s possible to call (in switch case statemnts) different functions from same interruptIn pin when I press a button many time ( 1 button press then first function,2 button press second function and so on…).Can you help me?thanks

Hello,

and what did you already tried?
You need something like

  • handle with a button bouncing (hardware or software solution)
  • use and establish a timeout timer where you will count interrupts from the button for a final decision .

BR, Jan

If I have something like this

DigitalOut Led(#pin)
InterruptIn P(#pin)
string state

main() {

Switch (state)
case (S_OFF)
P.fall(&Light_on)
break
case (S_ON)
P.fall(&Light_blinking)
break
case (S_BLNK)
P.fall(&Light_off)
break
}

where Light_on,Light_blinking,Light_off) are 3 functions that called from same interrupt line

It’s a working solution?

Nope, that will not working.

It is not optimal (it will be inaccurate), but something like below could work (not tested, just for example)

#include "mbed.h"

DigitalOut Led(#pin);
InterruptIn P(#pin);

volatile int pressCounter = 0;

void Light_on();
void Light_blinking();
void Light_off();

void Pressed(){
   pressCounter++;
}

int main() {
  printf("Mbed button test\n");
  P.fall(callback(&Pressed));

  while(1){
    Switch (pressCounter){
      case 1:{
        Light_on();
        break;
      }
      case 2:{
        Light_blinking();
        break;
      }
      case 3:{
        Light_off();
        break;
      }
      default:{
        // error do nothing
      }
   }
  pressCounter = 0; // reset
  thread_sleep_for(500); // time for button press
  }
}

BR, Jan

I probably did not understand the task, according to your second topic, but I will place here a code anyway.

#include "mbed.h"

#define VARIANTSMAX 2
#define LOWTIME 200
#define HIGHTIME 500ms

DigitalOut led1(LED1);
InterruptIn button(BUTTON1);
Timeout tOut;

volatile bool buttonFlag = false;

void PressHandler(){
    if(!buttonFlag){
        buttonFlag = true;
        button.disable_irq();
    } 
}

volatile bool timeOutFlag = false;

void TimeOutHandler(){
    if(!timeOutFlag ){
        timeOutFlag  = true;
    } 
}

long Millis(void) {
    using namespace std::chrono;
    auto now_ms = time_point_cast<milliseconds>(Kernel::Clock::now()); // Convert time_point to one in microsecond accuracy
    return now_ms.time_since_epoch().count();
}

int main()
{
    printf("Running on Mbed OS %d.%d.%d.\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
    printf("Multiple button press test\n");
    button.fall(callback(&PressHandler));
    int counter = 0;
    long last_ms = Millis();
    
    while (true)
    {
        if(buttonFlag){
            long timeTemp = Millis(); 
            if((timeTemp - last_ms) > LOWTIME){
                tOut.attach(callback(&TimeOutHandler),HIGHTIME);
                if(++counter >= VARIANTSMAX){
                    tOut.detach();
                    timeOutFlag = true;
                }
            }
            last_ms = timeTemp;
            buttonFlag= false;
            button.enable_irq();
        }
        if(timeOutFlag){
            button.disable_irq();
            switch (counter) {
                case 1:{
                    printf("Case %d\n", counter);
                    break;
                }
                case 2:{
                    printf("Case %d\n", counter);
                    break;
                }
            }
            counter = 0;
            timeOutFlag = false;
            button.enable_irq();
        }
        //led1 = !led1;
        //thread_sleep_for(WAIT_TIME_MS);
    }
}

BR, Jan