My team is having a problem with programing a servo to toggled. We would like our buttons to sort of "toggle" the servo, so that we press it and it moves to the position and stays and then when we press it again it goes back to 0. But we don't know how. Please help!!!

Tue Nov 18, 2008 5:23 pm

matpat10

Rookie

Joined: Tue Nov 18, 2008 5:13 pmPosts: 9

Re: Can't get servo to work with two different buttons

I forgot to change the subject title.

Tue Nov 18, 2008 5:31 pm

Jeff McBride

Professor

Joined: Fri Sep 19, 2008 1:22 amPosts: 200

Re: Can't get servo to work with two different buttons

This code will toggle the target position of a servo every time game controller button 3 is pressed.

Actually, the code posted above probably won't work (assuming that it's within a scan sequence) because we continually scan the joystick, so the code would actually activate more than once, and thus the result would be indeterminate.

In order to correctly handle a toggle, you have to make a one-shot notifier as well...

I took the code and added in the "b3Pressed" boolean variable to handle that one-shot

bool b3Pressed = false; // This is the new piece -- This is a holder to let us know the state of the last press of the button... /* Section within bluetooth scan loop */

if (joy1Btn(3)) // Toggle the position of the servo... { if ( ! b3Pressed) // only deal with this if the previous scan was off! { if (servoTarget == servoTarget1) servoTarget = servoTarget2; else servoTarget = servoTarget1;

servo[servo1] = servoTarget; } b3Pressed = true; // indicate that we're already pressed for the next scan through } else b3Pressed = false; // clear the "history" so we can tell that we've got a new button press...}

Try that (or something similar). The biggest problem with the button pushes on this system is that the bluetooth is slower than the actual scan speed of the controller. To that end, you can't push the button fast enough to make it only scan through once.

Sun Nov 30, 2008 3:57 pm

Jeff McBride

Professor

Joined: Fri Sep 19, 2008 1:22 amPosts: 200

Re: Can't get servo to work with two different buttons

There is another way to handle the button changes. Keep track of the previous state of the entire joystick.joy1_Buttons field and only fall into (or call) the code that processes the button clicks when the field changes:

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum