Hello I am currently making a weather balloon controller that mainly records acceleration once it hits the Jet stream. I have the accelerometer working for the most part but am having trouble actually having data log to the SD card. To preface this, this is a school project and I have VERY little experience in programming in general so I was kinda just fusing 2 scripts together hoping it would work. It did to some extent but now I'd like to seek outside help. Any help is greatly appreciated!
#include <Adafruit_LSM303_Accel.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
/* Assign a unique ID to this sensor at the same time */
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(54321);
const int chipSelect = 10;
void displaySensorDetails(void) {
sensor_t sensor;
accel.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print("Sensor: ");
Serial.println(sensor.name);
Serial.print("Driver Ver: ");
Serial.println(sensor.version);
Serial.print("Unique ID: ");
Serial.println(sensor.sensor_id);
Serial.print("Max Value: ");
Serial.print(sensor.max_value);
Serial.println(" m/s^2");
Serial.print("Min Value: ");
Serial.print(sensor.min_value);
Serial.println(" m/s^2");
Serial.print("Resolution: ");
Serial.print(sensor.resolution);
Serial.println(" m/s^2");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
void setup(void) {
#ifndef ESP8266
while (!Serial)
; // will pause Zero, Leonardo, etc until serial console opens
#endif
Serial.begin(9600);
Serial.println("Accelerometer Test");
Serial.println("");
/* Initialise the sensor */
if (!accel.begin()) {
/* There was a problem detecting the ADXL345 ... check your connections */
Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
while (1)
;
}
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
/* Display some basic information on this sensor */
displaySensorDetails();
accel.setRange(LSM303_RANGE_4G);
Serial.print("Range set to: ");
lsm303_accel_range_t new_range = accel.getRange();
switch (new_range) {
case LSM303_RANGE_2G:
Serial.println("+- 2G");
break;
case LSM303_RANGE_4G:
Serial.println("+- 4G");
break;
case LSM303_RANGE_8G:
Serial.println("+- 8G");
break;
case LSM303_RANGE_16G:
Serial.println("+- 16G");
break;
}
accel.setMode(LSM303_MODE_NORMAL);
Serial.print("Mode set to: ");
lsm303_accel_mode_t new_mode = accel.getMode();
switch (new_mode) {
case LSM303_MODE_NORMAL:
Serial.println("Normal");
break;
case LSM303_MODE_LOW_POWER:
Serial.println("Low Power");
break;
case LSM303_MODE_HIGH_RESOLUTION:
Serial.println("High Resolution");
break;
}
}
void loop(void) {
/* Get a new sensor event */
sensors_event_t event;
accel.getEvent(&event);
String dataString = "";
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("X: ");
Serial.print(event.acceleration.x);
Serial.print(" ");
Serial.print("Y: ");
Serial.print(event.acceleration.y);
Serial.print(" ");
Serial.print("Z: ");
Serial.print(event.acceleration.z);
Serial.print(" ");
Serial.println("m/s^2");
dataFile.print("X: ");
dataFile.print(event.acceleration.x);
dataFile.print(" ");
dataFile.print("Y: ");
dataFile.print(event.acceleration.y);
dataFile.print(" ");
dataFile.print("Z: ");
dataFile.print(event.acceleration.z);
dataFile.print(" ");
dataFile.println("m/s^2");
dataFile.flush();
/* Delay before the next sample */
delay(500);
}