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

Question for MHTS
http://robotc.net/forums/viewtopic.php?f=52&t=10492
Page 3 of 3

Author:  akash329dsouza [ Thu Mar 05, 2015 6:32 pm ]
Post subject:  Re: Question for MHTS

Ok Thanks!, I should be able to figure it out from here then!
Also, thanks Synergists! I might be able to use that in the Grabbers for the lift!

Author:  MHTS [ Mon Mar 30, 2015 5:23 pm ]
Post subject:  Re: Question for MHTS

Synergists wrote:
Just for your reference, this is how we did a toggle button. Initially the button was meant to switch between three different positions, but now we just need two but the functionality is still there if we need three or more positions later:
Code:
#ifndef ROBOTDEFINITIONS
#include "..\RobotDefintions 150131.c"
#define ROBOTDEFINITIONS 1
#endif

int nextServoCommand = 1;
bool bButtonPressed = false;

void autoServoBall()
{
   clearDebugStream();
   while(true)
   {
      getJoystickSettings(joystick);
      if(joy2Btn(9) == true && bButtonPressed == false)
      {
         switch((int) nextServoCommand)
         {
         case 1: servo[autoBallDispenser] = HoldSmallBall;
            nextServoCommand++;
            bButtonPressed = true;
            //writeDebugStreamLine("Small Ball");
            break;
         case 2: servo[autoBallDispenser] = HoldNoBalls;
            nextServoCommand = 1;
            bButtonPressed = true;
            //writeDebugStreamLine("No Balls");
            break;
         //case 3: servo[autoBallDispenser] = HoldBothBalls;
         //   nextServoCommand = 1;
         //   bButtonPressed = true;
         //   //writeDebugStreamLine("Both Balls");
         //   break;
         default:
            break;
         }
      }
      if(joy2Btn(9) == true != true)
      {
         bButtonPressed = false;
      }
   }
}

BTW, don't know if you realize it, the above code is actually implementing a toggling button using a state machine. I have renamed the variable "nextServoCommand" to "servoBallState" and optimized the code a little bit to show you the equivalence. This code allows any number of states. Each time button 9 is pressed, it can go to a different state.
Code:
int servoBallState = 1;

void autoServoBall()
{
    clearDebugStream();
    while(true)
    {
        getJoystickSettings(joystick);
        //
        // This basically said if button 9 is pressed.
        // It is detecting an edge event (i.e. a transistion
        // from button 9 not pressed to being pressed.
        //
        if(joy2Btn(9) == true && bButtonPressed == false)
        {
            bButtonPressed = true;
            switch (servoBallState)
            {
            case 1:
                servo[autoBallDispenser] = HoldSmallBall;
                servoBallState++;  //advance to next state (the toggled ON state)
                //writeDebugStreamLine("Small Ball");
                break;
            case 2:
                servo[autoBallDispenser] = HoldNoBalls;
                servoBallState = 1;  // go back to previous state (the toggled OFF state)
                //writeDebugStreamLine("No Balls");
                break;
//            case 3: servo[autoBallDispenser] = HoldBothBalls;
//               nextServoCommand = 1;
//               //writeDebugStreamLine("Both Balls");
//               break;
            default:
                break;
            }
        }
        else if(!joy2Btn(9))
        {
            bButtonPressed = false;
        }
    }
}

A toggle button has only two states (ON and OFF), you can do it with a boolean instead of an integer state such as the code below.
Code:
bool bButtonPressed = false;
bool toggledOn = false;

while (true)
{
    getJoystickSettings(joystick);
    if (!bButtonPressed and joy2Btn(9))
    {
        bButtonPressed = true;
        toggledOn = !toggledOn;
    }
    else if (!joy2Btn(9))
    {
        bButonPressed = false;
    }

    if (toggledOn)
    {
        servo[autoBallDispenser] = HoldSmallBall;
        //writeDebugStreamLine("Small Ball");
    }
    else
    {
        servo[autoBallDispenser] = HoldNoBalls;
        //writeDebugStreamLine("No Balls");
    }
}

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