How do you make a push button work as a toggle switch using PLTW natural language?

I need to make a toggle that is push to start then again to stop…

Plus it needs to wait 5 sec after the first button press before it starts.

task main()
{
int enable = 0, btnState = 0;
while(1){
if(SensorValue[StartButton] && !btnState) enable = (enable + 1) % 2;
btnState = SensorValue[StartButton];

if(enable){	wait(5);
	if(SensorValue(SonarF) < 20)/*Determine which Sonar*/
	{startMotor(MotorRF, 50);
		startMotor(MotorRM, 50);
		startMotor(MotorRB, 50);
		startMotor(MotorLF, 50);
		startMotor(MotorLM, 50);
		startMotor(MotorLB, 50);
	}
	else
	{
		startMotor(MotorRF, -50);
		startMotor(MotorRM, -50);
		startMotor(MotorRB, -50);
		startMotor(MotorLF, 50);
		startMotor(MotorLM, 50);
		startMotor(MotorLB, 50);
	}
}
if(SensorValue(SonarF) < 2)/*Determine which Sonar*/
{startMotor(MotorRF, 100);
	startMotor(MotorRM, 100);
	startMotor(MotorRB, 100);
	startMotor(MotorLF, 100);
	startMotor(MotorLM, 100);
	startMotor(MotorLB, 100);
}
else
{
	startMotor(MotorRF, -50);
	startMotor(MotorRM, -50);
	startMotor(MotorRB, -50);
	startMotor(MotorLF, 50);
	startMotor(MotorLM, 50);
	startMotor(MotorLB, 50);
}
   
{ else }
	stopMotor(MotorRF);
stopMotor(MotorRM);
stopMotor(MotorRB);
stopMotor(MotorLF);
stopMotor(MotorLM);
stopMotor(MotorLB);

}
}

Plus There is an error for line 74: Error:Unexpected ‘else’. Ignored.

Thanks,
Hettyc Tracyn

Could you surround your code with [code] and [/code] on the forum so its more readable please? :slight_smile:

#pragma config(Sensor, dgtl1,  StartButton,    sensorTouch)
#pragma config(Sensor, dgtl2,  SonarF,         sensorSONAR_inch)
#pragma config(Sensor, dgtl4,  StopButton,     sensorTouch)
#pragma config(Sensor, dgtl7,  SonarB,         sensorSONAR_cm)
#pragma config(Sensor, dgtl9,  SonarR,         sensorSONAR_cm)
#pragma config(Sensor, dgtl11, SonarL,         sensorSONAR_cm)
#pragma config(Motor,  port1,           MotorRF,       tmotorNone, openLoop)
#pragma config(Motor,  port2,           MotorRM,       tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port3,           MotorRB,       tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port4,           MotorLF,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port5,           MotorLM,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port6,           MotorLB,       tmotorNone, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

//Project Title: Paoli IN, LRCC Sumobot MK2
//  Team Members: Joshua N Barnett, Xander Phelps
//  Date: 03/04/2020
//  Section: RDI

//Psudocode:

//		button switch pressed
//		scan with front sonar by revolving right
//			(right motors going backwards, and left motors going fowards)
//		once object detected
//			procede towards object at half speed
//		once object within 2 inches
//			procede foward at top speed

task main()
{
	int enable = 0, btnState = 0;
while(1){
    if(SensorValue[StartButton] && !btnState) enable = (enable + 1) % 2;

    btnState = SensorValue[StartButton];

    if(enable){	wait(5);
		if(SensorValue(SonarF) < 20)/*Determine which Sonar*/
		{startMotor(MotorRF, 50);
			startMotor(MotorRM, 50);
			startMotor(MotorRB, 50);
			startMotor(MotorLF, 50);
			startMotor(MotorLM, 50);
			startMotor(MotorLB, 50);
		}
		else
		{
			startMotor(MotorRF, -50);
			startMotor(MotorRM, -50);
			startMotor(MotorRB, -50);
			startMotor(MotorLF, 50);
			startMotor(MotorLM, 50);
			startMotor(MotorLB, 50);
		}
	}
	if(SensorValue(SonarF) < 2)/*Determine which Sonar*/
	{startMotor(MotorRF, 100);
		startMotor(MotorRM, 100);
		startMotor(MotorRB, 100);
		startMotor(MotorLF, 100);
		startMotor(MotorLM, 100);
		startMotor(MotorLB, 100);
	}
	else
	{
		startMotor(MotorRF, -50);
		startMotor(MotorRM, -50);
		startMotor(MotorRB, -50);
		startMotor(MotorLF, 50);
		startMotor(MotorLM, 50);
		startMotor(MotorLB, 50);
	}
       
    { else }
		stopMotor(MotorRF);
	stopMotor(MotorRM);
	stopMotor(MotorRB);
	stopMotor(MotorLF);
	stopMotor(MotorLM);
	stopMotor(MotorLB);
}
    }

Plus There is an error for line 74: Error:Unexpected ‘else’. Ignored.

Thanks,
Hettyc Tracyn

bool enable = false, btnState = false;
while(1){
    enable = sensorValue[touch] && !btnState ? !enable : enable;
    btnState = sensorValue[touch];
    if(enable){
       //do
    } else {
       //do another thing
    }
    wait1Msec(10);
}
2 Likes

In my personal opinion, you should be the one to find out these simple problems. But anyways… By following the proper syntax and formatting, you can easily figure out and locate the problem almost immediately. I found the location of the missing curly braces and re-formatted it to be more understandable (Also, I’d suggest a wait within the while loop whenever nothing if physically running, so I added that too):
(Code moved to response below as it’s likely what you were looking for)

Your code looks a bit confusing, so I’m trying to see what you’re trying to do. See if this works:

#pragma config(Sensor, dgtl1,  StartButton,    sensorTouch)
#pragma config(Sensor, dgtl2,  SonarF,         sensorSONAR_inch)
#pragma config(Sensor, dgtl4,  StopButton,     sensorTouch)
#pragma config(Sensor, dgtl7,  SonarB,         sensorSONAR_cm)
#pragma config(Sensor, dgtl9,  SonarR,         sensorSONAR_cm)
#pragma config(Sensor, dgtl11, SonarL,         sensorSONAR_cm)
#pragma config(Motor,  port1,           MotorRF,       tmotorNone, openLoop)
#pragma config(Motor,  port2,           MotorRM,       tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port3,           MotorRB,       tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port4,           MotorLF,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port5,           MotorLM,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port6,           MotorLB,       tmotorNone, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

//Project Title: Paoli IN, LRCC Sumobot MK2
//  Team Members: Joshua N Barnett, Xander Phelps
//  Date: 03/04/2020
//  Section: RDI

//Psudocode:

//		button switch pressed
//		scan with front sonar by revolving right
//			(right motors going backwards, and left motors going fowards)
//		once object detected
//			procede towards object at half speed
//		once object within 2 inches
//			procede foward at top speed

task main()
{
    bool enable = false, btnState = false;
    while(1)
    {
        enable = sensorValue[touch] && !btnState ? !enable : enable;
        btnState = sensorValue[touch];

        if(enable)
        {
            wait(5);
            if(SensorValue(SonarF) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 100);
                startMotor(MotorRM, 100);
                startMotor(MotorRB, 100);
                startMotor(MotorLF, 100);
                startMotor(MotorLM, 100);
                startMotor(MotorLB, 100);
            }
            else if(SensorValue(SonarF) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 50);
                startMotor(MotorRM, 50);
                startMotor(MotorRB, 50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);   
                startMotor(MotorLB, 50);
            }
            else
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
        }
        else
        {
            stopMotor(MotorRF);
            stopMotor(MotorRM);
            stopMotor(MotorRB);
            stopMotor(MotorLF);
            stopMotor(MotorLM);
            stopMotor(MotorLB);
        }
        wait1Msec(10);
    }
}

