Skip to content

Commit 17004db

Browse files
Merge pull request #342 from PaintYourDragon/master
Add Jack-o'-Lantern code
2 parents facd0d1 + 93269f5 commit 17004db

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Jack-o-Lantern sketch for Adafruit Circuit Playground (Classic or Express)
2+
3+
#include "Adafruit_CircuitPlayground.h"
4+
5+
void setup() {
6+
CircuitPlayground.begin();
7+
CircuitPlayground.setBrightness(255); // LEDs full blast!
8+
}
9+
10+
uint8_t prev = 128; // Start brightness in middle
11+
12+
void loop() {
13+
uint8_t lvl = random(64, 192); // End brightness at 128±64
14+
split(prev, lvl, 32); // Start subdividing, ±32 at midpoint
15+
prev = lvl; // Assign end brightness to next start
16+
}
17+
18+
void split(uint8_t y1, uint8_t y2, uint8_t offset) {
19+
if(offset) { // Split further into sub-segments w/midpoint at ±offset
20+
uint8_t mid = (y1 + y2 + 1) / 2 + random(-offset, offset);
21+
split(y1 , mid, offset / 2); // First segment (offset is halved)
22+
split(mid, y2 , offset / 2); // Second segment (ditto)
23+
} else { // No further subdivision - y1 determines LED brightness
24+
uint32_t c = (((int)(pow((float)y1 / 255.0, 2.7) * 255.0 + 0.5) // Gamma
25+
* 0x1004004) >> 8) & 0xFF3F03; // Expand to 32-bit RGB color
26+
for(uint8_t i=0; i<10; i++) CircuitPlayground.strip.setPixelColor(i, c);
27+
CircuitPlayground.strip.show();
28+
delay(4);
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Jack-o-Lantern sketch for Adafruit Hallowing
2+
3+
#include "Adafruit_NeoPixel.h"
4+
5+
#define NUM_PIXELS 30
6+
7+
Adafruit_NeoPixel strip(NUM_PIXELS, 4, NEO_GRB + NEO_KHZ800);
8+
9+
void setup() {
10+
strip.begin();
11+
}
12+
13+
uint8_t prev = 128; // Start brightness in middle
14+
15+
void loop() {
16+
uint8_t lvl = random(64, 192); // End brightness at 128±64
17+
split(prev, lvl, 32); // Start subdividing, ±32 at midpoint
18+
prev = lvl; // Assign end brightness to next start
19+
}
20+
21+
void split(uint8_t y1, uint8_t y2, uint8_t offset) {
22+
if(offset) { // Split further into sub-segments w/midpoint at ±offset
23+
uint8_t mid = (y1 + y2 + 1) / 2 + random(-offset, offset);
24+
split(y1 , mid, offset / 2); // First segment (offset is halved)
25+
split(mid, y2 , offset / 2); // Second segment (ditto)
26+
} else { // No further subdivision - y1 determines LED brightness
27+
uint32_t c = (((int)(pow((float)y1 / 255.0, 2.7) * 255.0 + 0.5) // Gamma
28+
* 0x1004004) >> 8) & 0xFF3F03; // Expand to 32-bit RGB color
29+
for(uint8_t i=0; i<NUM_PIXELS; i++) strip.setPixelColor(i, c);
30+
strip.show();
31+
delay(4);
32+
}
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Jack-o'-Lantern flame example Adafruit Circuit Playground Express"""
2+
3+
import math
4+
import board
5+
import neopixel
6+
try:
7+
import urandom as random # for v1.0 API support
8+
except ImportError:
9+
import random
10+
11+
NUMPIX = 10 # Number of NeoPixels
12+
PIXPIN = board.D8 # Pin where NeoPixels are connected
13+
STRIP = neopixel.NeoPixel(PIXPIN, NUMPIX, brightness=1.0)
14+
PREV = 128
15+
16+
def split(first, second, offset):
17+
"""
18+
Subdivide a brightness range, introducing a random offset in middle,
19+
then call recursively with smaller offsets along the way.
20+
@param1 first: Initial brightness value.
21+
@param1 second: Ending brightness value.
22+
@param1 offset: Midpoint offset range is +/- this amount max.
23+
"""
24+
if offset != 0:
25+
mid = ((first + second + 1) / 2 + random.randint(-offset, offset))
26+
offset = int(offset / 2)
27+
split(first, mid, offset)
28+
split(mid, second, offset)
29+
else:
30+
level = math.pow(first / 255.0, 2.7) * 255.0 + 0.5
31+
STRIP.fill((int(level), int(level / 8), int(level / 48)))
32+
STRIP.write()
33+
34+
while True: # Loop forever...
35+
LVL = random.randint(64, 191)
36+
split(PREV, LVL, 32)
37+
PREV = LVL
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Jack-o'-Lantern flame example Adafruit Hallowing"""
2+
3+
import math
4+
import board
5+
import neopixel
6+
try:
7+
import urandom as random # for v1.0 API support
8+
except ImportError:
9+
import random
10+
11+
NUMPIX = 30 # Number of NeoPixels
12+
PIXPIN = board.EXTERNAL_NEOPIXEL # Pin where NeoPixels are connected
13+
STRIP = neopixel.NeoPixel(PIXPIN, NUMPIX, brightness=1.0)
14+
PREV = 128
15+
16+
def split(first, second, offset):
17+
"""
18+
Subdivide a brightness range, introducing a random offset in middle,
19+
then call recursively with smaller offsets along the way.
20+
@param1 first: Initial brightness value.
21+
@param1 second: Ending brightness value.
22+
@param1 offset: Midpoint offset range is +/- this amount max.
23+
"""
24+
if offset != 0:
25+
mid = ((first + second + 1) / 2 + random.randint(-offset, offset))
26+
offset = int(offset / 2)
27+
split(first, mid, offset)
28+
split(mid, second, offset)
29+
else:
30+
level = math.pow(first / 255.0, 2.7) * 255.0 + 0.5
31+
STRIP.fill((int(level), int(level / 8), int(level / 48)))
32+
STRIP.write()
33+
34+
while True: # Loop forever...
35+
LVL = random.randint(64, 191)
36+
split(PREV, LVL, 32)
37+
PREV = LVL

0 commit comments

Comments
 (0)