Limit Switches

1st I’d like to say Hello to Marko,

I’m working on my Vex using EasyC 2.0 and I have most of my code figured out,
But as usual I always run into a problem.
I built a small lift using a Vex motor and gear rack.
The idea is to use 2 bumpers as a switch and when you push bumper1 the motor will
raise the lift and bumper2 will lower the lift. I have a limit switch at the top/bottom of the lift to stop the motor.
Right now it works, But once either limit switch is mashed it stops but it won’t do anything
else. Is there a way to code the switch to stop the lift and wait for the next bumper signal?
There’s also some other features in the code that seem to be working but I can’t get past this one problem.
Thanks
Calvin
Here’s a copy of my code:

#include “Main.h”

void main ( void )
{
int Limit1 = 0;
int Limit2 = 0;
int Bumper1 = 0;
int Bumper2 = 0;
int Bumper3 = 0;

// Trying to run a small Vex lift up/down with the help of a 12ch. remote
// Limit switches are staying active !!!
while ( 1 == 1 )
{
Bumper1 = GetDigitalInput ( 7 ) ; // 12 Channel Remote with Relays
if ( Bumper1 == 0 ) // If 1ch relay is closed
{
Wait ( 2000 ) ; // After pressing button wait 2 seconds
SetMotor ( 1 , 255 ) ; // Run motor up
}
else
{
Limit1 = GetDigitalInput ( 5 ) ; // Top limit switch to stop motor
if ( Limit1 == 0 ) // If limit switch is closed
{
SetMotor ( 1 , 127 ) ; // Stop motor
Wait ( 4000 ) ; // Wait 4 seconds then turn head
SetServo ( 2 , 0 ) ; // Turn to the right position
Wait ( 4000 ) ; // Wait 4 seconds in this position
SetServo ( 2 , 255 ) ; // Turn to the left position
Wait ( 4000 ) ; // Wait 4 seconds
SetServo ( 2 , 127 ) ; // Turn back straight
Wait ( 2000 ) ; // Wait 2 seconds
SetServo ( 2 , 0 ) ; // Turn to the right last time
Wait ( 2000 ) ; // Wait 2 seconds
SetServo ( 2 , 127 ) ; // Turn back straight
}
// Lift is up at this point and the top switch is still pressed
// Head continues to turn side to side
}
// Lift still up and will not return due to top limit switch !!!
// Need a override for the limit switches !!!
Bumper2 = GetDigitalInput ( 8 ) ; // Same 12ch. Remote with Relays
if ( Bumper2 == 0 ) // If 2ch. Relay is closed
{
Wait ( 2000 ) ; // After pressing button wait 2 seconds
SetMotor ( 1 , 0 ) ; // Run Motor Down
}
else
{
Limit2 = GetDigitalInput ( 6 ) ; // Bottom limit switch to stop motor
if ( Limit2 == 0 ) // If limit switch is closed
{
SetMotor ( 1 , 127 ) ; // Stop motor
}
}
Bumper3 = GetDigitalInput ( 9 ) ; // Same 12ch. Remote with Relays
if ( Bumper3 == 0 ) // If 3ch. Relay is closed
{
SetMotor ( 3 , 255 ) ; // Rotation Motor
}
else
{
SetMotor ( 3 , 127 ) ;
}
}
}

We had the same problem, we had a bucket that we wanted to raise and lower, and had limit switches on the top and bottom, and when we pushed the joystick up, it would raise, and if we pushed it down, it would lower. The problem is that if you simply say “if the top touch sensor is pressed, stop running the motor” and “if the bottom touch sensor is pressed, stop running the motor” in the code, then this is what happens: You start to run the motor, eventually, the limit switch is pressed, and the motor is stopped, then, you want to go back down, well, the top limit switch is still pressed and so keeps the motor from running. What you need to do is have it check “Is the arm trying to go up?” if so, then “is the top limit switch pressed?” if so, stop the motor, if not, then you would allow the motor to run. And you can do the same thing with the bottom limit switch. Hopefully I was clear, if you have anymore questions, feel free to ask. Hope this helps. :slight_smile:

