I've noticed that two tests with the exact same parameters have a huge difference of about 100g, which is well beyond the margin of error. Since I am testing a limited amount of prototypes, I can't afford to have bad data on any of them. The chart of results is attached; ideally the red and the yellow lines should be equal in height.
Below is my code.
Code: Select all
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_H3LIS331.h>
#include <Adafruit_Sensor.h>
// Used for software SPI
#define H3LIS331_SCK 13
#define H3LIS331_MISO 12
#define H3LIS331_MOSI 11
// Used for hardware & software SPI
#define H3LIS331_CS 10
Adafruit_H3LIS331 lis = Adafruit_H3LIS331();
const int buttonPin = 13; // the number of the pushbutton pin
const int ledVin = 10;
const int ledPin = 9;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(230400);
while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("H3LIS331 test!");
// if (!lis.begin_SPI(H3LIS331_CS)) {
// if (!lis.begin_SPI(H3LIS331_CS, H3LIS331_SCK, H3LIS331_MISO, H3LIS331_MOSI)) {
if (! lis.begin_I2C()) { // change this to 0x19 for alternative i2c address
Serial.println("Couldnt start");
while (1) yield();
}
Serial.println("H3LIS331 found!");
lis.setRange(H3LIS331_RANGE_400_G); // 100, 200, or 400 G!
Serial.print("Range set to: ");
switch (lis.getRange()) {
case H3LIS331_RANGE_100_G: Serial.println("100 g"); break;
case H3LIS331_RANGE_200_G: Serial.println("200 g"); break;
case H3LIS331_RANGE_400_G: Serial.println("400 g"); break;
}
lis.setDataRate(LIS331_DATARATE_1000_HZ);
Serial.print("Data rate set to: ");
switch (lis.getDataRate()) {
case LIS331_DATARATE_POWERDOWN: Serial.println("Powered Down"); break;
case LIS331_DATARATE_50_HZ: Serial.println("50 Hz"); break;
case LIS331_DATARATE_100_HZ: Serial.println("100 Hz"); break;
case LIS331_DATARATE_400_HZ: Serial.println("400 Hz"); break;
case LIS331_DATARATE_1000_HZ: Serial.println("1000 Hz"); break;
case LIS331_DATARATE_LOWPOWER_0_5_HZ: Serial.println("0.5 Hz Low Power"); break;
case LIS331_DATARATE_LOWPOWER_1_HZ: Serial.println("1 Hz Low Power"); break;
case LIS331_DATARATE_LOWPOWER_2_HZ: Serial.println("2 Hz Low Power"); break;
case LIS331_DATARATE_LOWPOWER_5_HZ: Serial.println("5 Hz Low Power"); break;
case LIS331_DATARATE_LOWPOWER_10_HZ: Serial.println("10 Hz Low Power"); break;
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(ledVin, OUTPUT);
digitalWrite(ledVin, HIGH);
}
}
void loop() {
buttonState = digitalRead(buttonPin);
unsigned long start_time = millis();
if (buttonState == LOW) {
Serial.println("testStart");
int lastmillis = 0;
int i = 0;
lastmillis = millis();
do{
digitalWrite(ledPin, HIGH);
sensors_event_t event;
lis.getEvent(&event);
//Serial.print(i); Serial.print(","); Serial.print("\t");
Serial.println(event.acceleration.z / SENSORS_GRAVITY_STANDARD);
}
while ((millis() - start_time) < 1500); {
// 1500ms have passed
Serial.println("COMPLETE");
digitalWrite(ledPin, LOW);
delay(1000);
}
//delay(0);
}
}