How do you make a Vex EDR push button function as a toggle switch?
Help would be appreciated.
Thank you,
Hettyc Tracyn
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?
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
}
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);
}
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)
}
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?
#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);
}
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.
The only problem with the program is that when I try to add more than one sonar to it, it wonāt startā¦