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

How do you make a Vex EDR push button function as a toggle switch?

Help would be appreciated.

Thank you,
Hettyc Tracyn

This is in RobotC and needs to be PLTW Natural Language.

I’ve tried several ways:

untilBump(StartButton);

or

	int buttonpress = 0;
	buttonpress = StartButton;
	while (buttonpress == 1)

Any suggestions?
If so, Thank You,
Hettyc Tracyn

I see you want a toggle, but how many modes do you want to reel through every time the button is pressed?
Do you want it to switch between two modes? ie. Press button once it does one code press it again do another code.
Do you want to switch between three modes? ie. Press once activates code 2, press another code 3, press another code 1.
Or more modes?

1 Like

Here’s a quick thing I wrote up with a two mode switch:

bool buttonPressed = false;
bool enabled = false;

while(true){

if (sensorValue[Touch] && !buttonPressed)
 {
buttonPressed = true;
if(enabled) enabled = false;
else enabled = true;
}
else if (!sensorValue[Touch]){
buttonPressed = false;
}

if(enabled){
//Do something
}
else{
//Do another thing
}

wait1Msec(20); //Since it's a repeating while loop, we wait 20 milliseconds each time to save CPU

}
1 Like

Here’s a cool one that works

int enable = 0;
bool btnState = false;
while(true){
    if(sensorValue[touch] && !btnState){
        enable++;
        enable%=2;
    }
    btnState = sensorValue[touch];
    if(enable == 1){
       //do
    } else {
       //do another thing
    }
    wait1msec(10);
}
4 Likes

Ah man you’re tempting me to see how compact we can make this. Alright, lets see…

int enable = 0, btnState = 0;
while(1){
    if(sensorValue[touch] && !btnState) enable = (enable + 1) % 2;
    btnState = sensorValue[touch];

    if(enable){
       //do
    } else {
       //do another thing
    }

wait1msec(10)

}
2 Likes

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…