Skip to content

Instantly share code, notes, and snippets.

@ashton
Created January 24, 2011 01:30
Show Gist options
  • Select an option

  • Save ashton/792662 to your computer and use it in GitHub Desktop.

Select an option

Save ashton/792662 to your computer and use it in GitHub Desktop.
Variação de Matiz com LED RGB e Arduino
#define bPin 9
#define gPin 10
#define rPin 11
int r, g, b;
int hue;
void setup() {
pinMode(rPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(bPin, OUTPUT);
}
void loop() {
for(int hue = 0; hue < 255; hue += 4) {
HSBToRGB(hue, 255, 255, &r, &g, &b);
analogWrite(rPin, r);
analogWrite(gPin, g);
analogWrite(bPin, b);
delay(10);
}
}
void HSBToRGB( int inHue, int inSaturation, int inBrightness, int *oR, int *oG, int *oB ) {
if( inSaturation == 0) {
//achromatic (grey)
*oR = *oG = *oB = inBrightness;
} else {
unsigned int scaledHue = (inHue * 6);
unsigned int sector = scaledHue >> 8; // sector 0 to 5 around the color wheel
unsigned int offsetInSector = scaledHue - (sector << 8); // position within the sector
unsigned int p = (inBrightness * ( 255 - inSaturation )) >> 8;
unsigned int q = (inBrightness * ( 255 - ((inSaturation * offsetInSector) >> 8) )) >> 8;
unsigned int t = (inBrightness * ( 255 - ((inSaturation * ( 255 - offsetInSector )) >> 8) )) >> 8;
switch( sector ) {
case 0:
*oR = inBrightness;
*oG = t;
*oB = p;
break;
case 1:
*oR = q;
*oG = inBrightness;
*oB = p;
break;
case 2:
*oR = p;
*oG = inBrightness;
*oB = t;
break;
case 3:
*oR = p;
*oG = q;
*oB = inBrightness;
break;
case 4:
*oR = t;
*oG = p;
*oB = inBrightness;
break;
default: // case 5:
*oR = inBrightness;
*oG = p;
*oB = q;
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment