Limit Switch Coding help!


I am having trouble programming a limit switch for my NBN robot in ROBOTC. My robot’s main goal is the high lift. Because of the way I built my robot, when the lift is down I can still pull the lifter down which cause the frame to bend forward. This is my first year doing VEX and I am not very good at coding yet. My solution to this was to put a limit switch just under the ramp so when it is down, the switch is pressed. I have my 4 lifter motors coded to Channel 3 on the controller. The way my sister teams and I decided to code this was to try and code it so that when the switch is pressed, Channel 3 can only be + so the motors could only push up. This did not work as the motors could still be pulled down.


Can you use the absolute value function on the Channel 3 reading? So no matter what Channel 3 is reading, the motor will be commanded with a positive number.



I tried using abs() but I can still pull the ramp further down. The picture is the updated code.


Are you sure your limit switch is set up correctly, wired properly, plugged into the proper port, and is physically getting activated when the lift is down? If the switch never actually gets activated, then your motors will still be able to drive things down.


I am confused on the if statements, they seem to do absolutely nothing in your first picture and in your second picture, if the limit switch is pressed down and your ch3 is negative, the motors attached to ports 5,6,7, and 8 will go from negative 127 to positive 127. If the limit switch is not pushed down then your second if statement does nothing


The limit switch is plugged into port dgtl12 and I took the switch off and activated it by hand and nothing happened still.


So classic limit switch code may look like this. The idea is to limit the motors movement to one direction if the limit switch is activated.

#pragma config(Sensor, dgtl12, LimitSwitch,    sensorTouch)
#pragma config(Motor,  port2,           motorA,        tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
    int motorValue = 0;
      // Get joystick
      motorValue = vexRT Ch2 ];
      // Limit to down movement if limit switch is hit
      // limit switch is setup as touch sensor so pressed is 1
      if( SensorValue LimitSwitch ] == 1 ) {
        // are we going up ?
        if( motorValue > 0 ) {
          motorValue = 0;
      // Send control value to motor
      motor motorA ] = motorValue;

      // Don't hog cpu


My friend helped me format the code for my robot and the picture is the dummy code to get it to try to work. Unfortunately this code still does not work


Perhaps the switch is broken, or the dgtl12 port on the cortex broken. Do you see the value changing in the sensors debug window? If not try another switch and/or port.


I now know that I had the sensor in port SP in stead of port 12 because I could barely see my cortex.
Both of the codes work but, I decided to use jpearman’s code.


Oh, yes, that’s a very good point. That’s one of those classic robot programming errors: You hit a limit switch and the motors are shut down, just as planned. But then when you command the robot to move the other direction, the motors remain turned off and you can never move again. Direction matters.


my problem is that once it detects the switch it still coasts down rather than a sudden stop. and im using an exact copy of the “typical code”


Don’t revive dead threads start new ones


I recommend using PID and a potentiometer instead of a switch if you want to stop at an exact spot. If you have taken classical physics, and learned about the Newtonian laws, such as Newton’s Third Law (which you can apply to pushing on the platforms :P), you should know the motors have to accelerate and decelerate. Thus, your motors will not instantly stop when you tell them to. :smiley:

If you’re going to chastise someone, at least offer some help. :smiley: :smiley: :smiley:



the potentiometer doesnt have enough movement. plus, im only having it stop at the end of travel for the lower end. not the upper end. but also i got the switch to work.