4 years, 11 months ago.

AnalogIn read within a Callback

I am trying to create a class that takes care of polling an analog in pin, and stores the results in an array. I am trying to do this with a ticker and callback. The idea is that the main loop can interrogate the data that has been collected in this class. If I use analogin.read in my callback I get a Mutex Lock error It works fine if I am using a digitalin.read What is the correct way for me to set up collecting sample readings from analogin outside the main loop?

1 Answer

4 years, 11 months ago.

In general do not put any code in an interrupt callback function or as little as possible.

In that callback function, simply set a flag and act on that flag in your main program.

I use something very basic like this to sense an operator switch input:

void callback() {    
    while(SW1==1);  // wait for release of switch (this is blocking)
    IRQ=1;  // variable IRQ is set to 1, test for this in main program.
}

This example is not be thread based RTOS friendly.

Accepted Answer