Programming LED's with Roborio, but LED's stuck!

I am programming our LED’s for the robot this year and a mentor suggested using the new LED library for this year instead of an arduino. I looked at some example code that seamed to be working, but it was using HSV instead of RGB which is what I was used to. The line of code that set a specific pixel on our LED strip to a color was:
m_ledBuffer.setHSV(pixel, hue, saturation, value);

I found another line that used RGB instead of HSV:
m_ledBuffer.setRGB(pixel, red, green, blue);

Using this line, I created a simple ‘for’ loop that would set every pixel on our 60 pixel LED strip to (255, 0, 0,) meaning red:

for (var i = 0; i < 60; i++) {
m_ledBuffer.setRGB(i, 255, 0, 0);
}

This code turned on the whole LED strip, but each LED was a different color, still making the rainbow pattern of the original function. I set different LED’s to red, but each one was stuck on their original color. It seems I can only turn on specific LED’s but I can’t change their colors. Has anybody encountered this problem or know how to fix it? Thanks!

Did you remember to set the LED strip to the buffer at changing it?

After setting your buffer, try using the following:
m_led.setBuffer(m_ledBuffer);

3 Likes

It doesn’t look like I did that in the code. I actually don’t have the code with me, but I have the original code that I modified, and it didn’t have that line. The one thing that doesn’t make sense to me, is that the original code was working without that line, but I will try it when I get the chance. Thanks!

package edu.wpi.first.wpilibj.examples.addressableled;
import edu.wpi.first.wpilibj.AddressableLED;
import edu.wpi.first.wpilibj.AddressableLEDBuffer;
import edu.wpi.first.wpilibj.TimedRobot;
public class Robot extends TimedRobot {
private AddressableLED m_led;
private AddressableLEDBuffer m_ledBuffer;
// Store what the last hue of the first pixel is
private int m_rainbowFirstPixelHue;
@Override
public void robotInit() {
// PWM port 9
// Must be a PWM header, not MXP or DIO
m_led = new AddressableLED(9);
// Reuse buffer
// Default to a length of 60, start empty output
// Length is expensive to set, so only set it once, then just update data
m_ledBuffer = new AddressableLEDBuffer(60);
m_led.setLength(m_ledBuffer.getLength());
// Set the data
m_led.setData(m_ledBuffer);
m_led.start();
}
@Override
public void robotPeriodic() {
// Fill the buffer with a rainbow
rainbow();
// Set the LEDs
m_led.setData(m_ledBuffer);
}
private void rainbow() {
// For every pixel
for (var i = 0; i < m_ledBuffer.getLength(); i++) {
// Calculate the hue - hue is easier for rainbows because the color
// shape is a circle so only one value needs to precess
final var hue = (m_rainbowFirstPixelHue + (i * 180 / m_ledBuffer.getLength())) % 180;
// Set the value
m_ledBuffer.setHSV(i, hue, 255, 128);
}
// Increase by to make the rainbow “move”
m_rainbowFirstPixelHue += 3;
// Check bounds
m_rainbowFirstPixelHue %= 180;
}
}

This is the main code that I modified. It was working fine.

Oh no! Sorry about the lack of indentation! Programmers nightmare!

All good! And I stand corrected, its not m_led.setBuffer(m_ledBuffer), its m_led.setData(m_ledBuffer);

You’re also calling it in robotPeriodic() which means it will always set the LEDs to what looks like a rainbow pattern in your case.

1 Like

So how do I make it set the LED’s to the specified color? Do I move the m_led.setData(m_ledBuffer); and if so, where?

You’re setting the LEDs correctly, it’s just that you’re always setting it to the rainbow pattern which is why it was originally working.

My suggestion is to first move these lines out of robotPeriodic() and into robotInit() so that it only runs once:
// Fill the buffer with a rainbow
rainbow();
// Set the LEDs
m_led.setData(m_ledBuffer);

Once you move the code to robotInit(), try creating and setting a new pattern in teleopInit() and autoInit() just to see if the pattern sticks and if the issue was that you’re repeatedly setting the LEDs to rainbow.

Side note: Making an LED subsystem might be easier, you should be able to create your own patterns and then call them through commands if you’re using command based. Even if you aren’t using command based, it’ll make your code much cleaner.

1 Like

In the modified code, that is more or less what I did. I modified rainbow() to set the LED’s to be all red, and that is where I ran into the problem, but I have yet to move the m_led.setData(mledBuffer); to robotInit(). I will try that and don’t worry, this was just a test project for out robot, and the LED code will be moved to its own subsystem soon. Thanks for all your help!

When you post code, enclose it with three ` characters.

it makes a nice code block
1 Like

Great! Thanks!

Ok I failed. It turns out that each LED does take the values I give it, but they all seem to scramble the inputs. For example, for LED 1, I give it 255,0,0 and it makes it green. For LED 20, I give it 255,0,0 and it makes it red! WHAT IS THIS!

It sounds like your LED strip might be taking RGBA? What LED strip are you using?

1 Like

My coding mentor said something about getting a new strip because he thinks it is a RGBW. I am not sure, but we will probably be testing that tomorrow.

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