There is one error by the way Connor:

        On Line 36:
               *Warning*:Invalid '=' operation for types 'bool' and 'long'

I do not understand what it means… or what you do with it.

That is an error on the variable types. The sensor value recieved from the sensor is a long value, despite being a 0 or 1:

It was a mistake on our part, and I apologize.
Knowing the following:

And also knowing:

And knowing integers are whole numbers too,
You can replace bool enable = false, btnState = false; to int enable = false, btnState = false; and it will likely work.

Thank You. The error went away.

1 Like

The only problem with the program is that when I try to add more than one sonar to it, it won’t start…

Here the currant code is:

#pragma config(Sensor, dgtl1,  StartButton,    sensorTouch)
#pragma config(Sensor, dgtl2,  SonarF,         sensorSONAR_inch)
#pragma config(Sensor, dgtl4,  StopButton,     sensorTouch)
#pragma config(Sensor, dgtl7,  SonarB,         sensorSONAR_cm)
#pragma config(Sensor, dgtl9,  SonarR,         sensorSONAR_cm)
#pragma config(Sensor, dgtl11, SonarL,         sensorSONAR_cm)
#pragma config(Motor,  port2,           MotorRF,       tmotorNone, openLoop)
#pragma config(Motor,  port3,           MotorRM,       tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port4,           MotorRB,       tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port5,           MotorLF,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port6,           MotorLM,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port7,           MotorLB,       tmotorNone, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

//Project Title: Paoli IN, LRCC Sumobot MK2
//  Team Members: Joshua N Barnett, Xander Phelps
//  Date: 03/04/2020
//  Section: RDI

//Psudocode:

//		button switch pressed
//		scan with front sonar by revolving right
//			(right motors going backwards, and left motors going fowards)
//		once object detected
//			procede towards object at half speed
//		once object within 2 inches
//			procede foward at top speed

task main()
{
    int enable = false, btnState = false;
    while(1)
    {
        enable = SensorValue[StartButton] && !btnState ? !enable : enable;
        btnState = SensorValue[StartButton];

        if(enable)
        {
            wait(5);
            if(SensorValue(SonarF) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 100);
                startMotor(MotorRM, 100);
                startMotor(MotorRB, 100);
                startMotor(MotorLF, 100);
                startMotor(MotorLM, 100);
                startMotor(MotorLB, 100);
            }
            else if(SensorValue(SonarF) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 50);
                startMotor(MotorRM, 50);
                startMotor(MotorRB, 50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
            else
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
        }
        else
        {
            stopMotor(MotorRF);
            stopMotor(MotorRM);
            stopMotor(MotorRB);
            stopMotor(MotorLF);
            stopMotor(MotorLM);
            stopMotor(MotorLB);
        }
        wait1Msec(10);
    }
}

when I try to copy and paste and switch the sonar it won’t even start.

#pragma config(Sensor, dgtl1,  StartButton,    sensorTouch)
#pragma config(Sensor, dgtl2,  SonarF,         sensorSONAR_inch)
#pragma config(Sensor, dgtl4,  StopButton,     sensorTouch)
#pragma config(Sensor, dgtl7,  SonarB,         sensorSONAR_cm)
#pragma config(Sensor, dgtl9,  SonarR,         sensorSONAR_cm)
#pragma config(Sensor, dgtl11, SonarL,         sensorSONAR_cm)
#pragma config(Motor,  port2,           MotorRF,       tmotorNone, openLoop)
#pragma config(Motor,  port3,           MotorRM,       tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port4,           MotorRB,       tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port5,           MotorLF,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port6,           MotorLM,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port7,           MotorLB,       tmotorNone, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

