RGB Lightshow using a Neopixel (WS2812) LED strip and the LIS3SH accelerometer. Used for my scooter at 35C3.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

184 lines
5.2 KiB

/*
* Blinky, Colorful Neopixel (WS2812) LED Stip Controlled by Motion Sensor (LIS3DH)
*
* copyright: Jannik Beyerstedt | https://jannikbeyerstedt.de | code@jannikbeyerstedt.de
* license: http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 License
*/
#include <Wire.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
const uint32_t accel_interval = 50;
#include <Adafruit_NeoPixel.h>
#define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT 16
const uint32_t neopx_defaultInt = 100;
// I2C: SCL A4, SDA A5, SDO float or GND: 0x18
Adafruit_LIS3DH lis = Adafruit_LIS3DH();
const int accel_thldNoiseX = 500; // below this is no movement
const int accel_thldNoiseY = 500;
const int accel_thldNoiseZ = 9000;
const int accel_thldMaxX = 5000; // light effects should scale until this point
const int accel_thldMaxY = 5000;
const int accel_thldMaxZ = 15000;
// Neopixel ring: NEO_RGB or NEO_GRB + NEO_KHZ400 or NEO_KHZ800
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ400);
uint32_t neopx_loopCnt = 0;
// some global variables
uint32_t accel_lastWake = 0;
uint32_t neopx_lastWake = 0;
uint32_t neopx_interval = 50;
uint32_t debug_lastWake = 0;
const uint32_t debug_interval = 200;
const int accel_rangeX = accel_thldMaxX - accel_thldNoiseX;
const int accel_rangeY = accel_thldMaxY - accel_thldNoiseY;
const int accel_rangeZ = accel_thldMaxZ - accel_thldNoiseZ;
void setup() {
Serial.begin(115200);
Serial.println("Neopixel Animation with LIS3DH Motion Sensor\n");
if (!lis.begin(0x18)) { // change this to 0x19 for alternative i2c address
Serial.println("Couldnt start");
while (1)
;
}
lis.setRange(LIS3DH_RANGE_4_G); // 2, 4, 8 or 16 G!
strip.begin();
strip.show(); // Initialize all pixels to 'off'
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
uint32_t currentTime_millis = millis();
// READ SENSOR
if (currentTime_millis - accel_lastWake >= accel_interval) {
accel_lastWake = currentTime_millis;
lis.read();
int32_t diffX = abs(lis.x) - accel_thldNoiseX;
if (diffX > accel_rangeX) {
diffX = accel_rangeX;
}
int32_t diffY = abs(lis.y) - accel_thldNoiseY;
if (diffY > accel_rangeY) {
diffY = accel_rangeY;
}
int32_t diffZ = abs(lis.z) - accel_thldNoiseZ;
if (diffZ > accel_rangeZ) {
diffZ = accel_rangeZ;
}
neopx_interval = neopx_defaultInt;
if (diffX > 0) {
int32_t fasterInt = neopx_defaultInt - (diffX * (neopx_defaultInt - 5) / accel_rangeX);
if (neopx_interval > fasterInt) {
neopx_interval = fasterInt;
}
} else if (diffY > 0) {
uint32_t fasterInt = neopx_defaultInt - (diffY * (neopx_defaultInt - 5) / accel_rangeY);
if (neopx_interval > fasterInt) {
neopx_interval = fasterInt;
}
} else if (diffZ > 0) {
uint32_t fasterInt = neopx_defaultInt - (diffZ * (neopx_defaultInt - 5) / accel_rangeZ);
if (neopx_interval > fasterInt) {
neopx_interval = fasterInt;
}
}
}
// DO LIGHTSHOW
if (currentTime_millis - neopx_lastWake >= neopx_interval) {
neopx_lastWake = currentTime_millis;
neopx_loopCnt++;
// rainbow
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i + neopx_loopCnt) & 255));
}
strip.show();
}
// DEBUG OUTPUT
if (currentTime_millis - debug_lastWake >= debug_interval) {
debug_lastWake = currentTime_millis;
Serial.print("X: ");
Serial.print(lis.x);
Serial.print(" \tY: ");
Serial.print(lis.y);
Serial.print(" \tZ: ");
Serial.print(lis.z);
Serial.println();
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256; j++) {
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i + j) & 255));
}
strip.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
for (int j = 0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q = 0; q < 3; q++) {
for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, Wheel((i + j) % 255)); //turn every third pixel on
}
strip.show();
delay(wait);
for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, 0); //turn every third pixel off
}
}
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}