Code: Select all
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_NeoPixel.h>
#define LIS3DH_CS 8
#define PIXEL_PIN 6
#define PIXEL_COUNT 8
#define PlayPIXEL_PIN 17
#define PlayPIXEL_COUNT 10
Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS);
Adafruit_NeoPixel pixels(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel playPixels(PlayPIXEL_COUNT, PlayPIXEL_PIN, NEO_GRB + NEO_KHZ800);
void setup(void) {
pixels.begin(); // initialize external neopixels
playPixels.begin(); // initialize circuit playground neopixels
Serial.begin(115200);
while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("LIS3DH test!");
if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address
Serial.println("Couldnt start");
while (1) yield();
}
Serial.println("LIS3DH found!");
// lis.setRange(LIS3DH_RANGE_4_G); // 2, 4, 8 or 16 G!
Serial.print("Range = "); Serial.print(2 << lis.getRange());
Serial.println("G");
// lis.setDataRate(LIS3DH_DATARATE_50_HZ);
Serial.print("Data rate set to: ");
switch (lis.getDataRate()) {
case LIS3DH_DATARATE_1_HZ: Serial.println("1 Hz"); break;
case LIS3DH_DATARATE_10_HZ: Serial.println("10 Hz"); break;
case LIS3DH_DATARATE_25_HZ: Serial.println("25 Hz"); break;
case LIS3DH_DATARATE_50_HZ: Serial.println("50 Hz"); break;
case LIS3DH_DATARATE_100_HZ: Serial.println("100 Hz"); break;
case LIS3DH_DATARATE_200_HZ: Serial.println("200 Hz"); break;
case LIS3DH_DATARATE_400_HZ: Serial.println("400 Hz"); break;
case LIS3DH_DATARATE_POWERDOWN: Serial.println("Powered Down"); break;
case LIS3DH_DATARATE_LOWPOWER_5KHZ: Serial.println("5 Khz Low Power"); break;
case LIS3DH_DATARATE_LOWPOWER_1K6HZ: Serial.println("16 Khz Low Power"); break;
}
}
void loop() {
pixels.clear(); // Turns off all external neopixels
playPixels.clear(); // Turns off all circuit playground neopixels
lis.read(); // get X Y and Z data at once
// Then print out the raw data
Serial.print("X: "); Serial.print(lis.x);
Serial.print(" \tY: "); Serial.print(lis.y);
Serial.print(" \tZ: "); Serial.print(lis.z);
/* Or....get a new sensor event, normalized */
sensors_event_t event;
lis.getEvent(&event);
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("\t\tX: "); Serial.print(event.acceleration.x);
Serial.print(" \tY: "); Serial.print(event.acceleration.y);
Serial.print(" \tZ: "); Serial.print(event.acceleration.z);
Serial.println(" m/s^2 ");
Serial.println();
delay(200);
if ( (event.acceleration.x >= 15)||(event.acceleration.y >= 15)) { //when shaken, light up
for(int i=0; i<PIXEL_COUNT; i++) { // For each external neopixel
pixels.setPixelColor(i, pixels.Color(0, 150, 0)); //update neopixels to desired color and brightness
pixels.show(); // Activate the Neopixels on external light
}
for(int i=0; i<PlayPIXEL_COUNT; i++) { // For each Circuit Playground neopixel
playPixels.setPixelColor(i, playPixels.Color(150, 0, 0)); //update neopixels to desired color and brightness
playPixels.show(); // Activate the Neopixels on the Circuit Playground
}
delay (2000);
pixels.clear(); // Turns off all external neopixels
playPixels.clear(); // Turns off all circuit playground neopixels
}
}