HI! I have a student working on the following project
https://learn.adafruit.com/3d-printed-f ... d?view=all
We are using the following accelerometer since I didn't have the adafruit one in house
https://www.sparkfun.com/products/12756
We have everything hooked up correctly, getting no errors in the Arduino Code, pushing to board, etc. But cannot get the Neopixel to light up. We also installed the accelerometer's library correctly....but still no go.
Any thoughts??
Flora & Accelerometer
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_bill
- Posts: 88151
- Joined: Sat Feb 07, 2009 10:11 am
Re: Flora & Accelerometer
The project and associated code are designed around the LSM303 accelerometer. The Sparkfun board you linked is an MMA8452Q. That will require a different library and most likely has a different API. Have you found a library for the MMA8452Q and modified the code accordingly?
- ssmithwy
- Posts: 4
- Joined: Wed Jun 06, 2018 1:29 pm
Re: Flora & Accelerometer
I do have the library installed. Not sure what API stands for?
I don't get any errors, just can't get it to turn on the Neopixel ring. I also have tested the accelerometer and know it is working.
Perplexed...
I feel like it must be something in the code but can't figure out what...??
I don't get any errors, just can't get it to turn on the Neopixel ring. I also have tested the accelerometer and know it is working.
Perplexed...
I feel like it must be something in the code but can't figure out what...??
- adafruit_support_bill
- Posts: 88151
- Joined: Sat Feb 07, 2009 10:11 am
Re: Flora & Accelerometer
API stands for Application Programming Interface. It is the set of functions that are supported by the library.
If you post your code, we can try to spot the problem.
If you post your code, we can try to spot the problem.
- ssmithwy
- Posts: 4
- Joined: Wed Jun 06, 2018 1:29 pm
Re: Flora & Accelerometer
Code: Select all
#include <SparkFun_MMA8452Q.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Adafruit_LSM303.h>
#include <Adafruit_NeoPixel.h>
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, 9, NEO_GRB + NEO_KHZ800);
Adafruit_LSM303 lsm;
// Here is where you can put in your favorite colors that will appear!
// just add new {nnn, nnn, nnn}, lines. They will be picked out randomly
// R G B
uint8_t myFavoriteColors[][3] = {{200, 0, 200}, // purple
{0, 117, 255}, // blue
{200, 200, 200}, // white
};
// don't edit the line below
#define FAVCOLORS sizeof(myFavoriteColors) / 3
// mess with this number to adjust TWINklitude :)
// lower number = more sensitive
#define MOVE_THRESHOLD 100
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'
}
void loop()
{
// Take a reading of accellerometer data
lsm.read();
Serial.print("Accel X: "); Serial.print(lsm.accelData.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(lsm.accelData.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(lsm.accelData.z); Serial.print(" ");
// Get the magnitude (length) of the 3 axis vector
// http://en.wikipedia.org/wiki/Euclidean_vector#Length
double storedVector = lsm.accelData.x*lsm.accelData.x;
storedVector += lsm.accelData.y*lsm.accelData.y;
storedVector += lsm.accelData.z*lsm.accelData.z;
storedVector = sqrt(storedVector);
Serial.print("Len: "); Serial.println(storedVector);
// wait a bit
delay(100);
// get new data!
lsm.read();
double newVector = lsm.accelData.x*lsm.accelData.x;
newVector += lsm.accelData.y*lsm.accelData.y;
newVector += lsm.accelData.z*lsm.accelData.z;
newVector = sqrt(newVector);
Serial.print("New Len: "); Serial.println(newVector);
// are we moving
if (abs(newVector - storedVector) > MOVE_THRESHOLD) {
Serial.println("Twinkle!");
flashRandom(5, 1); // first number is 'wait' delay, shorter num == shorter twinkle
flashRandom(5, 3); // second number is how many neopixels to simultaneously light up
flashRandom(5, 2);
}
}
void flashRandom(int wait, uint8_t howmany) {
for(uint16_t i=0; i<howmany; i++) {
// pick a random favorite color!
int c = random(FAVCOLORS);
int red = myFavoriteColors[c][0];
int green = myFavoriteColors[c][1];
int blue = myFavoriteColors[c][2];
// get a random pixel from the list
int j = random(strip.numPixels());
//Serial.print("Lighting up "); Serial.println(j);
// now we will 'fade' it in 5 steps
for (int x=0; x < 5; x++) {
int r = red * (x+1); r /= 5;
int g = green * (x+1); g /= 5;
int b = blue * (x+1); b /= 5;
strip.setPixelColor(j, strip.Color(r, g, b));
strip.show();
delay(wait);
}
// & fade out in 5 steps
for (int x=5; x >= 0; x--) {
int r = red * x; r /= 5;
int g = green * x; g /= 5;
int b = blue * x; b /= 5;
strip.setPixelColor(j, strip.Color(r, g, b));
strip.show();
delay(wait);
}
}
// LEDs will be off when done (they are faded to 0)
}
Last edited by adafruit_support_bill on Wed Jun 06, 2018 4:20 pm, edited 1 time in total.
Reason: Please use [code] tags when posting code to the forums.
Reason: Please use [code] tags when posting code to the forums.
- adafruit_support_bill
- Posts: 88151
- Joined: Sat Feb 07, 2009 10:11 am
Re: Flora & Accelerometer
You have included the SparkFun_MMA8452Q.h library. But you are still including the Adafruit_LSM303.h library.
And you are still declaring an LSM303 sensor in the code:
You need to remove the include for the Adafruit_LSM303.h and that sensor declaration, and replace it with one for your Sparkfun sensor.
When you do that, the compiler will most likely generate a bunch of errors for all the places where you are referencing the LSM303 device such as here:
That will need to be replaced with the equivalent Sparkfun code. Sparkfun should have some documentation for their API.
Looking at the example code here, their API for the MMA8452Q is a bit different that our API for the LSM303.
https://github.com/sparkfun/MMA8452_Acc ... _Basic.ino
Code: Select all
#include <SparkFun_MMA8452Q.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Adafruit_LSM303.h>
#include <Adafruit_NeoPixel.h>
Code: Select all
Adafruit_LSM303 lsm;
When you do that, the compiler will most likely generate a bunch of errors for all the places where you are referencing the LSM303 device such as here:
Code: Select all
// get new data!
lsm.read();
double newVector = lsm.accelData.x*lsm.accelData.x;
newVector += lsm.accelData.y*lsm.accelData.y;
newVector += lsm.accelData.z*lsm.accelData.z;
Looking at the example code here, their API for the MMA8452Q is a bit different that our API for the LSM303.
https://github.com/sparkfun/MMA8452_Acc ... _Basic.ino
- ssmithwy
- Posts: 4
- Joined: Wed Jun 06, 2018 1:29 pm
Re: Flora & Accelerometer
So here is what my student spliced together with all of the input. Still no error codes when we do it, still no lights being activated by the accelerometer. Our next "try" will be to actually solder everything together and then plug in battery. This works with the gemmas really well, but have never had this issue with Flora.
Thoughts?
Thoughts?
Code: Select all
#include <Wire.h> // Must include Wire library for I2C
#include <SparkFun_MMA8452Q.h> // Includes the SFE_MMA8452Q library
// Begin using the library by creating an instance of the MMA8452Q
// class. We'll call it "accel". That's what we'll reference from
// here on out.
MMA8452Q accel;
// The setup function simply starts serial and initializes the
// accelerometer.
void setup()
{
Serial.begin(9600);
Serial.println("MMA8452Q Test Code!");
// Choose your adventure! There are a few options when it comes
// to initializing the MMA8452Q:
// 1. Default init. This will set the accelerometer up
// with a full-scale range of +/-2g, and an output data rate
// of 800 Hz (fastest).
accel.init();
// 2. Initialize with FULL-SCALE setting. You can set the scale
// using either SCALE_2G, SCALE_4G, or SCALE_8G as the value.
// That'll set the scale to +/-2g, 4g, or 8g respectively.
//accel.init(SCALE_4G); // Uncomment this out if you'd like
// 3. Initialize with FULL-SCALE and DATA RATE setting. If you
// want control over how fast your accelerometer produces
// data use one of the following options in the second param:
// ODR_800, ODR_400, ODR_200, ODR_100, ODR_50, ODR_12,
// ODR_6, or ODR_1.
// Sets to 800, 400, 200, 100, 50, 12.5, 6.25, or 1.56 Hz.
//accel.init(SCALE_8G, ODR_6);
}
// The loop function will simply check for new data from the
// accelerometer and print it out if it's available.
void loop()
{
// Use the accel.available() function to wait for new data
// from the accelerometer.
if (accel.available())
{
// First, use accel.read() to read the new variables:
accel.read();
// accel.read() will update two sets of variables.
// * int's x, y, and z will store the signed 12-bit values
// read out of the accelerometer.
// * floats cx, cy, and cz will store the calculated
// acceleration from those 12-bit values. These variables
// are in units of g's.
// Check the two function declarations below for an example
// of how to use these variables.
printCalculatedAccels();
//printAccels(); // Uncomment to print digital readings
// The library also supports the portrait/landscape detection
// of the MMA8452Q. Check out this function declaration for
// an example of how to use that.
printOrientation();
Serial.println(); // Print new line every time.
}
}
// The function demonstrates how to use the accel.x, accel.y and
// accel.z variables.
// Before using these variables you must call the accel.read()
// function!
void printAccels()
{
Serial.print(accel.x, 3);
Serial.print("\t");
Serial.print(accel.y, 3);
Serial.print("\t");
Serial.print(accel.z, 3);
Serial.print("\t");
}
// This function demonstrates how to use the accel.cx, accel.cy,
// and accel.cz variables.
// Before using these variables you must call the accel.read()
// function!
void printCalculatedAccels()
{
Serial.print(accel.cx, 3);
Serial.print("\t");
Serial.print(accel.cy, 3);
Serial.print("\t");
Serial.print(accel.cz, 3);
Serial.print("\t");
}
// This function demonstrates how to use the accel.readPL()
// function, which reads the portrait/landscape status of the
// sensor.
void printOrientation()
{
// accel.readPL() will return a byte containing information
// about the orientation of the sensor. It will be either
// PORTRAIT_U, PORTRAIT_D, LANDSCAPE_R, LANDSCAPE_L, or
// LOCKOUT.
byte pl = accel.readPL();
switch (pl)
{
case PORTRAIT_U:
Serial.print("Portrait Up");
break;
case PORTRAIT_D:
Serial.print("Portrait Down");
break;
case LANDSCAPE_R:
Serial.print("Landscape Right");
break;
case LANDSCAPE_L:
Serial.print("Landscape Left");
break;
case LOCKOUT:
Serial.print("Flat");
break;
}
}
Last edited by adafruit_support_bill on Thu Jun 07, 2018 3:00 pm, edited 1 time in total.
Reason: Please use [code] tags when submitting code to the forums
Reason: Please use [code] tags when submitting code to the forums
- adafruit_support_bill
- Posts: 88151
- Joined: Sat Feb 07, 2009 10:11 am
Re: Flora & Accelerometer
Without soldered connections it would be unreliable at best. If it still doesn't work after soldering, post some photos and we'll see if we can spot any problems.Our next "try" will be to actually solder everything together
Please be positive and constructive with your questions and comments.