Limit Switch Code

Help!

I have been looking up how to code a limit switch but I never get it right! I am using robotC. My team and I are trying two different robots at the same time to see which is more efficient. So we need to code a limit switch for an elevator lift and one for a two bar lift.

Here’s some simple, one motor, example code showing how to use limit switches to stop the motor at the top and bottom of travel.

#pragma config(Sensor, dgtl1,  limit_up,       sensorTouch)
#pragma config(Sensor, dgtl2,  limit_down,     sensorTouch)
#pragma config(Motor,  port1,            ,             tmotorVex393_HBridge, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
    int motor_speed;
    
    while(true) {
      if( vexRT Ch2 ] > 10 ) {
        motor_speed = vexRT Ch2 ];
        // Lift up
        if( SensorValue limit_up ] == 1 ) {
          // stop motor
          motor_speed = 0;
        }
      }
      else
      if( vexRT Ch2 ] < -10 ) {
        motor_speed = vexRT Ch2 ];
        // Lift down
        if( SensorValue limit_down ] == 1 ) {
          // stop motor
          motor_speed = 0;
        }
      }
      else
        motor_speed = 0;
    
      motor port1 ] = motor_speed;
    
      // Give the cpu a chance !
      wait1Msec(20);  
    }
}