Jordan

And Hello to you too… I am prepping for a Chemistry Lab, so I have no time to debug this right now…

I will get back to you later today… Unless someone else beats me to it…

Check Out Calvin’s R2D2 Project

I understand what you said, But I have no idea how to add it to the code.
I’m bottomline basic when it comes to coding, But I try.
The bumpers will be replaced with a 12ch. remote that has relays.
I had the relays working along time ago so it should be easy to do again.

I hope to use the 12ch remote and push button 1 to raise the lift and it will go up
then the 2nd servo will turn right to left to forward then stop.
Button 2 will lower the lift to the resting place.
Later, I hope to add another Vex motor that will rotate when button 3 is pushed
and stop when button 3 is pushed again.
Thanks for the help Guys :o)
calvin

The way I handle this problem is by putting an if statement around the code you want to run. Something like this:


//this will only allow the arm to move only if the limit switch isn't pressed
if(LimitSwitch != 0)    //if the limit switch is not pressed
{
       if(Bumper == 0)  //if the bumper is pressed
       { 
            //do stuff
       }
}

ctx32,
Here’s my naive offering:
First, it’s much easier to figure out what’s going on in the code if you indent it, such as:
#include “Main.h”

void main ( void )
{[INDENT] int Limit1 = 0;
int Limit2 = 0;
int Bumper1 = 0;
int Bumper2 = 0;
int Bumper3 = 0;

// Trying to run a small Vex lift up/down with the help of a 12ch. remote
// Limit switches are staying active !!!!!!
while ( 1 == 1 )
{
Bumper1 = GetDigitalInput ( 7 ) ; // 12 Channel Remote with Relays
if ( Bumper1 == 0 ) // If 1ch relay is closed[INDENT]         {
    Wait ( 2000 ) ; // After pressing button wait 2 seconds
    SetMotor ( 1 , 255 ) ; // Run motor up
    }

[/INDENT]else[INDENT] {
Limit1 = GetDigitalInput ( 5 ) ; // Top limit switch to stop motor
if ( Limit1 == 0 ) // If limit switch is closed[INDENT] {
SetMotor ( 1 , 127 ) ; // Stop motor
Wait ( 4000 ) ; // Wait 4 seconds then turn head
SetServo ( 2 , 0 ) ; // Turn to the right position
Wait ( 4000 ) ; // Wait 4 seconds in this position
SetServo ( 2 , 255 ) ; // Turn to the left position
Wait ( 4000 ) ; // Wait 4 seconds
SetServo ( 2 , 127 ) ; // Turn back straight
Wait ( 2000 ) ; // Wait 2 seconds
SetServo ( 2 , 0 ) ; // Turn to the right last time
Wait ( 2000 ) ; // Wait 2 seconds
SetServo ( 2 , 127 ) ; // Turn back straight
}
[/INDENT]// Lift is up at this point and the top switch is still pressed
// Head continues to turn side to side
}
[/INDENT]// Lift still up and will not return due to top limit switch !!!
// Need a override for the limit switches !!!
Bumper2 = GetDigitalInput ( 8 ) ; // Same 12ch. Remote with Relays
if ( Bumper2 == 0 ) // If 2ch. Relay is closed[INDENT] {
Wait ( 2000 ) ; // After pressing button wait 2 seconds
SetMotor ( 1 , 0 ) ; // Run Motor Down TRY ADDING A PAUSE HERE
}
[/INDENT]else[INDENT] {
Limit2 = GetDigitalInput ( 6 ) ; // Bottom limit switch to stop motor
if ( Limit2 == 0 ) // If limit switch is closed[INDENT] {
SetMotor ( 1 , 127 ) ; // Stop motor
}
[/INDENT]}
[/INDENT]Bumper3 = GetDigitalInput ( 9 ) ; // Same 12ch. Remote with Relays
if ( Bumper3 == 0 ) // If 3ch. Relay is closed[INDENT] {
SetMotor ( 3 , 255 ) ; // Rotation Motor
}
[/INDENT]else[INDENT] {
SetMotor ( 3 , 127 ) ;
}
[/INDENT]}
[/INDENT]}

Secondly, I’m not sure you’re whole problem is limit switches, although I concur with LegoMindstormmaniac’s point about considering the upper limit switch only when trying to go up and considering the lower limit switch only when trying to go down.

It appears to me that your code checks for the instantaneous status of Bumper1, then either sets the motor running upward **or **checks the upper limit switch. If the upper limit has been reached, it stops the motor and wiggles Servo 2. If the upper limit has not been reached, the motor is left running upward. It appears that if you continue to hold Bumper1, the upper limit switch is ignored.

Your code then checks the instantaneous status of Bumper2. If Bumper2 is pressed, it starts the motor downward. If Bumper2 is not pressed, it checks the lower limit switch. If the lower limit switch is pressed, it stops the motor. If the lower limit switch is not pressed, it leaves the motor in its previous state. It appears that if you continue to hold Bumper2, the lower limit switch is ignored.

Bumper3 is then checked and Motor 3 is started if Bumper3 is pressed, otherwise Motor 3 is stopped.

The code then loops through this ad infinitum. So, if you’re at the top limit and holding Bumper2, it will start the motor downward, then loop back to the top and, because the upper limit switch is stll pressed, stop. If you add a pause where I suggested, it will have some time to run the motor downward before it checks the upper limit switch again. If you make the pause long enough, the motor will be able to continue downward when the code loops.

There’s a similar issue to address in the relationship among Bumper2, the lower limit switch, and the downward motion.

Good luck,
Eric

I didn’t have a chance today to work on the code, But I did find out that if the
Bumper is held down it does cause the limit switch to not work.
So, I’ll have to work the code some more and see if I can figure out what to do.
Calvin

I guess I’m starting to understand more about what you guys are telling me.
So I’m rewriting my code and this is what I have so far.

#include “Main.h”

void main ( void )
{
int Limit1 = 0;
int Limit2 = 0;
int Bumper1 = 0;
int Bumper2 = 0;
int Bumper3 = 0;

  while ( 1 == 1 )
  {
        Bumper1 = GetDigitalInput ( 1 ) ; // 12 Channel Remote with Relays
        if ( Bumper1 == 0 ) // Relay Closed
        {
              Limit2 = GetDigitalInput ( 4 ) ; // Top Limit
              if ( Limit2 == 1 ) // If switch is open
              {
                    SetMotor ( 1 , 255 ) ; // Run up Motor
              }
              else // If closed stop motor
              {
                    SetMotor ( 1 , 127 ) ; // Stop motor
                    Wait ( 3000 ) ; // Wait 3 seconds
                    SetMotor ( 2 , 255 ) ; // Turn servo right
                    Wait ( 4000 ) ; // wait 4 seconds
                    SetMotor ( 2 , 0 ) ; // Turn servo left
                    Wait ( 4000 ) ; // Wait 4 seconds
                    SetMotor ( 2 , 127 ) ; // Turn servo forward
                    Wait ( 2000 ) ; // Wait 2 seconds
                    SetMotor ( 2 , 255 ) ; // Turn servo right
                    Wait ( 1000 ) ; // Wait 1 second
                    SetMotor ( 2 , 127 ) ; // Turn servo forward
              }
        }
  }

}

It’s actually working much better.
I push/hold bumper1 and the motor goes up hits the top limit2 switch and the motor stops.
Then the servo wiggles back/fourth.
Great, Now to get it back down :o)
Calvin

Sorry to post so much, But I got it working up/down like a CHARM!!!
The OLD GOAT got it.
Thank you so much for pointing me in the right direction.
Calvin

Sorry to come to this discussion so late. Post # 8 in this thread addresses the same issue. Sounds like you’ve got it fixed, though.