toggle button and limit switch

Sorry I need help. I am programming in easy C and I want to create a program that uses the digital button #8 up and the limit switch in the same condition. My students have a claw bot and I want them to push and release the digital button 8 and have their lever move autonomously until it hits the limit switch. Here is what I have so far:

{While 1

eight_button_up = get joystick digital (1,8,2)
top_limit = get digital input(8)
{if (eight_button_up==1++top_limit==0)
set motor (7,127)
set motor (8,-127)}
set motor (7,0)
set motor (8,0)
}

for some reason it keeps saying error: expected “)” before “top_limit”.

Can someone help me? Thanks.

ok, so i got the limit switch and the button to work, but the motor cuts out when I release the button. How do I program the button to activate on press and release?

You If statement looks strange. The left curly brace should be after the conditional statement. The code you have will run the motor while they have the button pressed and until it reaches the limit.

While(1)
{
    eight_button_up = get joystick digital (1,8,2)
    top_limit = get digital input(8)
	if (eight_button_up==1 && top_limit==0)
		{
		set motor (7,127)
		set motor (8,-127)
		}
	else 
		{ 
		set motor (7,0)
		set motor (8,0)
		}
}

To have the button latch you could use the following code.

char eight_button;
char enable_motor = 0;
char top_limit;

while ( 1 )
{
	eight_button = GetJoystickDigital ( 1 , 8 , 2 ) ;
	top_limit = GetDigitalInput ( 8 ) ;
	if ( eight_button == 1 )
		{ // If button is pressed then enable motor but don't start it
		enable_motor = 1 ;
		}
	else
		{ // If button is not pressed then check if motor is enabled.
		if ( enable_motor == 1 && top_limit == 0 )
			{ // If motor is enabled and limit switch is not then run motor
			SetMotor ( 1 , 50 ) ;
			}
		else
			{ // In all other cases stop motor
			SetMotor ( 1 , 0 ) ;
			}
	}
}

Good luck.

thanks for the reply. How do you add the enable_motor into the if statement? Second, the second else statement doesn’t have anything in it. Is that correct? Thanks again.

I believe he just set a variable called enable motor and called it in that if loop. Also, the second else statement does have statements inside of it. It contains another set of if-else loops.