ROBOTC.net forums
http://www.robotc.net/forums/

Can't get servo to work with two different buttons
http://www.robotc.net/forums/viewtopic.php?f=52&t=943
Page 1 of 1

Author:  matpat10 [ Tue Nov 18, 2008 5:23 pm ]
Post subject:  Can't get servo to work with two different buttons

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!!!

Author:  matpat10 [ Tue Nov 18, 2008 5:31 pm ]
Post subject:  Re: Can't get servo to work with two different buttons

I forgot to change the subject title.

Author:  Jeff McBride [ Tue Nov 18, 2008 7:05 pm ]
Post subject:  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.

Code:
#define servoTarget1  50  // Minimum position of servo
#define servoTarget2  99  // Maximum position of servo
...
    int servoTarget = -1;  // Start out with servo in an unknown state.
...
    if (joy1Btn(3))   // Toggle the position of the servo...
    {
        if (servoTarget == servoTarget1)
            servoTarget = servoTarget2;
        else
            servoTarget = servoTarget1;

        servo[servo1] = servoTarget;
    }
...
}

Author:  NalaTI [ Sun Nov 30, 2008 3:57 pm ]
Post subject:  Re: Can't get servo to work with two different buttons

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

Code:
Code:
#define servoTarget1  50  // Minimum position of servo
#define servoTarget2  99  // Maximum position of servo
...
    int servoTarget = -1;  // Start out with servo in an unknown state.

    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.

Author:  Jeff McBride [ Sun Nov 30, 2008 4:52 pm ]
Post subject:  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:

Code:
        buttonsPrev = 0;
        while (true)
        {
             getjoysticksettings(joystick);

             if (joystick.joy1_Buttons != buttonsPrev)
             {
                  buttonsPrev = joystick.joy1_Buttons;
                  if (joy1Btn(3))
                  {
                     // Do work
                  }

                  if (joy1Btn(2))
                  ....
             }
         }


This avoids having to track separate "pressed" flags for each button.

Author:  matpat10 [ Sun Nov 30, 2008 9:03 pm ]
Post subject:  Re: Can't get servo to work with two different buttons

Thank you all, we got the program to work out and are having no problems with our TeleOp program. Now for the Autonomous...... :programmer:

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/