Code for IR Break Beam

I’m a relatively inexperienced programmer trying to code an IR Break Beam sensor for my FRC team’s robot. I need code that spins a motor when the beam is broken. How would I write that code?

My team codes in Java and uses WPI Command-Based Programming.

2 Likes

I’m going to dare to recommend the ‘Trigger’ class, which allows you to define conditionals (Boolean-returning methods) that act in a similar way to the controller buttons you already bind commands to.

3 Likes

We use digital input on the roborio for breakbeam sensors.

Here are wpilib docs for that:

An Arduino tutorial for our breakbeam that we used:

2 Likes

I agree with both of the above!

Here is Team 3468’s code from the last two seasons. We used three of these IR Reflection sensors (same exact concept except both sensors are on the same side, and you calibrate the sensitivity such that you get a reflection when an object is in the way.)

The sensors themselves are read through DigitalInput objects in our Conveyor Subsystem.

Then we use them to trigger commands in our RobotContainer class by setting them up as the condition to Trigger objects using Lambdas.

and then using those Trigger objects to schedule commands very similar to Button objects, through the method names are a tad different.

I realize our triggering of commands there can look a little confusing as we were chaining multiple sensors along with a button inputs to automate our entire intake and firing process. However you can very much keep it simple like:

conveyorDetector.whileActiveContinuous(new RunMotor());
3 Likes

You are a lifesaver!!! I actually have working code now. thank you so much :slight_smile:

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.