Limit switch pressed

So I am new to using sensors and I am learning to use the limit switch. I want the piston set to false when the limit switch is pressed, and the piston set true when a button on the controller is pressed. can anyone advise on what to do?

while (1) {
    // This is the main execution loop for the user control program.
    // Each time through the loop your program should update motor + servo
    // values based on feedback from the joysticks.

    // ........................................................................
    // Insert user code here. This is where you use the joystick values to
    // update your motors, etc.
    // ........................................................................
  
     while(true){
 Leftfront.spin(forward,Controller1.Axis3.position()+Controller1.Axis4.position()-Controller1.Axis1.position(), percent);
 Rightfront.spin(forward,Controller1.Axis3.position()-Controller1.Axis4.position()+Controller1.Axis1.position(), percent);
 Rightrear.spin(forward,Controller1.Axis3.position()+Controller1.Axis4.position()+Controller1.Axis1.position(), percent);
 Leftrear.spin(forward,Controller1.Axis3.position()-Controller1.Axis4.position()-Controller1.Axis1.position(), percent);
     
if( LimitSwitchB.pressed(void )()){
piston.set( false );
}
else if(Controller1.ButtonY.pressed(void ()){
piston.set( false );
}
else if(Controller1.ButtonA.pressed(void ()){
piston.set( true );
}
else { 
piston.set( true );
}
 
wait(20,msec);

Something like this work?

void usercontrol(void) {

bool pistonStatus = false;  //Create a variable to hold state.
  while (1) {
    if(Controller1.ButtonA.pressing()){
      pistonStatus = true;
    }
    else if(Controller1.ButtonY.pressing()){
      pistonStatus = false;
    }
    else if(LimitSwitchA){  //might be LimitSwitchA.pressing()
      pistonStatus = false;
    }
    piston.set(pistonStatus);
    wait(20, msec); 
  }
}
3 Likes

i cant test the code because i have a problem and i dont understand the problem.

the first while loop has an error apparently
[clang] expected unqualified-id

void usercontrol(void) {
  // User control code here, inside the loop
  void


  while (1) {
    // This is the main execution loop for the user control program.
    // Each time through the loop your program should update motor + servo
    // values based on feedback from the joysticks.

    // ........................................................................
    // Insert user code here. This is where you use the joystick values to
    // update your motors, etc.
    // ........................................................................
  
     while(true){
 Leftfront.spin(forward,Controller1.Axis3.position()+Controller1.Axis4.position()-Controller1.Axis1.position(), percent);
 Rightfront.spin(forward,Controller1.Axis3.position()-Controller1.Axis4.position()+Controller1.Axis1.position(), percent);
 Rightrear.spin(forward,Controller1.Axis3.position()+Controller1.Axis4.position()+Controller1.Axis1.position(), percent);
 Leftrear.spin(forward,Controller1.Axis3.position()-Controller1.Axis4.position()-Controller1.Axis1.position(), percent);
     
 if(Controller1.ButtonA.pressing()){
      pistonStatus = true;
    }
    else if(LimitSwitchA){
      pistonStatus = false; }

    piston.set(pistonStatus);
    wait(20, msec); 

You should only have one while loop and remove the void above.
image

void usercontrol(void) {

bool pistonStatus = false;  //Create a variable to hold state.
  while (1) {

// Ill add your code.
 Leftfront.spin(forward,Controller1.Axis3.position()+Controller1.Axis4.position()-Controller1.Axis1.position(), percent);
 Rightfront.spin(forward,Controller1.Axis3.position()-Controller1.Axis4.position()+Controller1.Axis1.position(), percent);
 Rightrear.spin(forward,Controller1.Axis3.position()+Controller1.Axis4.position()+Controller1.Axis1.position(), percent);
 Leftrear.spin(forward,Controller1.Axis3.position()-Controller1.Axis4.position()-Controller1.Axis1.position(), percent);
     
// The updated piston code.
    if(Controller1.ButtonA.pressing()){
      pistonStatus = true;
    }
    else if(Controller1.ButtonY.pressing()){
      pistonStatus = false;
    }
    else if(LimitSwitchA){  //might be LimitSwitchA.pressing()
      pistonStatus = false;
    }
    piston.set(pistonStatus);
    wait(20, msec); 
  }
}
4 Likes

i get error code
Use of undeclared identifier ‘pistonStatus’

I don’t see the error in the example. It should say what line number.

Make sure you have it declared near the top of UserControl
image

4 Likes

IT WORKS!!! thanks for your help.

2 Likes

I may have screwed up. I am using pneumatics for expansion in this season’s game and something is not working. I have narrowed the source of the problem to either the code or the solenoid. I tried to use the code you gave me earlier this year. Can you please check my code to see if the problem is with the code?

bool PistonStatus = false;
  while (1) {
   
   Leftfront.spin(forward,Controller1.Axis3.position()+Controller1.Axis4.position()+Controller1.Axis1.position(), percent);
 Rightfront.spin(forward,Controller1.Axis3.position()-Controller1.Axis4.position()-Controller1.Axis1.position(), percent);
 Rightrear.spin(forward,Controller1.Axis3.position()+Controller1.Axis4.position()-Controller1.Axis1.position(), percent);
 Leftrear.spin(forward,Controller1.Axis3.position()-Controller1.Axis4.position()+Controller1.Axis1.position(), percent);
  
  if(Controller1.ButtonB.pressing()){
  PistonStatus = true ;
  }
  else {
  PistonStatus = false ;
  }
  if(Controller1.ButtonL1.pressing()){
      intake.spin(vex::directionType::rev,190, vex::velocityUnits::pct);
    }
  else if(Controller1.ButtonL2.pressing()){
      intake.spin(vex::directionType::fwd,190, vex::velocityUnits::pct);
    }
    else
    intake.stop(brakeType::brake);
    
  
     
  if(Controller1.ButtonUp.pressing()){
     flywheel1.spin(vex::directionType::fwd,580, vex::velocityUnits::pct);
     flywheel2.spin(vex::directionType::fwd,580, vex::velocityUnits::pct);
   }
   else if(Controller1.ButtonDown.pressing()){
    flywheel1.stop(brakeType::coast);
    flywheel2.stop(brakeType::coast); 
   }

   if(Controller1.ButtonRight.pressing()){
   feeder.spin(vex::directionType::rev,30, vex::velocityUnits::pct);
   }
   else 
   feeder.stop(brakeType::coast);
   }
   
  Piston.set(PistonStatus);
    wait(20, msec);

Honestly I’d just set the value of the piston right where you check if the button has been pressed. For example you can get rid of the bool “PistonStatus” and where it sets the Piston based on that at the end off the loop and replace the button code to look like so:

if(Controller1.ButtonB.pressing()){
Piston.set(true);
}
else{}