UI Buttons

Hi,
We have buttons we want to use to automatically bring our arm to three different positions, using a limit switch (to bring the arm to position zero) and an encoder to accurately “guess” the middle and drop positions. Here is the code we will be using with three buttons on the ui. We are having trouble getting these buttons to work. they seem to turn to 1 instead of 0 when some of them are pressed, because we tested a joystick in the same port, and that is the behavior the exhibited. here is the code we are using, please let me know of possible problems. thanks!!!

static unsigned int desired_position = 1;		
static unsigned int current_position = 1;	//always must start out at -10 

degrees
unsigned int speed_fwd = 64;
unsigned int speed_rev = 190; 
static int go_home = 0;				//intialize the arm position to 

zero position
if (go_home == 0)
{
	pwm01 = speed_rev;
	if (rc_dig_in06 == 0)                 //is arm all the way back? (limit switch)
	{
		go_home = 1;
		pwm01 = 127;
	}
}
		


/* the arm will have 3 positions.
1: home position (-10 degrees)
2: load position (strait up or 0 degrees)
3: forward position (18 degrees)*/

if (p4_sw_trig == 1)
{
	desired_position = 1;
}
if (p4_sw_top == 1)
{
	desired_position = 2;
}
if (p4_sw_aux1 == 1)
{
	desired_position = 3;
}


if (desired_position == 1)			//check states of encoder, and act 

accordingly
{
	if (desired_position == current_position)
	{
		pwm01 = 127;
	}
	else
	{
		if (Get_Left_Encoder_Count() > 0)
		{
			pwm01 = speed_rev;
		}
		else
		{
			pwm01 = 127;
			desired_position = 1;
			current_position = 1;
			Set_Left_Encoder_Count(0);
		}
	}
}
if (desired_position == 2)
{
	if (desired_position == current_position)
	{
		pwm01 = 127;
	}
	else
	{
		if (Get_Left_Encoder_Count() < 1280)
		{
			pwm01 = speed_fwd;
		}
		else if (Get_Left_Encoder_Count() > 1280)
		{
			pwm01 = speed_rev;
		}
		else
		{
			pwm01 = 127;
			desired_position = 2;
			current_position = 2;
			Set_Left_Encoder_Count(1280);
		}
	}
}
if (desired_position == 3)			
{
	if (desired_position == current_position)
	{
		pwm01 = 127;
	}
	else
	{
		if (Get_Left_Encoder_Count() < 2304)
		{
			pwm01 = speed_fwd;
		}
		else
		{
			pwm01 = 127;
			desired_position = 3;
			current_position = 3;
			Set_Left_Encoder_Count(2304);
		}
	}
}

(some of the code lines are wrapped to the next line)
thanks in advance.

Your code looks fine to me. (but if I’m wrong… talk to my lawyer. ;))

I’m always confused about this, but I think that the buttons on the OI are reversed. (1/high for released, 0/low for pressed)

But… my whole post could be wrong. :o

I know that the states for a limit switch are reverse that for the OI buttons… but i’m not sure which one is what.

From the OI a button will be 0 if unpushed, 1 if pushed.
The RC is reversed and will be 1 if unpushed, 0 if pushed. (because of the pull-up)

…yep! and to muddy the waters a bit:
Limit switches can usually be wired “normally open” or " normally closed". If you don’t like the fact that the RC is “1 if unpushed and 0 if pushed”, wire the limit switch “normally closed” and it will behave as the OI :wink:

…and a few random musings:
for your desiredposition == 2,
I’d suggest giving yourselves some “room to maneuver” when it comes to setting the motors to neutral. Hitting and holding exactly 1280, may result in a lot of racheting. You may want to make that a band…say > 1278 and < 1282 or something like that

Whenever you are in the home position, re-zero your encoder counter. There is always the chance of being off a few counts as the arm moves up and down and error can build up over a lot of use.

…just my 2cents
Eric