Running a command with a sensor

What would be the easiest way to run a command off a sensors value?

I have a proximity switch that is a digital input, i want to close a solenoid when the proximity switch detect the gear.

Can i program that i my OI just like a joystick button?

		gearGobblerClose = new JoystickButton(driverJoystick, 12);
		gearGobblerClose.whenPressed(new Teleop_GearGobblerClose_Command());

Sounds like you need a Trigger.

1 Like

In OI.java


		GearSensorAnalogSwitch gearSwitch = new GearSensorAnalogSwitch();
		gearSwitch.whenPressed(new IntakeSetPosition(IntakePosition.GEAR_PRESENT));

Create a class that derives from Button to return the sensor value. The get() method just needs to return true/false from your sensor. For this example, save the following into GearSensorAnalogSwitch.java

package org.usfirst.frc.team3310.buttons;

import org.usfirst.frc.team3310.robot.Robot;

import edu.wpi.first.wpilibj.buttons.Button;

public class GearSensorAnalogSwitch extends Button {

	public boolean get() {
		return Robot.gearIntake.isGearPresent();
	}
}

For this example, the GearIntake subsystem has an AnalogInput named gearSensor and the isGearPresent() method return true/false based on the output voltage. If you have a digital sensor use a DigitalInput and return myDigitalSensor.get():

	public boolean isGearPresent() {
		return gearSensor.getAverageVoltage() > 2.5;
	}
	

got it to work !!! thank you so much !

I love the name Gear Gobbler.

Nom nom nom.

the whole things have a lot of different name that fortunately cannot be put on here!!! :ahh: :ahh: :ahh: :ahh: :ahh: