Skip to content

Instantly share code, notes, and snippets.

@mookman288
Last active November 22, 2016 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mookman288/7a576a649b8acfa5dd6000e72095281b to your computer and use it in GitHub Desktop.
Save mookman288/7a576a649b8acfa5dd6000e72095281b to your computer and use it in GitHub Desktop.
NeoPixel chase and fade with vibration sensor support on Gemma.
#include <Adafruit_NeoPixel.h>
#include <math.h>
#define LEDCOUNT 8
#define PINLED 1
#define PINSENSOR 0
#define RGB 3
#define STEPS 20
#define TIMER 4
#define TIMEOUT 5
//Declare color variables.
int colorAlpha[] = {0, 20, 18};
int colorBravo[] = {24, 0, 18};
//Declare chase fade variables.
int colorMap[STEPS * 2][RGB];
int currentColor = 0;
int currentLed = -1;
int currentStep = 0;
int isRunning = 0;
int incremental[RGB];
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDCOUNT, PINLED, NEO_GRB + NEO_KHZ800);
void setup() {
//Setup the NeoPixel strip.
strip.begin();
//Start the color as off.
strip.show();
//For each potential color source.
for (int i = 0; i < RGB; i++) {
//Determine the incremental values.
incremental[i] = (colorBravo[i] - colorAlpha[i]) / STEPS;
}
//For each potential iteration of the color map.
for (int i = 0; i < (STEPS); i++) {
//For each potential color source.
for (int n = 0; n < RGB; n++) {
//Create a parabola of all possible colors to fade.
colorMap[i][n] = round(colorBravo[n] - (incremental[n] * i));
colorMap[i + STEPS][n] = round(colorAlpha[n] + (incremental[n] * i));
}
}
//Setup the vibration sensor.
pinMode(PINSENSOR, INPUT_PULLUP);
}
void loop() {
//Based on whether the sensor is in use.
if (digitalRead(PINSENSOR) == LOW) {
runColors();
//Or if the application is still running.
} else if (isRunning > 0) {
turnOffColors();
}
}
static void runColors() {
//Set state of running.
isRunning = 1;
//Start the loop.
for (int i = 0; i < (LEDCOUNT); i++) {
//Use state to turn off the current LED's neighbor.
strip.setPixelColor(currentLed - 1, 0, 0, 0);
//Use state to set the current LED's color.
strip.setPixelColor(currentLed, colorMap[currentColor][0] * 0.5, colorMap[currentColor][1] * 0.5, colorMap[currentColor][2] * 0.5);
//Turn on the new LED.
strip.setPixelColor(i, colorMap[currentColor][0], colorMap[currentColor][1], colorMap[currentColor][2]);
//Use state to turn off the current LED's partner's neighbor.
strip.setPixelColor((LEDCOUNT - 1) - currentLed + 1, 0, 0, 0);
//Use state to set the current LED's color.
strip.setPixelColor((LEDCOUNT - 1) - currentLed, colorMap[currentColor][0] * 0.5, colorMap[currentColor][1] * 0.5, colorMap[currentColor][2] * 0.5);
//Turn on the current LED's partner.
strip.setPixelColor((LEDCOUNT - 1) - i, colorMap[currentColor][0], colorMap[currentColor][1], colorMap[currentColor][2]);
//Write the colors.
strip.show();
//Increment the current color.
currentColor = (currentColor >= ((STEPS * 2) - 1)) ? 0 : (currentColor + 1);
//Set the current LED.
currentLed = i;
//Set a delay to slow the loop down.
delay(100);
}
}
static void turnOffColors() {
//For each LED.
for (int i = 0; i < LEDCOUNT; i++) {
//Set the color of the LED.
strip.setPixelColor(i, 0, 0, 0);
}
//Set state of running.
isRunning = 0;
//Write the colors.
strip.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment