Toggle or Digital Switches

Hi, we had a question regarding the autonomous period of competition. Our team has always used toggle switches and true / false conditionals, though we were curious if there is anyway to make a digital toggle switch on the smart dashboard that we could press shortly before the beginning of autonomous. Also, we were curious if this was even legal. Thanks for the help!

Perfectly legal. This is perfectly functional, albeit 2 years old.

When looking through some stuff, I found a Reddit post with a way that seems effective. When I tried it on the code below I get an odd error. When we select a mode, it works fine. When we disable, select another mode, and re-enable the program, it runs both cases at the same time. Any ideas anyone?


package org.usfirst.frc.team2130.robot;


import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.command.Scheduler;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.CANTalon;
import edu.wpi.first.wpilibj.IterativeRobot;

public class Robot extends IterativeRobot {
	
    RobotDrive myRobot;
    CANTalon talon;
    CANTalon talon1;
    final String defaultAuto = "Default";
    final String customAuto = "My Auto";
    SendableChooser chooser;
    int mode = 1;

    public Robot() {
        myRobot = new RobotDrive(0, 1);
        myRobot.setExpiration(0.1);
        talon = new CANTalon(2);
        talon1 = new CANTalon(4);
    }
    
    public void robotInit() {
        chooser = new SendableChooser();
        chooser.addDefault("Run 2", 1);
        chooser.addObject("Run 4", 2);
        SmartDashboard.putData("Auto modes", chooser);
    }

    public void autonomousInit() {
    	mode = (int) chooser.getSelected();

    }
    
    public void autonomousPeriodic(){
    	switch(mode) {
	case 1:
		talon.set(0.25);
		break;
	case 2:
		talon1.set(1);
		break;
	}
    }
    
    public void operatorControl() {
        myRobot.setSafetyEnabled(true);
        while (isOperatorControl() && isEnabled()) {
            talon.set(0.25);
        }
    }
    
    public void test() {
    }
}

Why do you think both are running? If it’s just because both talon and talon1 are providing outputs, notice that nothing in your code ever turns either of them off. I think what’s happening is that the first selected mode turns on one of the talons, then disabling the robot “turns it off”, but it’ll turn right back on again when the robot is reenabled. The second selected mode does its thing, ending up with both talons on.

You should probably set both talons to zero in autonomousInit() so you have a known starting state.