Limit Switch VCS Programming Help


#1

Hello,
I am trying to use a limit switch to load 2 balls onto my robot at once. My idea was to put a limit switch behind the ramp that is used to guide the balls up to our launcher. So I wanted to program the switch so that if it is pressed once by the ball, it stops the intake system, and if it is not pressed, then nothing happens. If someone could give me some help on this, it would be much appreciated.


#2

Here is how you configure the limit switch inside of VCS:
https://help.vex.com/article/74-how-to-configure-the-limit-switch-in-vcs

You can get a detailed example at the VCS Command Reference page here:
https://help.vexcodingstudio.com/#cpp/namespacevex/classvex_1_1limit/pressing

Here is an example of what you’re thinking of doing. You’re first wanting to determine if the Limit switch is pressed, which you use .pressing() to do. Then tell the motor to stop.

# Stop the Intake when Limit Switch is pressed
if (Limit1.pressing()) {
    Intake.stop(brakeType::coast);
}

#3

Thank you for your feedback.

It works, but it only stops the intake once for a short amount of time. I was also wondering how to make the intake stop once it is pressed until it is turned back on manually on the controller. Thanks.


#4

Before you added the Limit switch, what made your intake turn on & off?

Do you press a button? Or is it something else that triggers the intake? Or is this autonomous?


#5

Here is my intake code:

if (Controller.ButtonDown.pressing())
{
if (!buttonPressed)

		{
			motorUp = !motorUp;	// Motor = not current motor value
			buttonPressed = true;		// Marks button as "pressed"
			motorDown = false;
		}
	} else if (Controller.ButtonUp.pressing())
	{
		if (!buttonPressed)	// If the button is pressed and was not pressed previously
		{
			motorDown = !motorDown;	// Motor = not current motor value
			buttonPressed = true; // Marks button as "pressed"
			motorUp = false;
		}
	} else
	{
		buttonPressed = false;	// Marks button as "not pressed"
		if (motorUp)	// If motor should be turned on, speed sets to 100.
		{
			intakeSpeed = 100;
		}
			else if (motorDown)
		{
			intakeSpeed = -50;
		}
		 else	// If motor should be off, speed sets to 0.
		{
			intakeSpeed = 0;
		}
	}

Put simply, if you press ButtonDown once, the intake turns on until you press ButtonDown again. For the outtake, it is the same concept, except you are pressing ButtonUp instead of ButtonDown.


#6

I would suggest a different approach. I’m going to assume you may want 1 ball or may want 2 balls. You can always adjust.

int ballsHeld = 0; // I’m working with operator control here. Start differently for autonomous. This value can be 0, 1, or 2.

void ballPickedUp(void) {
   ballsHeld++;
}

/* This next part is within user control. */

Limit1.pressed(ballPickedUp);

while(true){
   if (ballsHeld == 2) {
      Intake.stop(brakeType::coast);
   }
   else {
      Intake.spin(); // insert whatever values/directions go here.
   }
}

Also, depending on what you have for a firing command, include the following when you fire the balls:

ballsHeld = 0; // if you have a double catapult or similar
ballsHeld = ballsHeld - 1; // if you have a single launcher

#7

Nice! Didn’t see that as a possibility in the Reference. That will be useful to show the students.


#8

For some reason, when I insert the while(true){ loop, nothing else works. When I took the loop out, nothing happened when the switch was pressed. Did I do something wrong?

//vex::competition

int ballsHeld=0;
void ballPickedUp(void) {
ballsHeld++;

//user control
Limit1.pressed(ballPickedUp);

if (ballsHeld == 2) {
    Imp1.stop(vex::brakeType::coast);
}
else {
  Imp1.spin(vex::directionType::rev,intakeSpeed,vex::velocityUnits::pct);
}

#9

That while(true) should already exist in your user control. You have to put Limit1.pressed() before it and the if statement to spin the intake inside of it. If you put a second while(true) in your user control, then you’ll never exit the first to get to the second.


#10

when i turn the controller to left, the robotics turn to right, is the robot setting wrong or the vcs program wrong??? Can anyone help me , i don’t know what the controller setting “Axis” mean!!!


#11

two ball! It was very different , i think you shouldn’t used limit switch!!!