-
Notifications
You must be signed in to change notification settings - Fork 46
/
RGB_2_HSV_2_RGB_convert.ino
84 lines (65 loc) · 2.61 KB
/
RGB_2_HSV_2_RGB_convert.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//***************************************************************
// Demo of converting HSV to RGB_rainbow values.
// And then converting RGB back to HSV to show how close it is.
//
// Values are printed to serial monitor.
//
// The first two pixels will show that the HSV to RGB converted
// colors always match. Observing the third pixel will show
// how the conversion from RGB back to HSV is often pretty
// close, but can be a bit off sometimes. Converting RGB to HSV
// works best with fully saturated colors.
//
// Marc Miller, Nov 2017
//***************************************************************
#include "FastLED.h"
#define DATA_PIN 11
#define CLOCK_PIN 13
#define LED_TYPE LPD8806
#define COLOR_ORDER GBR
#define NUM_LEDS 32
#define BRIGHTNESS 255
CRGB leds[NUM_LEDS];
//---------------------------------------------------------------
void setup() {
Serial.begin(115200); // Allows serial monitor output (check baud rate)
delay(1500); // startup delay
//FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.addLeds<LED_TYPE,DATA_PIN,CLOCK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear();
}
//---------------------------------------------------------------
void loop() {
EVERY_N_SECONDS( 3 ) {
//CHSV hsv( random8(), 255, 255 ); //pick random Hue
CHSV hsv( random8(), random8(), random8() ); //pick random HSV
CRGB rgb;
hsv2rgb_rainbow( hsv, rgb); //convert HSV to RGB
//set first two pixels with HSV and RGB values to confirm they are the same.
leds[0] = hsv;
leds[1] = rgb;
//print out HSV and RGB values
Serial.print(" HSV: ");
Serial.print(hsv.h);
Serial.print(", "); Serial.print(hsv.s);
Serial.print(", "); Serial.println(hsv.v);
Serial.print(" RGB: ");
Serial.print(rgb.r);
Serial.print(", "); Serial.print(rgb.g);
Serial.print(", "); Serial.println(rgb.b);
//Now try converting RGB back to HSV using rgb2hsv_approximate.
//rgb2hsv_approximate works best with fully saturated or nearly
//saturated colors, otherwise it's not always super accurate.
hsv = rgb2hsv_approximate( rgb );
//set third pixels with HSV approximate values to show how close it is.
leds[2] = hsv;
//print out approximated HSV values
Serial.print("HSV(approx.): ");
Serial.print(hsv.h);
Serial.print(", "); Serial.print(hsv.s);
Serial.print(", "); Serial.println(hsv.v);
Serial.println(" ");
FastLED.show(); //display the pixels
}//end_EVERY_N
}//end_main_loop