Adding a code block to interrupt backwards motion with bump switch

Hi, newbie to the forum here.

I am running a PLTW class using Vex Natural Language PLTW and Vex Cortex 2.0. We are building 'bots that are supposed to go forward, and if they see an obstacle back up for a time, then pick a random direction (left or right) and then try going forward again. I have even set up an indicator light to check which direction it picked.

Now the issue: I am attempting to put in a code block that says when the bump switch is pressed, the reverse motion stops and the robot will “rest” for 1 second, then start the sequences where it turns.

Anyhow, this is the code. I tried putting the untilbump or SensorValue code in, but the robot seems to not register the switch. So any help would be appreciated – I feel like I am coding “correctly” but the order is wrong.

Another weird behavior is one of the LEDs seems to stay on even though the beginning of the code turns them both off.

(These robots were build and designed by students)

Any guidance is much appreciated.

#pragma config(Sensor, dgtl2, redlight, sensorLEDtoVCC)
#pragma config(Sensor, dgtl5, bumpSwitch, sensorTouch)
#pragma config(Sensor, dgtl9, ultrasonic, sensorSONAR_cm)
#pragma config(Motor, port1, leftMotor, tmotorVex393_HBridge, openLoop)
#pragma config(Motor, port10, rightMotor, tmotorVex393_HBridge, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main()

{int direction; //introduce variable “direction”
direction = 0; // set the variable to zero

	startMotor(leftMotor,60); //start moving forward

startMotor(leftMotor,-30); //start moving backwards
direction = random(2);
if (direction == 1)


There should be a way to see the value of your sensors in RobotC. Check if they display any values


The best way to do this would be to use a state machine.

Have variable 1int state` with values for moving forward, waiting, moving backward, etc…

Then you transition from moving back state either if 2 sec had elapsed or bumper switch hits the wall.

if( state==MOVING_FORWARD && see obstacle )
 state = MOVING_BACK;

if( state==MOVING_BACK && 
    ( GetTimer(T0)>2000 ||  SensorValue(bumpSwitch)==1 ) 

if( state==WAIT_1_SEC && GetTimer(T0)>1000 )

Thanks to you both. I want to clarify: is the && a way of doing if - X and Y statements in RobotC? (I am very new to the language).

Yes, && means the same as and


OK, looking at this, I have to be honest: I have zero idea of how to set up a state machine. I am not sure f you are saying to intro a variable, and then have it stand for something – that part I can kind of understand – but how does one get the variable return so that it “knows” if it is moving? This is pretty opaque to me, sorry about that. (I nee anyone explaining this to pretend I am your grandmother)

Just to clarify I tried to set this up and realized I had no idea what the heck I was doing

For your case state machine needs just one variable that changes its value, which I called state.

You could assign integers to that variable. Let say 0 means stop and wait, 1 means move forward, 2 means move back, etc…

Then you have a while(true) loop and inside the loop you have multiple if( state==NUMBER && something is happening ) statements, which means that if current state is NUMBER and sensors tell you that something is happening, then you set the state to another number.

When state changes you can stop or start motors. For example, if state is moving forward and sensor detects an obstacle, then you change state to moving back and set motors to reverse speed.

When current state is moving back and bumper switch is pressed then you stop motors, set state to wait 1 sec and reset the timer.

Then if state is wait 1 sec and timer runs to 1 sec you change state again and do something else appropriate for that state change.

1 Like

Right, but how does the program “know” the state is changed or not? I am setting up a state variable, but how do I detect that the robot is moving forward or back in order to change it? I get that the sensors return values but how do I “see” what the motors, for example, are doing?

You know that robot is moving forward if you start sending power to the motor at the same time that you set your state variable to MOVE_FORWARD.

Then you set motor power to zero or to move backward in the same if statement where you change the state variable to the respective values.

When you send power to the motor you can assume that it starts to move. If you run into obstacle you will know because your bumper switch will sense it.

1 Like