//Project Title: Paoli IN, LRCC Sumobot MK2
//  Team Members: Joshua N Barnett, Xander Phelps
//  Date: 03/04/2020
//  Section: RDI

//Psudocode:

//		button switch pressed
//		scan with front sonar by revolving right
//			(right motors going backwards, and left motors going fowards)
//		once object detected
//			procede towards object at half speed
//		once object within 2 inches
//			procede foward at top speed

task main()
{
    int enable = false, btnState = false;
    while(1)
    {
        enable = SensorValue[StartButton] && !btnState ? !enable : enable;
        btnState = SensorValue[StartButton];

        if(enable)
        {
            wait(5);
            if(SensorValue(SonarF) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 100);
                startMotor(MotorRM, 100);
                startMotor(MotorRB, 100);
                startMotor(MotorLF, 100);
                startMotor(MotorLM, 100);
                startMotor(MotorLB, 100);
            }
            else if(SensorValue(SonarF) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 50);
                startMotor(MotorRM, 50);
                startMotor(MotorRB, 50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
            else
            {if(SensorValue(SonarR) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
            else if(SensorValue(SonarR) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
            else
            {if(SensorValue(SonarB) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, -50);
                startMotor(MotorLM, -50);
                startMotor(MotorLB, -50);
            }
            else if(SensorValue(SonarR) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, -50);
                startMotor(MotorLM, -50);
                startMotor(MotorLB, -50);
            }
            else
            	 {if(SensorValue(SonarL) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 50);
                startMotor(MotorRM, 50);
                startMotor(MotorRB, 50);
                startMotor(MotorLF, -50);
                startMotor(MotorLM, -50);
                startMotor(MotorLB, -50);
            }
            else if(SensorValue(SonarL) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 50);
                startMotor(MotorRM, 50);
                startMotor(MotorRB, 50);
                startMotor(MotorLF, -50);
                startMotor(MotorLM, -50);
                startMotor(MotorLB, -50);
            }
            else
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
        }
        
        }
        wait1Msec(10);
    }
}}}

I Think It works now.

#pragma config(Sensor, dgtl1,  StartButton,    sensorTouch)
#pragma config(Sensor, dgtl2,  SonarF,         sensorSONAR_inch)
#pragma config(Sensor, dgtl4,  StopButton,     sensorTouch)
#pragma config(Sensor, dgtl7,  SonarB,         sensorSONAR_cm)
#pragma config(Sensor, dgtl9,  SonarR,         sensorSONAR_cm)
#pragma config(Sensor, dgtl11, SonarL,         sensorSONAR_cm)
#pragma config(Motor,  port2,           MotorRF,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port3,           MotorRM,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port4,           MotorRB,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port5,           MotorLF,       tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port6,           MotorLM,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port7,           MotorLB,       tmotorNone, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

//Project Title: Paoli IN, LRCC Sumobot MK2
//  Team Members: Joshua N Barnett, Xander Phelps
//  Date: 03/04/2020
//  Section: RDI

//Psudocode:

//		button switch pressed
//		scan with front sonar by revolving right
//			(right motors going backwards, and left motors going fowards)
//		once object detected
//			procede towards object at half speed
//		once object within 2 inches
//			procede foward at top speed

