Programming limit switch for top and bottom of lift

What solution do you use for programming limit switches to stop motors at the top and bottom of lift/arm travel?

motor = vexRTJoystick
if limit = 1
then motor = 0

With this type of solution once you hit the limit the motor value always equals 0 and you can never start the motors again, right?

A potentiometer is also another great means of limiting the lift travel. I had to scratch my head when the kids implemented the top and bottom. You don;t want to cut it off when you go past top or else you can’t go down. Similar for the bottom. Can’t cut the motors off completely or it might stay up/down there.

if (SensorValue[armPot] > top_pos)
{
// only allow downward motion on the motors

}
else if (SensorValue[armPot] < bottom_pos)
{
// only allow upward motion on the motors
}
else
{
// do your normal thing
}

You can couple this with a background task on arm position management. Set a global to the desired value and have the control loop do the rest.

So nested “if” statements are the way to go? Something like this pseudo code…

if top limit == 1
{
// only allow downward motion on the motors
if vexRTJoystick > 0 { motor = 0 }
else {motor = vexRTJoystick}
}

else if bottom limit == 1
{
// only allow upward motion on the motors
if vexRTJoystick < 0 { motor = 0 }
else {motor = vexRTJoystick}
}

else
{
// do your normal thing
motor = vexRTJoystick
}

Exactly!

I think that this can solve your problem :slight_smile:

Task usercontrol(){
int mvalue=0; //variable to control the motor value
While(1){
if((vexRT[Btn5U]==1)&&(sensorValue[limitU]==0)) //when up button pressed and up sensor not pressed
mvalue=127; //write 127 to the variable
else if((vexRT[Btn5U]==1)&&(sensorValue[limitU]==1)) // if up sensor and up button are pressed
mvalue=0; //stop motor
else if((vexRT[Btn5D]==1)&&(sensorValue[limitD]==0)) //when down button pressed and down sensor not pressed
mvalue=-127; //write -127 to the variable
else if((vexRT[Btn5D]==1)&&(sensorValue[limitD]==1)) // if down sensor and down button are pressed
mvalue=0; //stop motor
else mvalue=0; //if you not press any button stop motor

motor[arm]=mvalue; //arm motor equal to the value variable
}
}

A potentiometer would do both.
Here’s a code that I made that maybe could help by using buttons, but if you are wanting to use the joystick, then say that you need a joystick program, and I’ll make you one.


//Keep in mind, the code needs to be changed depending on your lift and the range it lifts.//
//The potentiometer goes between 0 and 4095//
//This code uses the motor name [motor1], and has the potentiometer with the name of [Potent]//
task (main)
{
while(1==1)
{
if(SensorValue[Potent]<1000)//Might have to change
{
motor[motor1]=0;
}
else if(SensorValue[Potent]>1000)//Might have to change
{
motor[motor1]=0;
}
else//If not >1000 or <1000
{
if(vexRT[Btn5U]==1)//If Btn5U on remote
{
motor[motor1]=127;
}
else if(vexRT[Btn5D]==1)//If Btn5D on remote
{
motor[motor1]=-127;
}
else//When neither 5D or 5U is pressed
{
motor[motor1]=0;
}
}
}
}

Thank you both, these are great.

You’re welcome :smiley:

Single motor assignment code methodology prevents jitter motors, and allows encapsulation of things like limit switches and slew control.


 array M[1..10] , PM[1..10] // request to set motor to this value,  previous motor value request
while(1){
  if (sensors) { set and reset M[1-10] as desired} // and all other code
// at bottom of while loop, do final motor set processing.
 //slew control: check desired M vs previous PM,  scale back M if increment is too big.
 if (M[1] > 0 and limit_m1_pos) { M[1] := 0}; // limit switch check is last thing before set, 
 if (M[1] < 0  and limit_m1_neg) {M[1] :=0;}  // we dont care why you want to change M, limit stops it
 if ( E_stop) { M[1..10] = [0,0,0,0,0,0,0,0,0,0];} // implement Estop in single location
motor[1] := M[1];  // this is the only time and only place we set motor
PM[1]:=M[1]; // save current motor state as previous motor state, if needed for slew control, etc.
} //wend