Code: Select all
uint16_t k, j = 0;
boolean iterated = false;
byte colwipe = 1;
uint32_t col;
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint16_t wait) {
if(millis() - previousMillis > 40) {
previousMillis = millis();
if (k < strip.numPixels()){
strip.setPixelColor(k, c);
strip.show();
//delay(wait);
k++;
} else {
k = 0;
colwipe++;
if (colwipe > 3){
colwipe = 1;
}
}
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
if (j < 256) {
if (iterated) {
j += 4;
iterated = false;
}
if (k < strip.numPixels()) {
strip.setPixelColor(k, hsv2rgb(((k * 256 / strip.numPixels()) + j), 255, 255));
k++;
} else {
k = 0;
iterated = true;
}
strip.show();
if (iterated == true){
delay(wait);
//iterated = false;
}
} else {
j = 0;
}
}
void fade(uint8_t wait) { // Whole-strip fade
if (j < 255) {
if (k < strip.numPixels()){
strip.setPixelColor(k, col);
k++;
} else {
k = 0;
col = hsv2rgb(j, 255, 255);
j += 4;
}
strip.show();
delay(wait);
} else {
j = 0;
}
}
uint32_t hsv2rgb(uint8_t hi, uint8_t s, uint8_t v) {
uint8_t n, r, g, b;
int16_t h;
h = hi * 6;
// Hue circle = 0 to 1530 (NOT 1536!)
h %= 1530; // -1529 to +1529
if(h < 0) h += 1530; // 0 to +1529
n = h % 255; // Angle within sextant; 0 to 254 (NOT 255!)
switch(h / 255) { // Sextant number; 0 to 5
case 0 : r = 255 ; g = n ; b = 0 ; break; // R to Y
case 1 : r = 254 - n; g = 255 ; b = 0 ; break; // Y to G
case 2 : r = 0 ; g = 255 ; b = n ; break; // G to C
case 3 : r = 0 ; g = 254 - n; b = 255 ; break; // C to B
case 4 : r = n ; g = 0 ; b = 255 ; break; // B to M
default: r = 255 ; g = 0 ; b = 254 - n; break; // M to R
}
uint32_t v1 = 1 + v; // 1 to 256; allows >>8 instead of /255
uint16_t s1 = 1 + s; // 1 to 256; same reason
uint8_t s2 = 255 - s; // 255 to 0
return ((((((r * s1) >> 8) + s2) * v1) & 0xff00) << 8) | // MAF!
(((((g * s1) >> 8) + s2) * v1) & 0xff00) |
( ((((b * s1) >> 8) + s2) * v1) >> 8);
}