11 years, 2 months ago.

Possible to trigger Ticker interface using argument?

Is there any way to start a ticker interface using argument?
I know that InterruptIn does that but i need my event based function to repeat itself periodically when an event is triggered.
I tried using mbed-rtos but my program always hang even when i did not include the header file in my main file.

1 Answer

11 years, 2 months ago.

hi, have you check the example in the handbook?

#include "mbed.h"
 
Ticker flipper;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
 
void flip() {
    led2 = !led2;
}
 
int main() {
    led2 = 1;
    flipper.attach(&flip, 2.0); // the address of the function to be attached (flip) and the interval (2 seconds)
 
    // spin in a main loop. flipper will interrupt it to call flip
    while(1) {
        led1 = !led1;
        wait(0.2);
    }
}

Is this what you want to do?

Greetings

No sorry.

For example

#include "mbed.h"
 
Ticker flipper;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalIn enable(p30);
 
void flip() {
    led2 = !led2;
}
 
int main() {
    led2 = 1;
    flipper.attach(&flip, 2.0, enable) //Ticker will only trigger when an event occur in DigitalIn P30. 
    while(1) {
        led1 = !led1;
        wait(0.2);
    }
}
posted by WB Tan 25 Jan 2013

That isn't possible, although you could make an own library which does it. Although also in your interrupt handler you could simply add that if enable == 1, do that.

If you have for example that after a button is pressed once, it should start calling flip, you can simply only attach it after the button has been pressed.

posted by Erik - 25 Jan 2013

hi, what do you meant isn´t possible? a try the example from the handbokk on my mbed and works.

The led1 is blinking and the led2 blink in the period that is specified (2 seconds)

Greetings

posted by Ney Palma 25 Jan 2013

Your code is possible, but what WB IsMe likes to do (in the post above mine) isn't possible (without making a custom library to do it).

posted by Erik - 25 Jan 2013

ok, then what yo want is the ticker function to be activate when some event ocurr (like pressing a button). Is that right? if so try this

#include "mbed.h"

InterruptIn button(p5);
Ticker flipper;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
int isPressed;

void flip() {
    led2 = !led2;
} 
  
void eventFunction() {
    if(!isPressed) {
        flipper.attach(&flip, 2.0);   
        isPressed=1;
    } else {
        flipper.detach();
        isPressed=0;   
    }
}

int main() {
    isPressed=0;
    led2 = 0;
    button.rise(&eventFunction);
         
    while(1) {
        led1 = !led1;
        wait(0.2);
    }
}

Greetings

posted by Ney Palma 25 Jan 2013