Skip to content

Commit 77aa82f

Browse files
authored
Add files via upload
1 parent 5d38d7e commit 77aa82f

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed
57.5 KB
Binary file not shown.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// SPDX-FileCopyrightText: 2017 Phillip Burgess for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Adafruit_NeoPixel.h>
6+
7+
#define NUM_LEDS 24 // 24 LED NeoPixel ring
8+
#define NEOPIXEL_PIN 0 // Pin D0 on Gemma
9+
#define VIBRATION_PIN 1 // Pin D1 on Gemma
10+
#define ANALOG_RANDOMNESS_PIN A1 // Not connected to anything
11+
12+
#define DEFAULT_FRAME_LEN 60
13+
#define MAX_FRAME_LEN 255
14+
#define MIN_FRAME_LEN 5
15+
#define COOLDOWN_AT 2000
16+
#define DIM_AT 2500
17+
#define BRIGHTNESS_HIGH 128
18+
#define BRIGHTNESS_LOW 32
19+
20+
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS, NEOPIXEL_PIN);
21+
22+
uint32_t color = pixels.Color(0, 120, 30);
23+
uint8_t offset = 0;
24+
uint8_t frame_len = DEFAULT_FRAME_LEN;
25+
uint32_t last_vibration = 0;
26+
uint32_t last_frame = 0;
27+
28+
void setup() {
29+
// Random number generator is seeded from an unused 'floating'
30+
// analog input - this helps ensure the random color choices
31+
// aren't always the same order.
32+
randomSeed(analogRead(ANALOG_RANDOMNESS_PIN));
33+
// Enable pullup on vibration switch pin. When the switch
34+
// is activated, it's pulled to ground (LOW).
35+
pinMode(VIBRATION_PIN, INPUT_PULLUP);
36+
pixels.begin();
37+
}
38+
39+
void loop() {
40+
uint32_t t;
41+
42+
// Compare millis() against lastFrame time to keep frame-to-frame
43+
// animation timing consistent. Use this idle time to check the
44+
// vibration switch for activity.
45+
while(((t = millis()) - last_frame) <= frame_len) {
46+
if(!digitalRead(VIBRATION_PIN)) { // Vibration sensor activated?
47+
color = pixels.Color( // Pick a random RGB color
48+
random(256), // red
49+
random(256), // green
50+
random(256) // blue
51+
);
52+
frame_len = DEFAULT_FRAME_LEN; // Reset frame timing to default
53+
last_vibration = t; // Save last vibration time
54+
}
55+
}
56+
57+
// Stretch out frames if nothing has happened in a couple of seconds:
58+
if((t - last_vibration) > COOLDOWN_AT) {
59+
if(++frame_len > MAX_FRAME_LEN) frame_len = MIN_FRAME_LEN;
60+
}
61+
62+
// If we haven't registered a vibration in DIM_AT ms, go dim:
63+
if((t - last_vibration) > DIM_AT) {
64+
pixels.setBrightness(BRIGHTNESS_LOW);
65+
} else {
66+
pixels.setBrightness(BRIGHTNESS_HIGH);
67+
}
68+
69+
// Erase previous pixels and light new ones:
70+
pixels.clear();
71+
for(int i=0; i<NUM_LEDS; i += 6) {
72+
pixels.setPixelColor((offset + i) % NUM_LEDS, color);
73+
}
74+
75+
pixels.show();
76+
77+
// Increase pixel offset until it hits 6, then roll back to 0:
78+
if(++offset == 6) offset = 0;
79+
80+
last_frame = t;
81+
}
58.3 KB
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# NeoPixel_Blinkendisc
2+
3+
Code to accompany this tutorial:
4+
https://learn.adafruit.com/a-neopixel-blinkendisc

Gemma/NeoPixel_Blinkendisc/code.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# SPDX-FileCopyrightText: 2017 Phillip Burgess for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
7+
import analogio
8+
import board
9+
import digitalio
10+
import neopixel
11+
12+
try:
13+
import urandom as random # for v1.0 API support
14+
except ImportError:
15+
import random
16+
17+
num_leds = 24 # 24 LED NeoPixel ring
18+
neopixel_pin = board.D0 # Pin where NeoPixels are connected
19+
vibration_pin = board.D1 # Pin where vibration switch is connected
20+
analog_pin = board.A0 # Not connected to anything
21+
strip = neopixel.NeoPixel(neopixel_pin, num_leds)
22+
23+
default_frame_len = 0.06 # Time (in seconds) of typical animation frame
24+
max_frame_len = 0.25 # Gradually slows toward this
25+
min_frame_len = 0.005 # But sometimes as little as this
26+
cooldown_at = 2.0 # After this many seconds, start slowing down
27+
dim_at = 2.5 # After this many seconds, dim LEDs
28+
brightness_high = 0.5 # Active brightness
29+
brightness_low = 0.125 # Idle brightness
30+
31+
color = [0, 120, 30] # Initial LED color
32+
offset = 0 # Animation position
33+
frame_len = default_frame_len # Frame-to-frame time, seconds
34+
last_vibration = 0.0 # Time of last vibration
35+
last_frame = 0.0 # Time of last animation frame
36+
37+
# Random number generator is seeded from an unused 'floating'
38+
# analog input - this helps ensure the random color choices
39+
# aren't always the same order.
40+
pin = analogio.AnalogIn(analog_pin)
41+
random.seed(pin.value)
42+
pin.deinit()
43+
44+
# Set up digital pin for reading vibration switch
45+
pin = digitalio.DigitalInOut(vibration_pin)
46+
pin.direction = digitalio.Direction.INPUT
47+
pin.pull = digitalio.Pull.UP
48+
49+
while True: # Loop forever...
50+
51+
while True:
52+
# Compare time.monotonic() against last_frame to keep
53+
# frame-to-frame animation timing consistent. Use this
54+
# idle time to check the vibration switch for activity.
55+
t = time.monotonic()
56+
if t - last_frame >= frame_len:
57+
break
58+
if not pin.value: # Vibration switch activated?
59+
color = [ # Pick a random RGB color...
60+
random.randint(32, 255),
61+
random.randint(32, 255),
62+
random.randint(32, 255)]
63+
frame_len = default_frame_len # Reset frame timing
64+
last_vibration = t # Save last trigger time
65+
66+
# Stretch out frames if nothing has happened in a couple of seconds:
67+
if (t - last_vibration) > cooldown_at:
68+
frame_len += 0.001 # Add 1 ms
69+
if frame_len > max_frame_len:
70+
frame_len = min_frame_len
71+
72+
# If we haven't registered a vibration in dim_at ms, go dim:
73+
if (t - last_vibration) > dim_at:
74+
strip.brightness = brightness_low
75+
else:
76+
strip.brightness = brightness_high
77+
78+
# Erase previous pixels and light new ones:
79+
strip.fill([0, 0, 0])
80+
for i in range(0, num_leds, 6):
81+
strip[(offset + i) % num_leds] = color
82+
83+
strip.write() # and issue data to LED strip
84+
85+
# Increase pixel offset until it hits 6, then roll back to 0:
86+
offset = (offset + 1) % 6
87+
88+
last_frame = t

0 commit comments

Comments
 (0)