Skip to content
The banana man edited this page Feb 22, 2024 · 7 revisions

LEDs


Setup

LED Objects can be used with the AddressableLED class in the edu.wpi.first.wpilibj package.
AddressableLED constructor : public AddressableLED(int port)

In order to actually modify the LEDs, you will need to use the AddressableLEDBuffer object found in the same package.
AddressableLEDBuffer constructor : public AddressableLEDBuffer(int length)
Length refers to the amount of LEDs, or "pixels" on the LED strip.

Then, you need to set the length (amount of LEDs) of your AddressableLED object using the respective setLength method. Note that you should never ever call getLength

And finally, you need to set the data of the LED using AddressableLED#setData(buffer), and initiate it with AddressableLED#start.

Your code should end up looking something like this:

// The following code is a snippet from the LED subsystem I wrote.
private AddressableLED led;
private AddressableLEDBuffer buffer;

private static final int port = 0; // The PWM port of the LED strip
private int length; // How many leds, or "pixels" are on the strip

public LEDSubsystem(int pixels) {

	this.length = pixels;

	buffer = new AddressableLEDBuffer(pixels);

	led = new AddressableLED(port);
	led.setLength(pixels);
	led.setData(buffer);
	led.start();

	led.setData(buffer);

}

LED Usage

Actually using the LEDs is very easy. The AddressableLEDBuffer class has various methods to make your life much easier.

Setting specific pixels

NOTE: You need to update the AddressableLED's data after making changes to the buffer!


There are two different methods that you can use to set specific pixels:
- `AddressableLEDBuffer#setRGB`
- `AddressableLEDBuffer#setHSV`

\> You can also loop though every pixel using a for loop to set all pixels.

\> After setting the buffer's pixels, you need to update it using `AddressableLED#setData(buffer)`. It is recommended to do this after you are finished changing pixels rather than every time you change a pixel.

LEDs are very cool. 👍👍👍

Clone this wiki locally