Merge pull request #22 from adafruit/NeoPixel_Jewel_10_Minute_Necklace
ported to CircuitPython
This commit is contained in:
commit
f02377b9cb
3 changed files with 214 additions and 0 deletions
|
|
@ -0,0 +1,114 @@
|
|||
#include <Adafruit_NeoPixel.h>
|
||||
|
||||
#define PIN 1
|
||||
|
||||
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(40, PIN);
|
||||
|
||||
uint8_t mode = 0, // Current animation effect
|
||||
offset = 0;
|
||||
uint32_t color = 0X00A4B3; // Starting color
|
||||
uint32_t prevTime;
|
||||
|
||||
void setup() {
|
||||
pixels.begin();
|
||||
pixels.setBrightness(40); // 1/3 brightness
|
||||
prevTime = millis();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
uint8_t i;
|
||||
uint32_t t;
|
||||
|
||||
switch(mode) {
|
||||
|
||||
case 0: //rainbow hold
|
||||
rainbowHold(20);
|
||||
delay(500);
|
||||
break;
|
||||
|
||||
case 1: //rainbow cycle slow
|
||||
rainbowCycleslow(20);
|
||||
delay(50);
|
||||
break;
|
||||
|
||||
case 2: //rainbow cycle fast
|
||||
rainbowCycle(5);
|
||||
delay(50);
|
||||
break;
|
||||
}
|
||||
|
||||
t = millis();
|
||||
if((t - prevTime) > 8000) { // Every 8 seconds...
|
||||
mode++; // Next mode
|
||||
if(mode > 3) { // End of modes?
|
||||
mode = 0; // Start modes over
|
||||
color >>= 8; // Next color R->G->B
|
||||
if(!color) color = 0xB300A4; // Reset color
|
||||
}
|
||||
for(i=0; i<32; i++) pixels.setPixelColor(i, 0);
|
||||
prevTime = t;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
void rainbow(uint8_t wait) {
|
||||
uint16_t i, j;
|
||||
|
||||
for(j=0; j<256; j++) {
|
||||
for(i=0; i<pixels.numPixels(); i++) {
|
||||
pixels.setPixelColor(i, Wheel((i+j) & 255));
|
||||
}
|
||||
pixels.show();
|
||||
delay(wait);
|
||||
}
|
||||
}
|
||||
|
||||
void rainbowCycle(uint8_t wait) {
|
||||
uint16_t r, j;
|
||||
|
||||
for(j=0; j<256*6; j++) { // 6 cycles of all colors on wheel
|
||||
for(r=0; r< pixels.numPixels(); r++) {
|
||||
pixels.setPixelColor(r, Wheel(((r * 256 / pixels.numPixels()) + j) & 255));
|
||||
}
|
||||
pixels.show();
|
||||
delay(wait);
|
||||
}
|
||||
}
|
||||
void rainbowCycleslow(uint8_t wait) {
|
||||
uint16_t r, j;
|
||||
|
||||
for(j=0; j<256*3; j++) { // 3 cycles of all colors on wheel
|
||||
for(r=0; r< pixels.numPixels(); r++) {
|
||||
pixels.setPixelColor(r, Wheel(((r * 256 / pixels.numPixels()) + j) & 255));
|
||||
}
|
||||
pixels.show();
|
||||
delay(wait);
|
||||
}
|
||||
}
|
||||
void rainbowHold(uint8_t wait) {
|
||||
uint16_t r, j;
|
||||
|
||||
for(j=0; j<256*1; j++) { // 3 cycles of all colors on wheel
|
||||
for(r=0; r< pixels.numPixels(); r++) {
|
||||
pixels.setPixelColor(r, Wheel(((r * 256 / pixels.numPixels()) + j) & 255));
|
||||
}
|
||||
pixels.show();
|
||||
delay(wait);
|
||||
}
|
||||
}
|
||||
|
||||
// Input a value 0 to 255 to get a color value.
|
||||
// The colours are a transition r - g - b - back to r.
|
||||
uint32_t Wheel(byte WheelPos) {
|
||||
if(WheelPos < 85) {
|
||||
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
|
||||
} else if(WheelPos < 170) {
|
||||
WheelPos -= 85;
|
||||
return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
|
||||
} else {
|
||||
WheelPos -= 170;
|
||||
return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,96 @@
|
|||
import board
|
||||
import neopixel
|
||||
import time
|
||||
|
||||
pixpin = board.D1
|
||||
numpix = 40
|
||||
|
||||
pixels = neopixel.NeoPixel(pixpin, numpix, brightness=.3, auto_write=False)
|
||||
|
||||
rgb_colors = ( [179,0,0],
|
||||
[0,179,0],
|
||||
[0,0,0] )
|
||||
|
||||
rgb_idx = 0 # index counter - primary color we are on
|
||||
color = (0, 164, 179) # Starting color
|
||||
mode = 0 # Current animation effect
|
||||
offset = 0;
|
||||
prevtime = 0
|
||||
|
||||
def wheel(pos):
|
||||
# Input a value 0 to 255 to get a color value.
|
||||
# The colours are a transition r - g - b - back to r.
|
||||
if (pos < 0) or (pos > 255):
|
||||
return (0, 0, 0)
|
||||
if (pos < 85):
|
||||
return (int(pos * 3), int(255 - (pos*3)), 0)
|
||||
elif (pos < 170):
|
||||
pos -= 85
|
||||
return (int(255 - pos*3), 0, int(pos*3))
|
||||
else:
|
||||
pos -= 170
|
||||
return (0, int(pos*3), int(255 - pos*3))
|
||||
|
||||
def rainbow_cycle(wait):
|
||||
for j in range(255*6): # 6 cycles of all colors on wheel
|
||||
for r in range(len(pixels)):
|
||||
idx = int ((r * 255 / len(pixels)) + j)
|
||||
pixels[r] = wheel(idx & 255)
|
||||
pixels.write()
|
||||
time.sleep(wait)
|
||||
|
||||
def rainbow(wait):
|
||||
for j in range(255):
|
||||
for i in range(len(pixels)):
|
||||
idx = int (i+j)
|
||||
pixels[i] = wheel(idx & 255)
|
||||
pixels.write()
|
||||
time.sleep(wait)
|
||||
|
||||
def rainbow_cycle_slow(wait):
|
||||
for j in range(255*3): # 3 cycles of all colors on wheel
|
||||
for r in range(len(pixels)):
|
||||
idx = int ((r * 255 / len(pixels)) + j)
|
||||
pixels[r] = wheel(idx & 255)
|
||||
pixels.write()
|
||||
time.sleep(wait)
|
||||
|
||||
def rainbow_hold(wait):
|
||||
for j in range(255*1): # 3 cycles of all colors on wheel
|
||||
for r in range(len(pixels)):
|
||||
idx = int ((r * 255 / len(pixels)) + j)
|
||||
pixels[r] = wheel(idx & 255)
|
||||
pixels.write()
|
||||
time.sleep(wait)
|
||||
|
||||
while True:
|
||||
|
||||
if ( mode == 0 ): # rainbow hold
|
||||
rainbow_hold(0.02)
|
||||
time.sleep(.5)
|
||||
|
||||
elif ( mode == 1 ): # rainbow cycle slow
|
||||
rainbow_cycle_slow(0.02)
|
||||
time.sleep(0.05)
|
||||
|
||||
elif ( mode == 2): # rainbow cycle fast
|
||||
rainbow_cycle(0.005)
|
||||
time.sleep(0.050)
|
||||
|
||||
t = time.monotonic()
|
||||
|
||||
if ((t - prevtime) > 8): # Every 8 seconds...
|
||||
mode += 1 # Next mode
|
||||
if( mode > 2 ): # End of modes?
|
||||
mode = 0 # Start modes over
|
||||
|
||||
if ( rgb_idx > 2 ): # reset R-->G-->B rotation
|
||||
rgb_idx = 0
|
||||
|
||||
color = rgb_colors[rgb_idx] # next color assignment
|
||||
rgb_idx += 1
|
||||
|
||||
for i in range(numpix):
|
||||
pixels[i] = (0,0,0)
|
||||
|
||||
prevtime = t
|
||||
4
NeoPixel_Jewel_10_Minute_Necklace/README.md
Normal file
4
NeoPixel_Jewel_10_Minute_Necklace/README.md
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# NeoPixel_Jewel_10_Minute_Necklace.ino
|
||||
|
||||
Code to accompany this tutorial:
|
||||
https://learn.adafruit.com/10-minute-neopixel-necklace
|
||||
Loading…
Reference in a new issue