Hey Chris,
We are using a Firmata Arduino device’s (connected via bluetooth) digital pin to toggle a liquid pump into a pumping / not pumping state, based on the state of a .mwel variable which is changed based on the behavior of the subject.
We execute a wait statement of some duration (e.g., 100 msec) after turning the pump on, and after the wait statement returns, we turn the pump off. This seems to work fine.
However, if we reduce the duration to 10 msec, we find that the liquid pump is triggered somewhat randomly: sometimes the pump turns on for a brief period of time when the experiment reaches this block of code (as expected), but sometimes the pump does not do anything at all.
I think this might be related to latencies in the Bluetooth connection, but I’m not sure. Do you have any idea as to why this behavior is happening with a 10 msec wait?
I’ve attached the relevant code.
Hi Michael,
However, if we reduce the duration to 10 msec, we find that the liquid pump is triggered somewhat randomly: sometimes the pump turns on for a brief period of time when the experiment reaches this block of code (as expected), but sometimes the pump does not do anything at all.
Bluetooth low energy (BLE) devices have a “connection interval”, which determines how often data is exchanged over the Bluetooth connection. Firmata BLE devices set up in the recommended way support a connection interval between 15ms and 30ms (which follows Apple’s recommendations).
I suspect what’s happening is, because 10ms is less than the minimum connection interval, sometimes the commands to start and stop the pump are being sent at the same time. (That is, the iPad is queueing BLE messages until the next connection happens, at which point it sends all the queued messages at once.) The result of this is that the Firmata device turns the pump-control line high and then immediately sets it low again, and it looks like the line was never set at all.
I think the solution is to lengthen the interval between turning the pump on and off. Since the maximum connection interval is 30ms, using a delay longer than that should be safe. Alternatively, you could start with the minimum interval (15ms), and keep raising it until the pump responds consistently. (I don’t know if there’s a way to determine what connection interval the iPad and Firmata device have actually agreed upon.)
Cheers,
Chris