Skip to content

Add Jack-o'-Lantern code #342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Jack-o-Lantern sketch for Adafruit Circuit Playground (Classic or Express)

#include "Adafruit_CircuitPlayground.h"

void setup() {
CircuitPlayground.begin();
CircuitPlayground.setBrightness(255); // LEDs full blast!
}

uint8_t prev = 128; // Start brightness in middle

void loop() {
uint8_t lvl = random(64, 192); // End brightness at 128±64
split(prev, lvl, 32); // Start subdividing, ±32 at midpoint
prev = lvl; // Assign end brightness to next start
}

void split(uint8_t y1, uint8_t y2, uint8_t offset) {
if(offset) { // Split further into sub-segments w/midpoint at ±offset
uint8_t mid = (y1 + y2 + 1) / 2 + random(-offset, offset);
split(y1 , mid, offset / 2); // First segment (offset is halved)
split(mid, y2 , offset / 2); // Second segment (ditto)
} else { // No further subdivision - y1 determines LED brightness
uint32_t c = (((int)(pow((float)y1 / 255.0, 2.7) * 255.0 + 0.5) // Gamma
* 0x1004004) >> 8) & 0xFF3F03; // Expand to 32-bit RGB color
for(uint8_t i=0; i<10; i++) CircuitPlayground.strip.setPixelColor(i, c);
CircuitPlayground.strip.show();
delay(4);
}
}
33 changes: 33 additions & 0 deletions Circuit_Playground_Jack_o_Lantern/JackOLantern_HalloWing.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Jack-o-Lantern sketch for Adafruit Hallowing

#include "Adafruit_NeoPixel.h"

#define NUM_PIXELS 30

Adafruit_NeoPixel strip(NUM_PIXELS, 4, NEO_GRB + NEO_KHZ800);

void setup() {
strip.begin();
}

uint8_t prev = 128; // Start brightness in middle

void loop() {
uint8_t lvl = random(64, 192); // End brightness at 128±64
split(prev, lvl, 32); // Start subdividing, ±32 at midpoint
prev = lvl; // Assign end brightness to next start
}

void split(uint8_t y1, uint8_t y2, uint8_t offset) {
if(offset) { // Split further into sub-segments w/midpoint at ±offset
uint8_t mid = (y1 + y2 + 1) / 2 + random(-offset, offset);
split(y1 , mid, offset / 2); // First segment (offset is halved)
split(mid, y2 , offset / 2); // Second segment (ditto)
} else { // No further subdivision - y1 determines LED brightness
uint32_t c = (((int)(pow((float)y1 / 255.0, 2.7) * 255.0 + 0.5) // Gamma
* 0x1004004) >> 8) & 0xFF3F03; // Expand to 32-bit RGB color
for(uint8_t i=0; i<NUM_PIXELS; i++) strip.setPixelColor(i, c);
strip.show();
delay(4);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Jack-o'-Lantern flame example Adafruit Circuit Playground Express"""

import math
import board
import neopixel
try:
import urandom as random # for v1.0 API support
except ImportError:
import random

NUMPIX = 10 # Number of NeoPixels
PIXPIN = board.D8 # Pin where NeoPixels are connected
STRIP = neopixel.NeoPixel(PIXPIN, NUMPIX, brightness=1.0)
PREV = 128

def split(first, second, offset):
"""
Subdivide a brightness range, introducing a random offset in middle,
then call recursively with smaller offsets along the way.
@param1 first: Initial brightness value.
@param1 second: Ending brightness value.
@param1 offset: Midpoint offset range is +/- this amount max.
"""
if offset != 0:
mid = ((first + second + 1) / 2 + random.randint(-offset, offset))
offset = int(offset / 2)
split(first, mid, offset)
split(mid, second, offset)
else:
level = math.pow(first / 255.0, 2.7) * 255.0 + 0.5
STRIP.fill((int(level), int(level / 8), int(level / 48)))
STRIP.write()

while True: # Loop forever...
LVL = random.randint(64, 191)
split(PREV, LVL, 32)
PREV = LVL
37 changes: 37 additions & 0 deletions Circuit_Playground_Jack_o_Lantern/jack_o_lantern_hallowing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Jack-o'-Lantern flame example Adafruit Hallowing"""

import math
import board
import neopixel
try:
import urandom as random # for v1.0 API support
except ImportError:
import random

NUMPIX = 30 # Number of NeoPixels
PIXPIN = board.EXTERNAL_NEOPIXEL # Pin where NeoPixels are connected
STRIP = neopixel.NeoPixel(PIXPIN, NUMPIX, brightness=1.0)
PREV = 128

def split(first, second, offset):
"""
Subdivide a brightness range, introducing a random offset in middle,
then call recursively with smaller offsets along the way.
@param1 first: Initial brightness value.
@param1 second: Ending brightness value.
@param1 offset: Midpoint offset range is +/- this amount max.
"""
if offset != 0:
mid = ((first + second + 1) / 2 + random.randint(-offset, offset))
offset = int(offset / 2)
split(first, mid, offset)
split(mid, second, offset)
else:
level = math.pow(first / 255.0, 2.7) * 255.0 + 0.5
STRIP.fill((int(level), int(level / 8), int(level / 48)))
STRIP.write()

while True: # Loop forever...
LVL = random.randint(64, 191)
split(PREV, LVL, 32)
PREV = LVL