Limit Switch

In java I’m trying to code a limit switch to my single motor called hunt


package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.Watchdog;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.Jaguar;

public class Pup extends SimpleRobot {

    private static final long TIME_DELAY = 1000; // in milliseconds
    RobotDrive drive = new RobotDrive(1, 2);
    Joystick leftstick = new Joystick(1);
    Joystick rightstick = new Joystick(2);
    Jaguar hunt = new Jaguar(4);
    Joystick spot = new Joystick(4);
    Joystick kicker = new Joystick(3);
    Watchdog fenrir = Watchdog.getInstance();
    Compressor fluffy = new Compressor(1, 1);

    void setUpRobot() {
        fluffy.start();
        fenrir.feed();
    }
    
    public void autonomous() {
        setUpRobot();
        fenrir.setEnabled(false);
        while (true && isAutonomous() && isEnabled()) {
            drive.drive(1.0, 0.0);  // drive 100% fwd 0% turn
        }
    }

    public void operatorControl() {
        setUpRobot();
        while (true && isOperatorControl() && isEnabled()) // loop until change
        {
            drive.tankDrive(leftstick, rightstick);
            hunt.set(kicker.getY());
            Timer.delay(0.005);
            fenrir.feed();
        }    
    }
}



but i want it so that when it hits it, it will not go that way

What does “it” mean?

Sorry I want the limit switch to stop the motor from continuing when hit.

Programming for a limit switch is straightforward. You just need to read the value of the switch and test for “active”. Sometimes a limit switch will return a True when it’s inactive, and False when it’s active; sometimes it will be the other way around. You also need to know whether the motor would be moving “forward” or “reverse” when it hits the switch.

When you see that the switch is active and the motor is being asked to travel in the direction you don’t want it to go anymore, set the motor value to zero. That’s it. If the switch is not active, don’t do anything special; that will let the motor move normally. If the motor isn’t being told to go in the direction you don’t want it to go, don’t do anything special; that will let the motor go back away from the limit.

In the API, theres a class called, DigitalInput. Once you create the class and specified with GPIO to keep track of, you can use the get() function to keep track of the state of the switch. Then you can use it as the boolean expression for the “if” statement.

What is the “if” statement for the limit switch.

Say if you made a limit switch and named it switch.

DigitalInput switch = new DigitalInput ( 4, 1 ); //switch on digital side car channel 1

the if may look something like

if ( switch.get () )
//do something when the switch is not tripped
else
//do something when the switch is tripped

Domo Arigato KayyPii-senpai and Alan-senpai

Slight tip:
Take out the true in

while (true && isAutonomous() && isEnabled()) {

.

It’s not a biggy at all, but you only want while(true) when you want it to infinitly loop. With other conditionals, it’s pointless.