I'm trying to use a RGB LED with my UNO R3. Each colour is controlled by a PWM pin. Testing it, I can easily control what colour it is, correctly. But there is a problem: Using the FastLED library, I made a rainbow gradient for it. To do this, I iterated through all the Hue values in HSV, and then converted it to RGB for the LED. When I attempt to run this code, it works fine until the rainbow part. It cycles from red to green, then red to green again, repeating about 4 times. The same thing happens from green-blue, and blue-red. It's supposed to cycle through all these values once, and smoothly. I don't know what the problem in the code is. I tried using the serial output to see what values there are, but I can't see a problem. I also tried changing the method of hsv2rgb from 'rainbow' to 'spectrum', but that also didn't work. It was the same. I tried swapping the wires around but that also didn't work.
Code:
#include "FastLED.h"
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
int rgbset(int paramR, int paramG, int paramB) {
int redVal = 0 + ((1023 - 0) / (255 - 0)) * (paramR - 0); // convertss the RGB values into the PWM range
int greenVal = 0 + ((1023 - 0) / (255 - 0)) * (paramG - 0);
int blueVal = 0 + ((1023 - 0) / (255 - 0)) * (paramB - 0);
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
}
void loop() {
rgbset(255, 0, 0); // this part works fine, it easily changes to the correct colours
delay(500);
rgbset(255, 255, 0);
delay(500);
rgbset(0, 255, 0);
delay(500);
rgbset(0, 255, 255);
delay(500);
rgbset(0, 0, 255);
delay(500);
rgbset(255, 0, 255);
delay(500);
for (int hue = 0; hue < 256; hue++) { // here's the problem zone
CHSV hsv(hue, 255, 255);
CRGB rgb;
hsv2rgb_rainbow(hsv, rgb);
rgbset(rgb.r, rgb.g, rgb.b);
delay(50);
}
}
If it is an RGB LED where each pin (R, G, B) is controlled by a PWM signal then you cannot control it using the FastLED library which controls addressable LEDs
I'm just using FastLED to convert from HSV to RGB for the rainbow gradient, which is then sent to the LED through analogWrite. Unless that does actually make a difference?