The Blinkendisc sketch as-written doesn't do too much, but it does offer a handful of configuration constants that you can tweak to change the behavior of the disc without rewriting the code.
First, let's look at the basic idea that drives the sketch. Here's a really simplified version of the loop()
function:
void loop() { uint32_t t; while(((t = millis()) - last_frame) <= frame_len) { if(!digitalRead(VIBRATION_PIN)) { // Vibration sensor activated? last_vibration = t; // Save last vibration time } } // Make any changes that need t, last_frame or last_vibration here. // Draw new frame of pixel animation here. pixels.show(); last_frame = t; }
This loop()
will get called over and over again for as long as the Gemma runs. Each time through, we:
- Set
t
to the current return value ofmillis()
, which should tell us the number of milliseconds since the sketch started. - Repeatedly compare this against the time the last animation frame was drawn (
last_frame
). While we're waiting for the correct time interval to have passed… - Use
if(!digitalRead(VIBRATION_PIN)) { ... }
to check for a vibration, and if we got one, setlast_vibration
tot
. (The full Arduino sketch also uses this opportunity to pick a new random color.)
At this point we know:
- How long ago we last advanced a frame.
- How long ago we last detected a vibration.
The rest of the code makes decisions about what to do with this information, and translates those decisions into lit-up LEDs on the NeoPixel ring. This all happens fast enough that the vibration switch should be responsive and the animation fairly smooth.
If you look at the top of the sketch, you'll see the following configuration values:
#define NUM_LEDS 24 // 24 LED NeoPixel ring #define NEOPIXEL_PIN 0 // Pin D0 on Gemma #define VIBRATION_PIN 1 // Pin D1 on Gemma #define ANALOG_RANDOMNESS_PIN A1 // Not connected to anything #define DEFAULT_FRAME_LEN 60 #define MAX_FRAME_LEN 255 #define MIN_FRAME_LEN 5 #define COOLDOWN_AT 2000 #define DIM_AT 2500 #define BRIGHTNESS_HIGH 128 #define BRIGHTNESS_LOW 32
The first four are used to set up hardware pins on the Gemma, and should be left alone unless you wire things up differently or translate the sketch to another board with a different layout.
The rest offer various ways to change the timing and brightness of the animation:
DEFAULT_FRAME_LEN |
Baseline duration, in ms, of one frame of animation. Accepts values 0-255. The sketch resets frame_len to this value every time it sees a vibration. |
MAX_FRAME_LEN |
The longest acceptable frame (and thus the slowest animation). The cooldown check will gradually increase frame_len until it hits this value, and then reset to MIN_FRAME_LEN. |
MIN_FRAME_LEN |
Shortest allowable value for frame_len (and thus the fastest animation). Values 0-255, should be lower than MAX_FRAME_LEN. |
COOLDOWN_AT |
Number of ms to wait after a vibration event before increasing frame_len, thus slowing the animation over time. |
DIM_AT |
Number of ms to wait before dimming the LEDs to BRIGHTNESS_LOW. |
BRIGHTNESS_HIGH |
Brightness for LEDs when in high-brightness mode - a value 0-255. Keep in mind this will impact battery life. |
BRIGHTNESS_LOW |
Brightness for LEDs when in low-brightness mode. A value 0-255. |
Text editor powered by tinymce.