Skip to content

Instantly share code, notes, and snippets.

@serverwentdown
Created May 25, 2015 03:01
Show Gist options
  • Save serverwentdown/0e8bfa1cbce0916b38bd to your computer and use it in GitHub Desktop.
Save serverwentdown/0e8bfa1cbce0916b38bd to your computer and use it in GitHub Desktop.
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library
#include <Adafruit_NeoPixel.h>
// Which pin on the Arduino is connected to the NeoPixels?
#define PIN 3
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 1
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_RGB + NEO_KHZ800);
unsigned int r = 0, g = 0, b = 0;
unsigned int hue = 1;
void setup() {
pixels.begin(); // This initializes the NeoPixel library.
}
void loop() {
//The Hue value will vary from 0 to 360, which represents degrees in the color wheel
for(int hue=0;hue<360;hue++)
{
setLedColorHSV(hue, 1, 0.25); //We are using Saturation and Value constant at 1
delay(48); //each color will be shown for 10 milliseconds
}
}
//Convert a given HSV (Hue Saturation Value) to RGB(Red Green Blue) and set the led to the color
// h is hue value, integer between 0 and 360
// s is saturation value, double between 0 and 1
// v is value, double between 0 and 1
//http://splinter.com.au/blog/?p=29
void setLedColorHSV(int h, double s, double v) {
//this is the algorithm to convert from RGB to HSV
double r=0;
double g=0;
double b=0;
double hf=h/60.0;
int i=(int)floor(h/60.0);
double f = h/60.0 - i;
double pv = v * (1 - s);
double qv = v * (1 - s*f);
double tv = v * (1 - s * (1 - f));
switch (i)
{
case 0: //rojo dominante
r = v;
g = tv;
b = pv;
break;
case 1: //verde
r = qv;
g = v;
b = pv;
break;
case 2:
r = pv;
g = v;
b = tv;
break;
case 3: //azul
r = pv;
g = qv;
b = v;
break;
case 4:
r = tv;
g = pv;
b = v;
break;
case 5: //rojo
r = v;
g = pv;
b = qv;
break;
}
//set each component to a integer value between 0 and 255
int red=constrain((int)255*r,0,255);
int green=constrain((int)255*g,0,255);
int blue=constrain((int)255*b,0,255);
pixels.setPixelColor(0, pixels.Color(red, green, blue));
pixels.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment