FastLED RGB to HSV serial conversion

Im trying to display a RGB color strip through the serial port Using HSV values. I am not sure the bast way to approach this however below is my attempt.

#include <FastLED.h>

#define LED_Pin 5
#define LEDnum 20

CRGB leds[LEDnum];
CHSV ledshsv[LEDnum];


void setup() {
  FastLED.addLeds<WS2812B, LED_Pin>(leds, LEDnum);
  FastLED.setBrightness(100); //to start will probably get set to 225 eventualy
}

void loop() {
  fill_rainbow(leds, LEDnum, 0, 225/LEDnum);
  for(int i = 0; i <= LEDnum; i++) {
   //ledshsv[i] = rgb2hsv_approximate(CRGB(leds[i].r, leds[i].g, leds[i].b));
 }
}

When trying to verify this it seems to completely verify but at the last second will say failed to verify however not give me any error codes. If anyone know why this is happening or a way to serial.print a RGB strip in HSV I would love to hear your takes.

Sorry the // is not suppose to be there however the code has the same problem with or without.

A web search for "rgb2hsv c code" turns up lots of possibilities.

Several examples are in this first hit

Which board are you compiling for? Your code compiles for a Arduino Mega on my system. You have one bug however that results in a warning that can NOT be ignored.

C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025214-10368-1d5wrec.s3yh\sketch_mar14b\sketch_mar14b.ino:18:37: warning: iteration 20 invokes undefined behavior [-Waggressive-loop-optimizations]
    ledshsv[i] = rgb2hsv_approximate(CRGB(leds[i].r, leds[i].g, leds[i].b));

The reason is the <= in the for-loop instead of < so you're writing outside the boundaries of the led array.

#include <FastLED.h>

#define LED_Pin 5
#define LEDnum 20

CRGB leds[LEDnum];

void setup() {
  FastLED.addLeds<WS2812B, LED_Pin>(leds, LEDnum);
  FastLED.setBrightness(100); //to start will probably get set to 225 eventualy
  Serial.begin(115200);
}

void loop() {
  fill_rainbow(leds, LEDnum, 0, 225/LEDnum);
  for(int i = 0; i < LEDnum; i++) {
    CHSV ledshsv = rgb2hsv_approximate(leds[i]);
    Serial.print("led #"); Serial.print(i);
    Serial.print(" h="); Serial.print(ledshsv.h);
    Serial.print(" s="); Serial.print(ledshsv.s);
    Serial.print(" v="); Serial.println(ledshsv.v);
 }
}

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