RobotC toggle

Hello, we are attempting to make our pneumatics work on a latch system. So we press the button, it opens, and it only closes when we press the same button again.

How can we do this? The way we thought to do this is not working. Here is some code from our program:

	

int Q = 0;
        	if(vexRT[Btn5UXmtr2] == 1 && SensorValue(intake) != 1)
		{
			SensorValue(intake) = 1;
			Q = 1
		}
		else if(vexRT[Btn5UXmtr2] == 1 && SensorValue(intake) == 1)
		{
			SensorValue(intake) = 0;
			Q = 0
		}
		else
		{
			SensorValue(intake) = Q;
		}

Do you see any problems with this code?

Thank You!

Here is what we do. It isn’t perfect, but it does work. I’m sure there is a more proper way to do it.



{
int claw = -1;

	if(vexRT[Btn5U] ==1)
	{
		claw = claw * -1;
		wait1Msec(150);
	}
	if (claw == 1)
	{
 		SensorValue[pneumatics] = 1;
 		wait1Msec(50);
 	}
 	if (claw == -1)
 	{
 		SensorValue[pneumatics] = 0;
 		wait1Msec(50);
 	}
}

Basically, there is a variable “claw” that gets multiplied by -1 with each button push. Therefore, “claw” will always be either -1 or +1. The output of the pneumatics is then controlled by the variable.

There was a toggle function in code I posted yesterday here.
https://vexforum.com/showpost.php?p=373842&postcount=4

“drivemode” is a variable that toggles 0 or 1, just use a similar method with a more appropriately named variable and then send that to the pneumatics.

Is there any particular reason why you’re using int and not bool?

Habit, bool is often not available as a standard type in the C language, it was added as part of the C99 standard (I think) and I started using C way before that.