/********************************************************************************************************
neopixel_basic:
Written by Scott Kildall
This will light just a NeoPixel strip of NeoPixels
---------------------------------------------------------------------------------------------------------
NeoPixel Information for initializing the strip, below
60ma/pixel for current load
Parameter 1 = number of pixels in strip
Parameter 2 = pin number (most are valid)
Parameter 3 = pixel type flags, add together as needed:
NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
**********************************************************************************************************/
#include <Adafruit_NeoPixel.h>
// the data pin for the NeoPixels
int neoPixelPin = 6;
// How many NeoPixels we will be using, charge accordingly
int numPixels = 25;
// Instatiate the NeoPixel from the ibrary
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numPixels, neoPixelPin, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.show();
strip.clear();
Serial.begin(115200);
}
void loop() {
// set the colors for the strip
/*
for( int i = 0; i < numPixels; i++ ) {
strip.setPixelColor(i, 255, 0, 0); strip.show();
delay(100);
strip.setPixelColor(i, 0, 0, 0);
}
*/
int i = 0;
while(1) {
setPixelColorHsv(strip, i, 255, 255, 255);
Serial.println(i);
delay(1000);
strip.show();
i++;
}
// wait for a short amount of time -- sometimes a short delay of 5ms will help
// technically we only need to execute this one time, since we aren't changing the colors but we will build on this structure
delay(1000);
}
void setPixelColorHsv(Adafruit_NeoPixel ls, uint16_t pixel, uint8_t h, uint8_t s, uint8_t v) {
int lr, lg, lb;
hsvToRgb(h, s, v, lr, lg, lb);
Serial.println(lr);
Serial.println(lg);
Serial.println(lb);
Serial.println("\n");
ls.setPixelColor(pixel, lr, lg, lb);
}
void hsvToRgb(int h, int s, int v, int &r, int &g, int &b) {
float fh = h / 255.0;
float fs = s / 255.0f;
float fv = v / 255.0f;
int i = static_cast<int>(fh * 6);
float f = fh * 6 - i;
float p = fv * (1 - fs);
float q = fv * (1 - f * fs);
float t = fv * (1 - (1 - f) * fs);
switch (i % 6) {
case 0:
r = static_cast<int>(fv * 255);
g = static_cast<int>(t * 255);
b = static_cast<int>(p * 255);
break;
case 1:
r = static_cast<int>(q * 255);
g = static_cast<int>(fv * 255);
b = static_cast<int>(p * 255);
break;
case 2:
r = static_cast<int>(p * 255);
g = static_cast<int>(fv * 255);
b = static_cast<int>(t * 255);
break;
case 3:
r = static_cast<int>(p * 255);
g = static_cast<int>(q * 255);
b = static_cast<int>(fv * 255);
break;
case 4:
r = static_cast<int>(t * 255);
g = static_cast<int>(p * 255);
b = static_cast<int>(fv * 255);
break;
case 5:
r = static_cast<int>(fv * 255);
g = static_cast<int>(p * 255);
b = static_cast<int>(q * 255);
break;
}
}