task main()
{
    int enable = false, btnState = false;
    while(1)
    {
        enable = SensorValue[StartButton] && !btnState ? !enable : enable;
        btnState = SensorValue[StartButton];

        if(enable)
        {
            wait(5);
            if(SensorValue(SonarF) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 100);
                startMotor(MotorRM, 100);
                startMotor(MotorRB, 100);
                startMotor(MotorLF, 100);
                startMotor(MotorLM, 100);
                startMotor(MotorLB, 100);
            }
            else if(SensorValue(SonarF) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 50);
                startMotor(MotorRM, 50);
                startMotor(MotorRB, 50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
            else
            {if(SensorValue(SonarR) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
            else if(SensorValue(SonarR) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
            else
            {if(SensorValue(SonarB) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, -50);
                startMotor(MotorLM, -50);
                startMotor(MotorLB, -50);
            }
            else if(SensorValue(SonarR) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, -50);
                startMotor(MotorLM, -50);
                startMotor(MotorLB, -50);
            }
            else
            	 {if(SensorValue(SonarL) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 50);
                startMotor(MotorRM, 50);
                startMotor(MotorRB, 50);
                startMotor(MotorLF, -50);
                startMotor(MotorLM, -50);
                startMotor(MotorLB, -50);
            }
            else if(SensorValue(SonarL) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 50);
                startMotor(MotorRM, 50);
                startMotor(MotorRB, 50);
                startMotor(MotorLF, -50);
                startMotor(MotorLM, -50);
                startMotor(MotorLB, -50);
            }
            else
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
        }

        }
        wait1Msec(10);
    }
}}
int disable = false;
while (0)
{disable = SensorValue[StartButton] && !btnState ? !disable : disable;
        btnState = SensorValue[StartButton];}
if (disable)
{stopMotor(MotorRF);
	stopMotor(MotorRM);
	stopMotor(MotorRB);
	stopMotor(MotorLF);
	stopMotor(MotorLM);
	stopMotor(MotorLB);
	}
}

working on normal stop. Says that the last while is always false… How do you fix this?

#pragma config(Sensor, dgtl1,  StartButton,    sensorTouch)
#pragma config(Sensor, dgtl2,  SonarF,         sensorSONAR_inch)
#pragma config(Sensor, dgtl4,  StopButton,     sensorTouch)
#pragma config(Sensor, dgtl7,  SonarB,         sensorSONAR_cm)
#pragma config(Sensor, dgtl9,  SonarR,         sensorSONAR_cm)
#pragma config(Sensor, dgtl11, SonarL,         sensorSONAR_cm)
#pragma config(Motor,  port2,           MotorRF,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port3,           MotorRM,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port4,           MotorRB,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port5,           MotorLF,       tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port6,           MotorLM,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port7,           MotorLB,       tmotorNone, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

//Project Title: Paoli IN, LRCC Sumobot MK2
//  Team Members: Joshua N Barnett, Xander Phelps
//  Date: 03/04/2020
//  Section: RDI

//Psudocode:

//		button switch pressed
//		scan with front sonar by revolving right
//			(right motors going backwards, and left motors going fowards)
//		once object detected
//			procede towards object at half speed
//		once object within 2 inches
//			procede foward at top speed

task main()
{
    int enable = false, btnState = false;
    while(1)
    {
        enable = SensorValue[StartButton] && !btnState ? !enable : enable;
        btnState = SensorValue[StartButton];

        if(enable)
        {
            wait(5);
            if(SensorValue(SonarF) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 100);
                startMotor(MotorRM, 100);
                startMotor(MotorRB, 100);
                startMotor(MotorLF, 100);
                startMotor(MotorLM, 100);
                startMotor(MotorLB, 100);
            }
            else if(SensorValue(SonarF) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 50);
                startMotor(MotorRM, 50);
                startMotor(MotorRB, 50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
            else
            {if(SensorValue(SonarR) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
            else if(SensorValue(SonarR) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
            else
            {if(SensorValue(SonarB) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, -50);
                startMotor(MotorLM, -50);
                startMotor(MotorLB, -50);
            }
            else if(SensorValue(SonarR) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, -50);
                startMotor(MotorLM, -50);
                startMotor(MotorLB, -50);
            }
            else
            	 {if(SensorValue(SonarL) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 50);
                startMotor(MotorRM, 50);
                startMotor(MotorRB, 50);
                startMotor(MotorLF, -50);
                startMotor(MotorLM, -50);
                startMotor(MotorLB, -50);
            }
            else if(SensorValue(SonarL) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 50);
                startMotor(MotorRM, 50);
                startMotor(MotorRB, 50);
                startMotor(MotorLF, -50);
                startMotor(MotorLM, -50);
                startMotor(MotorLB, -50);
            }
            else
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
        }

        }
        wait1Msec(10);
    }
}}
int disable = false;
while (1)
{disable = SensorValue[StartButton] && !btnState ? !disable : disable;
        btnState = SensorValue[StartButton];}
