Skip to content

Instantly share code, notes, and snippets.

@cloudwalking
Created December 16, 2015 00:26
Show Gist options
  • Save cloudwalking/0a32a5c30852136619d2 to your computer and use it in GitHub Desktop.
Save cloudwalking/0a32a5c30852136619d2 to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include "LPD8806.h"
// LED parameters:
#define LED_COUNT 16
#define DATA_PIN 6
#define CLOCK_PIN 12
LPD8806 strip;
void setup() {
strip = LPD8806(LED_COUNT, DATA_PIN, CLOCK_PIN);
// Turn the strip on.
strip.begin();
// Refresh the strip.
strip.show();
}
void loop() {
breath();
}
const int CYCLE_MILLISECONDS = 5000; // 5 second breathing cycle.
const uint8_t KEYFRAMES[] = {
// Rising
20, 21, 22, 24, 26, 28, 31, 34, 38, 41, 45, 50, 55, 60, 66, 73, 80, 87, 95,
103, 112, 121, 131, 141, 151, 161, 172, 182, 192, 202, 211, 220, 228, 236,
242, 247, 251, 254, 255,
// Falling
254, 251, 247, 242, 236, 228, 220, 211, 202, 192, 182, 172, 161, 151, 141,
131, 121, 112, 103, 95, 87, 80, 73, 66, 60, 55, 50, 45, 41, 38, 34, 31, 28,
26, 24, 22, 21, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
};
unsigned long lastBreath = 0.0;
int keyframePointer = 0;
void breath() {
int numKeyframes = sizeof(KEYFRAMES) - 1;
float period = CYCLE_MILLISECONDS / numKeyframes;
unsigned long now = millis();
if ((now - lastBreath) > period) {
lastBreath = now;
for (int i = 0; i < strip.numPixels(); i++) {
uint8_t color = (127 * KEYFRAMES[keyframePointer]) / 256;
strip.setPixelColor(i, color, 0, 0);
}
strip.show();
// Increment the keyframe pointer.
if (++keyframePointer > numKeyframes) {
// Reset to 0 after the last keyframe.
keyframePointer = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment