Delaying function return value
Posted: Tue Aug 30, 2016 5:45 pm
I have a function which needs to query a sensor, then wait some amount of time while the sensor takes a measurement, and then query the sensor again to receive the measurement.
Something along the lines of the code below is what I'd like to do. However, it won't work because the function returns (with no value) before the timer callback runs. Now I could do this by using tmr.delay(), but I'd like to avoid that if at all possible, since these wait times can be quite slow in some cases.
Anyone have any neat ideas on how to hold off returning a value until the sensor conversion is complete?
Something along the lines of the code below is what I'd like to do. However, it won't work because the function returns (with no value) before the timer callback runs. Now I could do this by using tmr.delay(), but I'd like to avoid that if at all possible, since these wait times can be quite slow in some cases.
Anyone have any neat ideas on how to hold off returning a value until the sensor conversion is complete?
Code: Select all
function returnSensor()
querySensor()
tmr.alarm(0, 100, tmr.ALARM_SINGLE, function()
val = querySensor()
return val
end)
end