Created
July 8, 2017 02:12
-
-
Save botamochi6277/4372ac666df49b6e900b1edf60fab387 to your computer and use it in GitHub Desktop.
light RGB LED based on HSV model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* light RGB LED based on HSV model | |
* This code is derived from "Adafruit Arduino - Lesson 3. RGB LED" | |
* https://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds/overview | |
*/ | |
// #define DEBUG | |
int redPin = 11; | |
int greenPin = 10; | |
int bluePin = 9; | |
//uncomment this line if using a Common Anode LED | |
//#define COMMON_ANODE | |
void setColorHSV(float h, float s, float v); | |
void setup() | |
{ | |
#ifdef DEBUG | |
Serial.begin(9600); | |
#endif | |
pinMode(redPin, OUTPUT); | |
pinMode(greenPin, OUTPUT); | |
pinMode(bluePin, OUTPUT); | |
} | |
void loop() | |
{ | |
for(int hue=0;hue<360;++hue){ | |
setColorHSV(hue,1.0,0.2); | |
delay(100); | |
} | |
} | |
void setColor(int red, int green, int blue) | |
{ | |
#ifdef COMMON_ANODE | |
red = 255 - red; | |
green = 255 - green; | |
blue = 255 - blue; | |
#endif | |
analogWrite(redPin, red); | |
analogWrite(greenPin, green); | |
analogWrite(bluePin, blue); | |
} | |
/** | |
* @brief HSV色空間に基づいて,RGBLEDを光らせる | |
* @param H 色相(Hue), 0.0--360.0 | |
* @param S 彩度(Saturation), 0.0--1.0 | |
* @param V 明度(Value), 0.0--1.0 | |
*/ | |
void setColorHSV(float h, float s, float v) | |
{ | |
int r,g,b; | |
int hi; | |
float f, p, q, t; | |
hi = ((int)(h / 60)) % 6; | |
f = h / 60 - hi; | |
p = v * (1 - s); | |
q = v * (1 - f * s); | |
t = v * (1 - (1 - f) * s); | |
// 256階調に戻す | |
v *= 255; | |
p *= 255; | |
q *= 255; | |
t *= 255; | |
switch (hi) { | |
case 0: r = v; g = t; b = p; break; | |
case 1: r = q; g = v; b = p; break; | |
case 2: r = p; g = v; b = t; break; | |
case 3: r = p; g = q; b = v; break; | |
case 4: r = t; g = p; b = v; break; | |
case 5: r = v; g = p; b = q; break; | |
} | |
#ifdef DEBUG | |
Serial.print(r); | |
Serial.print(","); | |
Serial.print(g); | |
Serial.print(","); | |
Serial.println(b); | |
#endif | |
setColor(r,g,b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment