How do you program limit switches?

Right now I have a limit switch plugged into one of the ports on the Digital IO. Now what methods/classes do use to tell whether the limit switch is pressed or not. There doesn’t seem to be a “limit switch” and I’m not sure what to use.

Anyone else know?

Im working on the same thing for my team for tomorrow. What are you trying to control with the switch?

Use a DigitalInput. It should be declared as a static variable, like this:

public static DigitalInput limitSwitch;

Initialize it like this:

limitSwitch = new DigitalInput(slot,channel);

To use the limit switch, call the get() method on it, which, assuming you have it wired correctly, will return true if the switch is closed and false if it is open.

Any more questions?

ok, so for the constructor. I pass the slot that it’s plugged into on the DigitalIO for the slot, but what do I pass for channel?

A motorized car jack that stops working when it goes too low or too high

DigitalInput name = new DigitalInput(slot)

fill in slot with the slot on the DIO that the switch is plugged into and name with whatever you want to call it.

name.get() will return you the boolean indicating whether the switch is pressed or Unpressed. If it’s wired right, true will be pressed and false will be unpressed

Thanks, guys! Limit switch is working :slight_smile:

Scissor jack? Very suave just to think of it.

Yup! We use it to change the angle of our shooter.

Awesome! So are we :cool:

Here’s ours:

https://sphotos-a.xx.fbcdn.net/hphotos-prn1/18586_490032097722820_942295922_n.jpg

https://sphotos-a.xx.fbcdn.net/hphotos-frc1/21737_490032087722821_1683332209_n.jpg

https://sphotos-a.xx.fbcdn.net/hphotos-ash3/535494_490032124389484_1217571241_n.jpg

For some of you using command based programming, I noticed that the DigitalIOButton class wasn’t working properly, so I programmed my own DigitalButton class using the very-functional DigitalInput class.


public class DigitalButton extends Button {
    private int channel;
    DigitalInput buttonInput;
    
    public DigitalButton(int channel) {
        this.channel = channel;
        buttonInput = new DigitalInput(channel);
    }

    public int getChannel() {
        return this.channel;
    }
    
    public boolean get() {
        return buttonInput.get();
    }
    
}