Skip to content

Instantly share code, notes, and snippets.

@JeffersGlass
Created May 3, 2020 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffersGlass/514798642f0496ef7959c0e8575a7386 to your computer and use it in GitHub Desktop.
Save JeffersGlass/514798642f0496ef7959c0e8575a7386 to your computer and use it in GitHub Desktop.
int red = 255;
int green = 127;
int blue = 240;
int redPin = 3;
int greenPin = 5;
int bluePin = 6;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
for (int i = 0; i < 360; i++){
setColorByHSV(i, 100, 255);
delay(10);
}
}
void setColorByHSV(int hue, int sat, int val) {
/* convert hue, saturation and brightness ( HSB/HSV ) to RGB
The dim_curve is used only on brightness/value and on saturation (inverted).
This looks the most natural.
*/
int colors[3];
sat = 255-sat;
int r;
int g;
int b;
int base;
if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
setLedColor(val, val, val);
} else {
base = ((255 - sat) * val)>>8;
switch(hue/60) {
case 0:
r = val;
g = (((val-base)*hue)/60)+base;
b = base;
break;
case 1:
r = (((val-base)*(60-(hue%60)))/60)+base;
g = val;
b = base;
break;
case 2:
r = base;
g = val;
b = (((val-base)*(hue%60))/60)+base;
break;
case 3:
r = base;
g = (((val-base)*(60-(hue%60)))/60)+base;
b = val;
break;
case 4:
r = (((val-base)*(hue%60))/60)+base;
g = base;
b = val;
break;
case 5:
r = val;
g = base;
b = (((val-base)*(60-(hue%60)))/60)+base;
break;
}
setLedColor(r, g, b);
}
}
void setLedColor(int r, int g, int b){
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment