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