Skip to content

Instantly share code, notes, and snippets.

@ReidCarlberg
Created October 26, 2013 18:52
Show Gist options
  • Save ReidCarlberg/7173169 to your computer and use it in GitHub Desktop.
Save ReidCarlberg/7173169 to your computer and use it in GitHub Desktop.
Halloween 2013
/*
Terrible Code written by @ReidCarlberg
I promise to refactor it sometime.
*/
#include <Wire.h>
#include <Adafruit_LSM303.h>
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define PINShort 10
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel body = Adafruit_NeoPixel(6, PINShort, NEO_GRB + NEO_KHZ800);
Adafruit_LSM303 lsm;
int lastX = 0;
int lastY = 0;
int lastZ = 0;
int isOff = 1;
int isKitt = 0;
int isGlitter = 0;
unsigned long glitterStart = 0;
unsigned long heartBeatStart = 0;
void setup() {
Serial.begin(9600);
// Try to initialise and warn if we couldn't detect the chip
if (!lsm.begin())
{
Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!");
while (1);
}
strip.begin();
strip.show(); // Initialize all pixels to 'off'
body.begin();
body.show();
}
void loop() {
//general accellerometer read
lsm.read();
int currX = abs((int) lsm.accelData.x);
int currY = abs((int) lsm.accelData.y);
int currZ = abs((int) lsm.accelData.z);
if (abs(currX - lastX) > 100 || abs(currY - lastY) > 100 ) {
if (isOff == 1) {
isOff = 0;
}
if (isKitt == 0 && isGlitter == 0) {
isKitt = 1;
} else if (isKitt == 1 && isGlitter == 0) {
isKitt = 0;
isGlitter = 1;
glitterStart = millis();
}
} else {
if (isKitt == 1 || (isGlitter == 1 && millis() - glitterStart > 3000)) {
isOff = 1;
isKitt = 0;
isGlitter = 0;
}
}
lastX = abs(currX);
lastY = abs(currY);
lastZ = abs(currZ);
Serial.print("Accel X: "); Serial.print((int)lsm.accelData.x); Serial.print(" ");
Serial.print("Y: "); Serial.print((int)lsm.accelData.y); Serial.print(" ");
Serial.print("Z: "); Serial.println((int)lsm.accelData.z); Serial.print(" ");
if (isOff == 1) {
handleHeartBeat();
} else if (isKitt == 1) {
handleBackAndForth();
} else {
handleGlitter();
}
}
void handleHeartBeat() {
if (millis() - heartBeatStart > 1500) {
strip.setPixelColor(11, strip.Color(0,0,255));
strip.setPixelColor(15, strip.Color(0,0,255));
strip.show();
delay(250);
strip.setPixelColor(11, strip.Color(0,0,0));
strip.setPixelColor(15, strip.Color(0,0,0));
strip.show();
delay(250);
strip.setPixelColor(11, strip.Color(0,0,255));
strip.setPixelColor(15, strip.Color(0,0,255));
strip.show();
delay(250);
strip.setPixelColor(11, strip.Color(0,0,0));
strip.setPixelColor(15, strip.Color(0,0,0));
strip.show();
delay(250);
heartBeatStart = millis();
}
}
void handleBackAndForth() {
for (int x = 1; x < strip.numPixels()-1; x++) {
strip.setPixelColor(x-1, strip.Color(255,255,255));
strip.setPixelColor(x, strip.Color(255,255,255));
strip.setPixelColor(x+1, strip.Color(255,255,255));
strip.show();
delay(10);
strip.setPixelColor(x-1, strip.Color(0,0,0));
strip.setPixelColor(x, strip.Color(0,0,0));
strip.setPixelColor(x+1, strip.Color(0,0,0));
strip.show();
delay(10);
}
for (int x = strip.numPixels()-1; x > 1; x--) {
strip.setPixelColor(x-1, strip.Color(255,255,255));
strip.setPixelColor(x, strip.Color(255,255,255));
strip.setPixelColor(x+1, strip.Color(255,255,255));
strip.show();
delay(10);
strip.setPixelColor(x-1, strip.Color(0,0,0));
strip.setPixelColor(x, strip.Color(0,0,0));
strip.setPixelColor(x+1, strip.Color(0,0,0));
strip.show();
delay(10);
}
}
void handleGlitter() {
int glitter = random(0,29);
int glitterBody = random(0,6);
int r = 255;
int g = 255;
int b = 255;
int color = random(1,4);
if (color == 1) {
g = 0;
b = 0;
} else if (color == 2) {
r = 0;
b=0;
} else if (color == 3) {
r=0;
g=0;
}
strip.setPixelColor(glitter, strip.Color(r,g,b));
strip.show();
delay(50);
strip.setPixelColor(glitter, strip.Color(0,0,0));
strip.show();
delay(50);
body.setPixelColor(glitterBody, body.Color(r,g,b));
body.show();
delay(50);
body.setPixelColor(glitterBody, body.Color(0,0,0));
body.show();
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment