Yet another code question using the toggle when pressed command

Well now I’m working on doing something for our blinkin led driver I want to use a few different buttons to toggle the lights to different patterns and was trying something like this but having it not work. Is there a better way or is it possible to fix it?

// if(m_stick.getrawButton(3)) {
// toggleWhenPressed(m_led_driver.set(.5),m_led_driver.set(0));
// }

See the screensteps live page for how to use toggleWhenPressed.

https://wpilib.screenstepslive.com/s/currentCS/m/cpp/l/241904-running-commands-on-joystick-input

The bottom line is that it should NOT be in an if statement driven by getrawButton, but called as a method of the button. Usually, it is called once during autonomous or teleop init.

I found much more reliability having a variable 1 that i would just negate each time swapping it from 1 to -1 and so on. Then just on press toggle that and run the correct command from there.

any tips on how to do that? I’m still learning code as I work on this project so any help is appreciated (sorry if I’m being difficult or asking stupid questions too)

int val = 1;
if (but.get()) val = -val;

if (val == 1) doSomething();
else doSomethingElse();

I’m also getting this error when trying to make a new button:

C:\Users\Administrator\Desktop\t-SHIRT launcher\t-shirt_Launcher\t-shirtattempt\src\main\java\frc\robot\Robot.java:49: error: cannot find symbol
private JoystickButton button;
^
symbol: class JoystickButton
location: class Robot
1 error

Have you imported the joystickbutton class?

Are you doing this in command based?

That was my next question.

If yes, then the right answer is to try and have one command running that’s looking for this variable to change or to create a standard java class that could instead send a data point to the runner class that’s doing the heavy lifting.

I’m trying to do that (I originally thought it was part of the joystick import) I tried this: import edu.wpi.first.wpilibj.JoystickButton; but that didn’t work what is that class?

also would this one work better for me?

class Robot extends IterativeRobot {
bool button_prev = false;

teleop() {
    if (button_prev != joystick.getButton(BUTTON_NUM)) {
      if (joystick.getButton(BUTTON_NUM)) {
        // joystick pressed
    } else {
        // joystick released
    }
}

button_prev = joystick.getButton(BUTTON_NUM);
}

}

Let me ask you a question that might better help the process. Do you have a github that you could link that would better contextualize the problem?

I can upload the code I have so far to github I don’t have seperated classes yet so I’ll warn you there just been putting the code all in one since it is isn’t to many parts

I think you’re writing more code than you need to going this route. You can already do a whenPressed/whenReleased(new SomeCommand());

It seemed like you wanted a press once for one thing and then press again to either do a different thing or stop the current thing.

1 Like

Thanks, any code is better than no code. Maybe we can help you separate things in a way that makes things easier to read and verify.

Feel free to link the page whenever it’s uploaded.

Will this work: https://github.com/820816/t-shirtlauncher/blob/master/Robot.java

“T-Shirt Launcher”…

yeah thats our current project with our FRC stuff we are doing vex now which isn’t as fun. We’re using an arm system to launch the shirts. Our FRC programmer graduated last year so nobody learned the code being one of the head members and next years president I got the fun job of the code

this is an old video of some testing: https://vimeo.com/297923142