I am working on a class project where I am using a Teensy 3.5 with a Prop Shield with sensor attached that will be loaded into a rocket that I will launch and gather data from the altitude, pressure and temperature sensors and then read into an array and then dump at the peak of the height and when it hits the ground. I believe I have everything working when I test the code but when I open up the txt file I get what looks to be ASCII code but I don't know how to fix it.
Below is the code:
//import statements
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_MPL3115A2.h>
/* Temperature Sensor, Raw Numbers, Teensyduino Tutorial #4
http://www.pjrc.com/teensy/tutorial4.html
*/
// global variable declarations
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
const int chipSelect = BUILTIN_SDCARD;
int seconds = 0;
int secondsPerTick = 0;
int counter = 0;
double pressure[15];
double heightA[15];
double temperature[15];
File myFile;
int tempNum;
//test to make sure program uploaded
void setup() {
Serial.begin(9600);
Serial.println("Adafruit_MPL3115A2 test!");
while (!Serial){
}
if (! baro.begin()) {
Serial.println("Couldnt find sensor");
return;
}
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
while ( seconds < 5 ){
double pascals = baro.getPressure();
pressure[counter] = pascals;
Serial.print(pascals/3377); Serial.println(" Inches (Hg)");
double altm = baro.getAltitude();
heightA[counter] = altm;
Serial.print(altm); Serial.println(" meters");
double tempC = baro.getTemperature();
temperature[counter] = tempC;
Serial.print(tempC); Serial.println(" *C");
tempNum ++;
secondsPerTick++; //increments seconds per tick
if(secondsPerTick == 3){ //if its 3 reset "secondspertick" and increments "seconds"
secondsPerTick = 0;
seconds++;
}
counter++;
}
myFile = SD.open("Sensor4.txt", FILE_WRITE);
if(myFile) {
Serial.print("Writing to file...");
//cycle through the array and store it in the SD card
myFile.print("Pressure Recordings(going up): ");
for(int i=0; i< 15; i++){
myFile.print(pressure[i]);
}
Serial.println('\n');
//header for the height sensor recordings
myFile.print("Height Recordings(going up): ");
//cycle through the array and store it in the SD card
for (int j = 0; j <15; j ++){
myFile.print(heightA[j]);
}
Serial.println('\n');
//header for the temperature sensor recordings
myFile.print("Temperature Recordings(going up): ");
Serial.println('\n');
//cycle through the array and store it in the SD card
for (int k = 0; k <15; k++){
myFile.print(temperature[k]);
Serial.println('\n');
}
// close the file:
myFile.close();
Serial.println("done.");
}
else {
// if the file didn't open, print an error:
Serial.println("error opening sensor_recordings.txt");
}
while ( seconds > 5 ){
double pascals = baro.getPressure();
pressure[counter] = pascals;
Serial.print(pascals/3377); Serial.println(" Inches (Hg)");
myFile.write(pressure[counter]);
double altm = baro.getAltitude();
heightA[counter] = altm;
Serial.print(altm); Serial.println(" meters");
double tempC = baro.getTemperature();
temperature[counter] = tempC;
Serial.print(tempC); Serial.println(" *C");
tempNum ++;
secondsPerTick++; //increments seconds per tick
if(secondsPerTick == 3){ //if its 3 reset "secondspertick" and increments "seconds"
secondsPerTick = 0;
seconds++;
}
counter++;
}
myFile = SD.open("Sensor4.txt", FILE_WRITE);
if(myFile) {
Serial.print("Writing to file...");
//cycle through the array and store it in the SD card
myFile.print("Pressure Recordings(going down): ");
for(int i=0; i< 15; i++){
myFile.print(pressure[i]);
}
Serial.println('\n');
//header for the height sensor recordings
myFile.print("Height Recordings(going down): ");
//cycle through the array and store it in the SD card
for (int j = 0; j <15; j ++){
myFile.print(heightA[j]);
}
Serial.println('\n');
//header for the temperature sensor recordings
myFile.print("Temperature Recordings(going down): ");
Serial.println('\n');
//cycle through the array and store it in the SD card
for (int k = 0; k <15; k++){
myFile.print(temperature[k]);
Serial.println('\n');
}
// close the file:
myFile.close();
Serial.println("done.");
}
else {
// if the file didn't open, print an error:
Serial.println("error opening sensor_recordings.txt");
}
}
void loop() {
}
But when I go to see what is on the file I created I get just a bunch of what appears to be ASCII code attached is the txt file which I wrote too.
Another thing I am having issues with is that the formatting is incorrect and I can't figure out how to fix it I want it to look like:
Pressure (going up):
Height (going up):
altitude (going up):
Pressure (going down):
Height (going down):
altitude (going down):
But it is all printing on the same line even with the new line print statement and its printing 5 of each headers instead of 2 of each.
Thank you for your time.
SENSOR4.TXT (1.5 KB)