Here are my two separate codes, the gyro code and acceleration code respectively...
Code: Select all
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_FXAS21002C.h>
#include <SD.h>
#include <SPI.h>
/* Assign a unique ID to this sensor at the same time */
Adafruit_FXAS21002C gyro = Adafruit_FXAS21002C(0x0021002C);
File myFile;
const int ChipSelect = 10;
void setup(void)
{
Serial.begin(9600);
/* Wait for the Serial Monitor */
while (!Serial) {
delay(1);
}
Serial.println("Initializing SD Card...");
if (!SD.begin(10))
{
Serial.println("Initialization Failed!!");
return;
}
Serial.println("Initialization Complete");
delay(2000);
Serial.println("Gyroscope Test"); Serial.println("");
/* Initialise the sensor */
if(!gyro.begin())
{
/* There was a problem detecting the FXAS21002C ... check your connections */
Serial.println("Ooops, no FXAS21002C detected ... Check your wiring!");
while(1);
}
Serial.println("Gyro working, beginning data in 3 Seconds.");
delay(3000);
/* Display some basic information on this sensor */
}
void loop(void)
{
myFile = SD.open("data.txt", FILE_WRITE);
/* Get a new sensor event */
sensors_event_t event;
gyro.getEvent(&event);
if (myFile)
{
/* Display the results (speed is measured in rad/s) */
Serial.print("X: "); Serial.print(event.gyro.x); Serial.print(" ");
myFile.print("X: "); myFile.print(event.gyro.x); myFile.print(" ");
Serial.print("Y: "); Serial.print(event.gyro.y); Serial.print(" ");
myFile.print("Y: "); myFile.print(event.gyro.y); myFile.print(" ");
Serial.print("Z: "); Serial.print(event.gyro.z); Serial.print(" ");
myFile.print("Z: "); myFile.print(event.gyro.z); myFile.print(" ");
Serial.println("rad/s ");
myFile.println("rad/s ");
myFile.close();
}
else
{
Serial.println("Error in opening file");
}
delay(250);
}
Code: Select all
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_FXOS8700.h>
#include <SD.h>
#include <SPI.h>
/* Assign a unique ID to this sensor at the same time */
Adafruit_FXOS8700 accelmag = Adafruit_FXOS8700(0x8700A, 0x8700B);
File myFile;
const int ChipSelect = 10;
void setup(void)
{
Serial.begin(9600);
/* Wait for the Serial Monitor */
while (!Serial) {
delay(1);
}
Serial.println("Initializing SD Card...");
if (!SD.begin(10))
{
Serial.println("Initialization Failed!!!");
return;
}
Serial.println("Initialization Complete");
delay(1000);
Serial.println("FXOS8700 Test"); Serial.println("");
/* Initialise the sensor */
if(!accelmag.begin(ACCEL_RANGE_4G))
{
/* There was a problem detecting the FXOS8700 ... check your connections */
Serial.println("Ooops, no FXOS8700 detected ... Check your wiring!");
while(1);
}
Serial.println("Accelerometer working, beginning data in 3 seconds.");
delay(3000);
}
void loop(void)
{
myFile = SD.open("data.txt", FILE_WRITE);
sensors_event_t aevent, mevent;
/* Get a new sensor event */
accelmag.getEvent(&aevent, &mevent);
if (myFile)
{
/* Display the accel results (acceleration is measured in m/s^2) */
Serial.print("X: "); Serial.print(aevent.acceleration.x, 8); Serial.print(" ");
myFile.print("X: "); myFile.print(aevent.acceleration.x, 8); myFile.print(" ");
Serial.print("Y: "); Serial.print(aevent.acceleration.y, 8); Serial.print(" ");
myFile.print("Y: "); myFile.print(aevent.acceleration.y, 8); myFile.print(" ");
Serial.print("Z: "); Serial.print(aevent.acceleration.z, 8); Serial.print(" ");
myFile.print("Z: "); myFile.print(aevent.acceleration.z, 8); myFile.print(" ");
Serial.println("m/s^2");
myFile.println("m/s^2");
myFile.close();
}
else
{
Serial.println("Error in opening file");
}
delay(125);
}
Any tips on coding and or how I should tackle this problem is much appreciated.
Thanks