-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
82 lines (76 loc) · 3.07 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
********************************************************************
ROCKET FLIGHT DATALOGGER
ES2A8 GROUP PROJECT, GROUP CHARLIE
James Brandreth & Gayatri Kapur
February 2016
********************************************************************
Program to read pressure, temperature, altitude from an MPL3115A2
barometric pressure sensor, 3-axis acceleration from an ADXL345
accelerometer and write the data to a file on an SD card.
********************************************************************
LIBRARIES
This program uses two third-party libraries from Adafruit:
Adafruit_Sensor.hm, Adafruit_ADXL345_U.h.
It also uses an edited version of the Adafruit_MPL3115A2.h library,
which should be provided with this program.
The Settings.h library is custom-built and also should be provided.
********************************************************************
*/
#include <Wire.h>
#include <SD.h>
#include <Adafruit_MPL3115A2.h> //NB must use edited version of this library provided with this program
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <Settings.h>
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
Settings set = Settings();
File flight;
float pressure, altitude, temperature, accX, accY, accZ;
int OverSampleRate, interval, stoptime;
uint32_t totalTime = 0, loopStart = 0, startTime = 0;
String line;
void setup(){
SD.begin(4);
baro.begin();
accel.begin();
accel.setRange(ADXL345_RANGE_16_G);
OverSampleRate = set.initialiseOverSampleRate(); //READ SETTINGS FILE TO SET OVERSAMPLE RATE
interval = set.initialiseMinInterval(); //READ SETTINGS FILE TO SET MINIMUM READING INTERVAL
stoptime = set.initialiseStopTime();
flight = SD.open("flight.txt", FILE_WRITE);
//======PRINT DATA FILE HEADER======
flight.println("ROCKET FLIGHT DATALOGGER - DATA FILE");
flight.println("GROUP CHARLIE");
flight.println("Oversample ratio: 2^" + String((OverSampleRate >>= 3)) + "\t Minimum reading interval: " + String(interval));
flight.println("Time(ms) \t Pressure (pascals)\t Altitude (m) \t Temp (degreeC) \t X acceleration (m/s^2) \t Y acceleration (m/s^2) \t Z acceleration (m/s^2)");
flight.flush();
}
void loop(){
totalTime=millis();
if(totalTime-loopStart > interval){
loopStart=millis();
//======ALTIMETER READING======
altitude = baro.getAltitude(OverSampleRate);
//======PRESSURE READING=======
pressure = baro.getPressure(OverSampleRate);
//=======TEMPERATURE READING=======
temperature = baro.getTemperature(OverSampleRate);
//=======ACCELEROMETER READING=======
sensors_event_t event;
accel.getEvent(&event);
accX = event.acceleration.x;
accY = event.acceleration.y;
accZ = event.acceleration.z;
//=======ASSEMBLE AND PRINT STRING=======
line=String(totalTime)+"\t"+String(pressure)+"\t"+String(altitude)+"\t"+String(temperature)+"\t"+String(accX)+"\t"+String(accY)+"\t"+String(accZ);
flight.println(line);
flight.flush(); //SAVES LINE TO SD CARD
}
if((totalTime - startTime) >= stoptime)
{
flight.close();
while(1){} // Stops taking readings at time defined in settings file
}
}