How to make a button that both starts and stops a program? (RobotC)

Trying to write a code that allows one bumpSwitch to start and stop a program.
So, when I press it starts the code and when I press it again it stops the program.

I’m no expert coder, but I think you could have it set a flag to true when the button is pressed the first time, and then only if the flag is true when it is pressed does it stop the program. Does that make sense?

This is what a loop is good for. By putting the program in the loop, and breaking the loop when bump switch is pressed, you can allow the program to end.
Psuedocode would be something like this:
If bumpSwitch = True
while (bumpSwitch = false)
If anyone can one up me, they are more than welcome, as i am not the programmer.

I have tried using the while function or loop function similar to:

while (bumpSwitch=1)

//run program

//stop program

This program works when I hold the button. What I want is, when I press the bumpSwitch once, it starts the program. Then if I press it again, it stops running the program.

Thank you for your help

You need something more like

bool flagBumpSwitch = 0

if (bumpSwitch == 1)
flagBumpSwitch = 1

if flagBumpSwitch == 1

//run program

if (bumpSwitch ==1 && flagBumpSwitch == 1)
flagBumpSwitch = 0

if flagBumpSwitch == 0

//stop program

At least, I think that’s the kind of thing you are looking for.

I don’t know RobotC very well, so there may be an easier way to do it, but I believe that is one way to achieve what you are looking for. There are better coders than me who could probably work magic with like 2-3 lines, but I think this should work for your purposes.

Hi, I am the programmer for my team.

Try something like this.

if(bumpswitch == 1) {
     if(programRunning == true) {
           programRunning = false;
else {
programRunning = true;

Try that and tell me if it works. If it doesn’t, I’ll try something else for you and see if I can fix it. Good luck!

task main() {

  word bumpLast = false, // word is the same as bool
    exeCode = false; //but bumper switches use word instead

  while(true) { //program will loop, ignoring most of the code when you want it to
    if(SensorValue[bump] && !bumpLast) { //if button is pressed and wasn't before
      bumpLast = SensorValue[bump]; //update value
      exeCode = !exeCode; //toggle state of execution

    if(exeCode) {
      //Insert what you want here

This should compile once you initialize the bumper switch in Motors and Sensors Setup and name it bump.

I tried lpieroni’s code.
It starts the program with the button press, but when I press the button again, it doesn’t stop. It also keeps looping.

Basically, the program is running a motor for a specific length ( I use the encoder to make the motor turn a specific amount of times).

I’ve also tried creating a program where I have variable that is true for a specific time ( so when I press the bumpSwitch during this time, it should start the program). After the time, it changes variables (so when I press it, it should stop the program).

Thank you all so much for the help!

This seems to be the simplest way to do it. It’ll work as long as you want your program to repeat in the while loop.

In your code, do you have a while loop inside the if(exeCode) block, or perhaps several wait statements? The variable will only update when the code reaches that particular line, and loops and waits delay that significantly.

Actually, wait, I’m stupid =P
Replace this

  if(SensorValue[bump] && !bumpLast) {
    bumpLast = SensorValue[bump];
    exeCode = !exeCode;

with this

  if(SensorValue[bump] && !bumpLast) {
    exeCode = !exeCode;
  bumpLast = SensorValue[bump];

In the first version, the value of bumpLast is set to true once, then never set back to false because the code only enters the if(!bumpLast) block when bumpLast is false. In the second version, the value of bumpLast is updated every iteration.

Does this look correct?

Do you want to be able to start the program again after it’s been stopped? If so, I would set it up so it’s something like:

float bumpValue = 0

task bumpSensor();
while(bumpValue == 0){
if(sensorValue[bumpSensor] == 1){
bumpValue = 1;
while(bumpValue == 1){
if(sensorValue[bumpSensor] == 1){
bumpValue = 0;

All you would need to do is call this task at the beginning of your usercontrol/autonomous period if that’s where you plan on putting it.

Oops. One thing I missed in my code. Everything inside of my bumpSensor task needs to be in their own while loop (while(true){}).

Lennon Headlee, is this what it should look like.

The task I added after all the bumpSwitch codes.
The program I specifically want to run is running motor under a specific encoder value.

I don’t have my bot so it’s difficult to test rn.

This code isn’t working properly, it starts with the bumpSwitch, but I can’t stop with the bumpSwitch.
Test 2.PNG

In ROBOTC I’m pretty sure you can run a checker task that simply kicks off a new task / kills the current task on bump switch press.