RGB LED rainbow gradient not in correct order

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);
  }
}

Welcome to the forum

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?

your UNO does not do PWM on 10 bits, it only uses 8 bits

try

#include <FastLED.h>

int redPin = 9;
int greenPin = 10;
int bluePin = 11;

int rgbset(int paramR, int paramG, int paramB) {
  analogWrite(redPin, paramR);
  analogWrite(greenPin, paramG);
  analogWrite(bluePin, paramB);
}

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

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);
  }
}

Yeah that fixed it. I don't know where I heard it, but it must have been a different Arduino board that has 10-bit PWM. Thank you! :slight_smile:

may be you were confused by analogRead which is 10 bits on your UNO?

see analogWriteResolution() - Arduino Reference

1 Like

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