Intake not stopping + wings code not working

In my driver control code, I have it so buttong L1 and L2 control the intake so it intake with L1 and extakes with L2 and if its already intaking and you click L1 again it stops the intake. For some reason, when i just click L2 (extake) and then click it again it works how its supposed to (extakes then stops) but when I click L1 (intake) it intakes but doesn’t stop (I like have to spam it multiple times and then it might stop) OR i have to switch to extake then itll stop.

I thought it might be because the button was sticky so i switched to a different controller and even switched the buttons around on my code but the intake function simply doesn’t stop. I’m very confused on what’s happening

Intake Code


///////////////////////////////
//Control Functions

void intakeIN() {
  if (intake.direction() == forward) {
    intake.stop();
    Controller1.Screen.print("stop");
  }
  else {
    intake.spin(forward);
  }
}

void intakeOUT() {
  if (intake.direction() == reverse) { 
    intake.stop();
    Controller1.Screen.print("stop");
  }
  else {
    intake.spin(reverse);
  }
}

Wings problem:
I have this code for the wings so that the front wings are controlled by only one button (R1). In theory, you click it once, extends, click it again, unextends. However, the code only extends and then never unextends even when I click again, I think it might be because its going directly through to the next loop.

I have tried replacing it with if else statement but for some reason that just doesn’t make it extend at all

(also the controller screen prints are just for testing)

Wings code:

bool frontWingOpen = false;

void frontWings(){
  if (frontWingOpen){
    frontWingOpen = false;
    wingFL.set(false);
    wingFR.set(false);
    wait(3,seconds);
    Controller1.Screen.print("close");
  }
  while (frontWingOpen == false){
    frontWingOpen = true;
    wingFL.set(true);
    wingFR.set(true);
    wait(3,seconds);
    Controller1.Screen.print("open");
  }
  wait(200,msec);
}

Also side question- is there any boolean that automatically detects whether a piston is extended or not like the one telling if a motor is spinning or not

I managed to solve the wings problem by changing to if-else and moving the frontWingOpen = true/false to the end of the section but the intake one remains

Let’s start with the wings code.

First of all, when just trying to toggle a cylinder, you can use Cylinder.value() to get its current state. So your wings code could just be:

void frontWings() {
  wingFL.set(!wingFL.value());
  wingFR.set(!wingFR.value());
}

I assume the wait 3 seconds and print is just for debugging purposes, so I didn’t include them.

Now let’s look at the intake:
The Motor.direction() command, at least in my experience, doesn’t work great, and its documentation is incomplete, so I’d just use an integer variable to keep track of the intake:
1 = forward
0 = stopped
-1 = reverse

int intakeDir{ 0 };

void intakeIN() {
  if (intakeDir == 1) {
    intake.stop();
    intakeDir = 0;
  } else {
    intake.spin(forward);
    intakeDir = 1;
  }
}

void intakeOUT() {
  if (intakeDir == -1) {
    intake.stop();
    intakeDir = 0;
  } else {
    intake.spin(reverse);
    intakeDir = -1;
  }
}

So, looking at your intake function I see 2 potential problems :

  1. You aren’t using a cooldown (or sometimes called a debounce) which may cause double firing
    Basically a cooldown is something like this :
bool cooldown = false; // set to false to show that you are not on cooldown
void littleCooldownfunction(){
 if(cooldown) return; // This allows the function to exit if another instance is running
cooldown = true; // Engage cooldown
// Do something here
task::sleep(150); // A wait between presses so that you prevent "double clicking"
cooldown = false; // Disengage cooldown
}
  1. You may have to use a boolean, motor functions like intake.direction() or intake.isSpinning() havent worked for me so instead I do something like this:
bool cooldown = false; 
bool isIntakeSpinningForward = false;
void intake(){
if(cooldown) return;// exit the function if on cooldown
if(isIntakeSpinningForward){ // if the intake is spinning 
Intake.stop(); // Stop the intake
isIntakeSpinningForward = false; // We are not spinning
task::sleep(150); // Wait 150 millseconds for cooldown
cooldown = false; // Set the cooldown to false
return; // Break out of the function
}
// So if the intake was true the code would have already stopped by now
Intake.spin(forward); // Spin forward
isIntakeSpinningForward = true; // Set the value to true
task::sleep(150); // Wait for 150 millseconds 
cooldown = false; //Stop cooldown
}

If these don’t work let me know and I will provide futher help (they are in order by most likely to fix your issue)

1 Like

You can usepiston.value() to get the value of the piston. This returns 0 or 1 depending on the state of the piston.