Controlling a Servo with Android

Ok So I’m controlling a servo. I have it working via the new SDK, but to get it to move I have it hooked up to a seekbar, and as I slide the bar it continuously calls my movement function sending the new degree…

I don’t think this is the best way to do it, as it results in a sort of jumpy/mushy control experience… Anyone have any better ideas on how to do it that would result in a more fluid/real time feeling control?

If I understand your issue correctly is that moving a slider on your Android app sends too many requests to your device.

What you need a way to throttle the requests being sent by the Android.

I don’t have a complete solution for you but I would try using the Handler class.

When the slider starts moving, send the position to the device right away, save the last position sent to an instance variable, start a timer with postDelayed in 500 ms and mark the timer started in an instance variable.

If the slider moves while the timer is started and before it fires, save the new position to send in another instance variable.

When the timer fires check if the new position to send is the same as the last position sent. If it’s the same, mark the timer as stopped. If it’s different, send the new position, save it as the last position and start another timer with postDelayed (i.e. follow the exact same steps as above).

This way, the servo will start moving right away but won’t be sent a new position more than once every 500ms.

Thanks- I thought of that, but that still seems like it would result in a jittery/laggy experience wouldn’t it? Like with the srvo blasting at full force to catch up to wherever the slider is once the timer resets?

Ostensibly I’d like to have the speed of the servo match the speed I’m moving the slider in a real time way. Maybe that’s just not possible… I need to think this one through more then I thought I guess. :stuck_out_tongue:

Looking at the terminal I think part of the issue is that sometimes the numbers come in out of order. I guess I need to account for that in my function…