circuit playground with lis3dh and neopixels

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Bagals
 
Posts: 29
Joined: Sun Mar 20, 2016 9:11 pm

circuit playground with lis3dh and neopixels

Post by Bagals »

Hello! I am trying to use the lis3dh accelerometer on the circuit playground classic to activate both the onboard neopixels and some external neopixels when it detects a shake, and turn off after some time. I keep getting a long compiling error code whenever I use both the neopixel library and the cp library, so I made some work around code to just use the adafruit lis3dh and neopixel libraries. I used the lid3dh example code to make a new work around. Right now it lights up with a shake as long as its tied to the serial monitor but it doesn't turn off after a delay. Ideally, it would turn back off, and eventually I need it to run without the serial monitor. Any ideas on what might be wrong? Here's the code below:

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
  }
}
If anyone could help, it would be much appreciated!

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: circuit playground with lis3dh and neopixels

Post by adafruit_support_mike »

What's the exact error message you get?

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”