8 years, 6 months ago.

Best way to change LED blink frequency based on application status

Hi, I'm wondering what the best way is to change the blink frequency depending on the status of some function. For example I'd like to introduce different LED blink frequency depending on thresholds I set for a sensor connected to an analog in PIN.

I tried to implement this with a global variable which is written from within the function that reads from analog in and evaluated by the ticker callback function to set a wait(). This would freeze my bluetooth stack

I found a working example that re-attaches the ticker from within the callback function but this uses a flag to control the ticker detach / attach. Not a big deal but I feel this is not the most elegant solution and might lead to bad maintainability and problems when others try to understand the code.

Is there a way how I could pass a float / integer when calling the callback function. How would you implement this?

void LedBlink(void)
{
   led1 = !led1; 
    if (ChangeStatusFlag) {
        // re-task ticker flash rate
        LedTicker.detach(); 
        LedTicker.attach(&LedBlink,led_timer); 
        ChangeStatusFlag = false;
    }
}

Thanks, Jens

1 Answer

8 years, 6 months ago.

connect the led to a PWM port. Set duty cycle to say 0.5. Depending on the thresholds change the pulse width.

Thanks Pramod! Looks like a good solution! However I am using PWM on a different pin with a higher frequency already and I think I can not set the frequency differently for different PWM ports.

posted by Jens Strümper 01 Oct 2015