I can't make a program to make my motor turn on and remain on at the press of a button


task main()
{
	bool spinnerOn;
	spinnerOn = false;
	while(1 == 1)
	{
		if(vexRT[Btn8D] == 1)
		{
			if(spinnerOn == false)
			{
				motor[intakeSpinner] = 127;
				spinnerOn = true;
			}
			if(spinnerOn == true)
			{
				motor[intakeSpinner] = 0;
				spinnerOn = false;
			}
		}
           }
}

I have it as that, but whenever I push button 8d, the motor turns on for about 1 second and then stops. I’d like it so that, at the push of the button, the motor will keep spinning until I push it again, which should make it stop. I need help :frowning:

Your problem is that the loop is repeating so quickly that it will be turning on and off the motor hundreds of times, and not stay on, because the button is pressed down for multiple repetitions of the loop, no matter how quick you try to tap it. What you want to do is wait until the button is released before checking again, or you can do a program like this that will only react if the button is now pressed but was not pressed before, meaning it was just pressed and hasn’t been held:


word btnLast = 0; //We need this variable later on
while(true)
{
	if(vexRT[Btn8D] && !btnLast) //This checks if the button has just been pressed, but was not the last time.
	{
		if(spinnerOn == false)
			{
				motor[intakeSpinner] = 127;
				spinnerOn = true;
			}
			if(spinnerOn == true)
			{
				motor[intakeSpinner] = 0;
				spinnerOn = false;
			}
	}
btnLast = vexRT[Btn8D]; //Set the current state to what will be the previous state for the next loop
}
    

Edit: Why does syntax highlighting not work if you write the code inside of the post?

You can keep that same program but instead of controlling the motor power inside that loop. Make a seperate set of if statements checking the Boolean values and if it’s true make the motors have power and if it isn’t make them not have power.

Okay, thank you guys!! I really appreciate the help! :slight_smile: