Help, Limit Switch when triggered not pressed down

Relatively new to VEXcodePro V5 and C++, so bare with me. I have some kids who have built a duck hunt shooting gallery game. They have a tank tread conveyor belt with ducks on it, and a limit switch behind each duck. They want the tread to move back and forth for a duration of time, and each time a duck is hit (limit switch tiggered) a different motor (image of the hunter) moves up and down in celebration. The hunter is taped to a cam and follower. The program works, but only when the limit switches are pressed for over a second. How can they get the limit switches to work when they are triggered, not held down?

here is the program.

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// water                motor         1               
// dave                 motor         2               
// bumperA              bumper        A               
// duck1                limit         C               
// duck2                limit         D               
// duck3                limit         E               
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "vex.h"

using namespace vex;

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
while(true){
//once bumper is pressed, the tread begins reciprical movement back and forth 
if(bumperA.pressing()){
//moves ducks on tread back and forth a total of 15 seconds, Dave the hunter (cam/follower) moves up and down at the low rpm. 
  repeat(10){
  dave.spin(forward);
  dave.setVelocity(25,rpm);
  water.spin(forward);
  wait(.7, seconds);  
  water.stop();
  water.spin(reverse);
  wait(.7, seconds);
  water.stop();
  dave.stop();

// if duck 1,2,or 3 (limit switches) are hit, Dave the hunter moves up and down at 200 rpm for 3 seconds in celebration, then returns to 25 rpm.
if(duck1.pressing() or duck2.pressing() or duck3.pressing()){
  dave.spin(forward);
  dave.setVelocity(200,rpm);
  wait(3,seconds);
  dave.stop();
} 
      
}


}
}
}

thanks for any feedback.

The conditional to check if a duck is hit will only execute after all the animation for the ducks moving finishes in every loop of the program. That is why you have to hold down the limit switches, because the code won’t check until it finishes those .7 second waits.

Two ways to accomplish what you are looking for would be to use Events or run multiple Threads. If you go into the Examples in VEXcode you can see some ways to structure either of those.

2 Likes

Outside of your while(true) you’ll want to register an event handler for each of the 3 limit switches. Before that, you’ll want to pull the code inside that if statement into a function. This is the general gist, I don’t know that it will actually compile:

#include "vex.h"

using namespace vex;

int isCelebrating = 0;

void daveCelebrate() {
  if (isCelebrating == 0) {
   isCelebrating = 1;
  dave.spin(forward);
  dave.setVelocity(200,rpm);
  wait(3,seconds);
  dave.stop();
  }
  isCelbrating = 0;
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  duck1.onpress(daveCelebrate);
  duck2.onpress(daveCelebrate);
  duck3.onpress(daveCelebrate);

while(true){
//once bumper is pressed, the tread begins reciprical movement back and forth 
if(bumperA.pressing()){
//moves ducks on tread back and forth a total of 15 seconds, Dave the hunter (cam/follower) moves up and down at the low rpm. 
  dave.spin(forward);
  dave.setVelocity(25,rpm);

  repeat(10){
  isCelebrating = 0;
  water.spin(forward);
  wait(.7, seconds);  
  water.stop();
  water.spin(reverse);
  wait(.7, seconds);
  while(isCelebrating == 1) { 
   wait(.1, seconds);
  }
  water.stop();
  dave.stop();

}


}
}
}

Above probably doesn’t do exactly what you want, and there’s more logic in it than I thought when I started re-writing it. Basically, you want to have the limit switches fire an event to make Dave celebrate. But you probably don’t want the game to continue running while he’s celebrating, so I added the global variable isCelebrating so the main loop can see if he’s celebrating and wait for him to finish.

1 Like