What do I need to get these variables to work?

So i’m trying to code a ring intake for our robot and I am using a variable. This way it will be a toggle type function. When I build it, it shows errors over the variable I use but I don’t understand why they are there. I think it may be something with the program and how it handles variables. It won’t let me add the pictures so ill just copy and paste the code.

//control the ring intake
if(Controller1.ButtonA.pressed && int x = 2()){
RingMotor.spin(forward, 75, pct);
int x = 1;
}
if(Controller1.ButtonA.pressed && int x = 1()){
RingMotor.stop();
int x = 2;
}

The errors are over the "x"s yellow over the ones that are apart of int x = # red over the others -
expected '( ’ for function-style cast or type construction
unused variable ‘x’ [-Wunused-variable]
expected ‘(’ for function-style cast or type construction
unused variable ‘x’ [-Wunused-variable]

1 Like

My first review of your code: There is an issue with the equals signs. One = is an assignment and two == is a comparison.
x = 10 is an assignment. I am telling x to equal 10.
if( x == 10) is a comparison. Is x equal to 10?

The second concern is that you are declaring x each time which resets it.
Use int x = 0 only one time at the top. This is where you declaring the variable before your where(true) statement.
Later in your code you need to only change the value of x with an assignment: x = 2; or x = 1;

int x = 1;
while(true) {
	//control the ring intake
	if(Controller1.ButtonA.pressed && int x == 2()){
		RingMotor.spin(forward, 75, pct);
		x = 1;
	}
	if(Controller1.ButtonA.pressed && int x = 1()){
		RingMotor.stop();
		x = 2;
	}
}

With that said you will still run into a problem of the motor starting and stopping repeatedly when you hold down the button. To solve this you will need a latch.

6 Likes