if (disable)
{stopMotor(MotorRF);
	stopMotor(MotorRM);
	stopMotor(MotorRB);
	stopMotor(MotorLF);
	stopMotor(MotorLM);
	stopMotor(MotorLB);
	}
}

It no longer has the error, it just won’t stop if you press the button again


#pragma config(Sensor, dgtl1,  StartStopButton,    sensorTouch)
#pragma config(Sensor, dgtl2,  SonarF,         sensorSONAR_inch)
#pragma config(Sensor, dgtl4,  StopButton,     sensorTouch)
#pragma config(Sensor, dgtl7,  SonarB,         sensorSONAR_cm)
#pragma config(Sensor, dgtl9,  SonarR,         sensorSONAR_cm)
#pragma config(Sensor, dgtl11, SonarL,         sensorSONAR_cm)
#pragma config(Motor,  port2,           MotorRF,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port3,           MotorRM,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port4,           MotorRB,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port5,           MotorLF,       tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port6,           MotorLM,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port7,           MotorLB,       tmotorNone, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

//Project Title: Paoli IN, LRCC Sumobot MK2
//  Team Members: Joshua N Barnett, Xander Phelps
//  Date: 03/04/2020
//  Section: RDI

//Psudocode:

//		button switch pressed
//		scan with front sonar by revolving right
//			(right motors going backwards, and left motors going fowards)
//		once object detected
//			procede towards object at half speed
//		once object within 2 inches
//			procede foward at top speed

task main()
{
    int enable = false, btnState = false;
    while(1)
    {
        enable = SensorValue[StartStopButton] && !btnState ? !enable : enable;
        btnState = SensorValue[StartStopButton];

        if(enable)
        {
            wait(5);
            if(SensorValue(SonarF) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 100);
                startMotor(MotorRM, 100);
                startMotor(MotorRB, 100);
                startMotor(MotorLF, 100);
                startMotor(MotorLM, 100);
                startMotor(MotorLB, 100);
            }
            else if(SensorValue(SonarF) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 50);
                startMotor(MotorRM, 50);
                startMotor(MotorRB, 50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
            else
            {if(SensorValue(SonarR) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
            else if(SensorValue(SonarR) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
            else
            {if(SensorValue(SonarB) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, -50);
                startMotor(MotorLM, -50);
                startMotor(MotorLB, -50);
            }
            else if(SensorValue(SonarR) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, -50);
                startMotor(MotorLM, -50);
                startMotor(MotorLB, -50);
            }
            else
            	 {if(SensorValue(SonarL) < 2)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 50);
                startMotor(MotorRM, 50);
                startMotor(MotorRB, 50);
                startMotor(MotorLF, -50);
                startMotor(MotorLM, -50);
                startMotor(MotorLB, -50);
            }
            else if(SensorValue(SonarL) < 20)/*Determine which Sonar*/
            {
                startMotor(MotorRF, 50);
                startMotor(MotorRM, 50);
                startMotor(MotorRB, 50);
                startMotor(MotorLF, -50);
                startMotor(MotorLM, -50);
                startMotor(MotorLB, -50);
            }
            else
            {
                startMotor(MotorRF, -50);
                startMotor(MotorRM, -50);
                startMotor(MotorRB, -50);
                startMotor(MotorLF, 50);
                startMotor(MotorLM, 50);
                startMotor(MotorLB, 50);
            }
        }

        }
        wait1Msec(10);
    }
}}}

have an emergency stop that cuts power. don’t need special coding.