Hi, I am a student relatively new to coding with Arduinos, and was wondering if anyone could point me in the right direction.
I wrote this code using the SparkFun LSM9DS1 IMU library, and though it compiles fine, when I upload it to my arduino, the serial monitor shows it only printing "Started" or no output at all.
#include <SPI.h> // SPI library included for SparkFunLSM9DS1
#include <Wire.h> // I2C library included for SparkFunLSM9DS1
#include <SparkFunLSM9DS1.h>
#include <time.h>
int buttonPin = 11; //set this number to the number of the pin the button is attatched to
float x, y, z;
LSM9DS1 imu;
void setup() {
//LSM9DS1 imu;
imu.settings.device.commInterface = IMU_MODE_SPI; // Set mode to SPI
imu.settings.device.mAddress = 9; // Mag CS pin connected to D9
imu.settings.device.agAddress = 10; // AG CS pin connected to D10
imu.settings.accel.scale = 16;
Serial.begin(9600);// initialize serial communication at 9600 bits per second
while (!Serial);
Serial.println("Started");
if (!imu.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
}
float vectorCalc(float x,float y,float z){
float accelVector = sqrt((x*x)+(y*y)+(z*z));
return accelVector;
}
void loop() {
Serial.println(" in loop, before starttime declared");
long startTime;
Serial.println(" starttime declared");
//int i;// I don't think this is needed since I'm not using an array anymore
float maximum;
int currentState;
while(1==1){//always true, loop runs forever
Serial.println("1 = 1");
startTime = time(NULL); //starttime set to current time
//Serial.println("starttime set to current time");
//i = 0;// I don't think this is needed since I'm not using an array anymore
maximum = 0;
//currentState = digitalRead(buttonPin); //check if button is HIGH (unpressed) or LOW (pressed). UNCOMMENT when button is attatched
currentState = LOW;//remove this line when button is attatched
if (currentState == LOW){
Serial.println("current state is low");
while (time(NULL) < (startTime + 10)){
if (imu.accelAvailable()) {
imu.readAccel();
}
float totalMag = vectorCalc(imu.ax,imu.ay,imu.az);//call vectorCalc function which turns x, y, and z components of acceleration into one combined magnitude
if (totalMag > maximum){//if value is bigger than the current max
maximum = totalMag;//make it the new max
}
//i++; // I don't think this is needed since I'm not using an array anymore
}
Serial.print(maximum, 2);//print maximum value with two decimal places
Serial.println(" gs");
}
}
}
I have added multiple print statements to determine if/where the program is breaking, but it seems to me the code is not even getting down to the loop function, and I don't know why. Any help on this would be greatly appreciated.
P.S. Also, I don't know if this is relevant, but I hade to comment out lines 1141, 1143, and 1146 in the SparkFun library cpp source to avoid compile time errors. I am using the SparkFun Library because I need to set the acceleration scale to 16 gs for my project. If there is any other way to achieve this without the hassle, I am open to suggestions. thanks