Relay Button Issue

We have two buttons that control one spike but in the code only one works if the other is commented out. How should we go about fixing this?

Well, the first thing you could do is show us your code.

Well, I coded an on/off switch recently and I came up with a one button toggle after a two button toggle.


public void teleopPeriodic()
{
    boolean spikeToggle = false;
    spike.set(Relay.Value.kOff);
    if(joy1.getRawButton(BUTTON_NUM)) //set the button to activate motor for the joystick
    {
        if(spikeToggle == false) // if the toggle is off, set to true and activate motor
        {
            spikeToggle = true;
            spike.set(Relay.Value.kOn);
        }
        Timer.delay(0.5); // cannot change the value within the first half second of activating, you can change value but it is here to have a safe toggle without toggling every millisecond
        else if(spikeToggle == true) // if the motor is on, turn it off and it is set to false
        {
            spikeToggle = false;
            spike.set(Relay.Value.kOff);
        }
        Timer.delay(0.5); // again it stalls the change for convenience and safety
    }
}

Two buttons were crazy, but if you want two buttons, just replace the else if with the if statement and add it to the second button loop. Hope this helped.

If you have any questions and comments, just reply or post, etc.

Happy Competitions!

So, what is the problem? I assume you fixed it? Regardless, you might want to create a separate thread in teleOp periodic since you have the 0.5s delays in there.

Here is how I did one button with CommandBased

Use something like (its explained in the OI class):

ButtonObject.whenPressed(new ToggleSpike());

then inside the command my code:

private boolean state = false;
    private boolean previousState = true;
    
    
    public SetFeedBelt() {
        // Use requires() here to declare subsystem dependencies
        // eg. requires(chassis);
        requires(feedBelt);
    }

    // Called just before this Command runs the first time
    protected void initialize() {
    }

    // Called repeatedly when this Command is scheduled to run
    protected void execute() {
        state = !state;
        feedBelt.setBelt(state);
    }

    // Make this return true when this Command no longer needs to run execute()
    protected boolean isFinished() {
        if(state != previousState) {
            return true;
        } else {
            return false;
        }
    }

    // Called once after isFinished returns true
    protected void end() {
        previousState = state;
    }

So with two buttons make it two button.whenPressed(new onOrOffCommand()); and two commands, and if they require the spike subsystem it will handle the interrupts for you.