1

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>


// global variable declarations
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();

int seconds = 0;
int secondsPerTick = 0;
int counter = 0;
double pressure[15];
double heightA[15];
double temperature[15];
File myFile;
int tempNum;
const int chipSelect = BUILTIN_SDCARD;




//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.write("Pressure Recordings(going up): ");
    for(int i=0; i< 15; i++){
      myFile.write(pressure[i]);
      }
    Serial.println('\n');

       //header for the height sensor recordings
   myFile.write("Height Recordings(going up): ");
    //cycle through the array and store it in the SD card
    for (int j = 0; j <15; j ++){
     myFile.write(heightA[j]); 
    }
    Serial.println('\n');

    //header for the temperature sensor recordings
    myFile.write("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.write(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");
  }

     //as the rocket descends      
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.write("Pressure Recordings(going down): ");
    for(int i=0; i< 15; i++){
      myFile.write(pressure[i]);
      }
    Serial.println('\n');

       //header for the height sensor recordings
   myFile.write("Height Recordings(going down): ");
    //cycle through the array and store it in the SD card
    for (int j = 0; j <15; j ++){
     myFile.write(heightA[j]); 
    }
    Serial.println('\n');

    //header for the temperature sensor recordings
    myFile.write("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.write(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() { 

Here is what gets printed to the txt file:

€€€ƒ€ƒ‚ƒ€€„~ƒ‚€€€€ƒ€ƒ‚ƒ€€„~ƒ‚€ƒ‚‚‚„‡€„ƒ„……ƒ‚ƒ„Pressure Recordings: ƒ‚‚‚„‡€„ƒ„……ƒ‚ƒ„ Height Recordings: 10010110010101111000Temperature Recordings: Å……ƒƒ…€€ƒ‡‚w Pressure Recordings: Å……ƒƒ…€€ƒ‡‚w
Height Recordings: 000010100010111 Temperature Recordings: Pressure Recordings: Å……ƒƒ…€€ƒ‡‚w Height Recordings: 000010100010111 Temperature Recordings: Pressure Recordings: Ç…„†…„…„ˆ†„„€† Height Recordings: 000000000000000 Temperature Recordings: Pressure Recordings: Ç…„†…„…„ˆ†„„€† Height Recordings: 000000000000000 Temperature Recordings: Pressure Recordings: †‰†‡…††‰…‰ˆˆ…ˆ Height Recordings: 000000000000000 Temperature Recordings: Pressure Recordings: †‰†‡…††‰…‰ˆˆ…ˆ Height Recordings: 000000000000000 Temperature Recordings: Pressure Recordings(going up): ÀŽ‘ŽŒŽŽHeight Recordings(going up): ///000/0/0/0///Temperature Recordings(going up): Pressure Recordings(going down): ÀŽ‘ŽŒŽŽHeight Recordings(going down): ///000/0/0/0///Temperature Recordings(going down): Pressure Recordings(going up): ½–™––•˜–••š™š—™Height Recordings(going up): ///////////////Temperature Recordings(going up): Pressure Recordings(going down): ½–™––•˜–••š™š—™Height Recordings(going down): ///////////////Temperature Recordings(going down):

I also don't know how I can get the data to be formatted to where it reads like:

Pressure(going up):----

Altitude(going up):---

Temperature(going up):---

Pressure(going down):----

Altitude(going down):---

Temperature(going down):---

1
  • If you want the data stored as text instead if binary, use myFile.print() instead of myFile.write().
    – Dave X
    Mar 30, 2017 at 3:59

1 Answer 1

1

You should convert double to String with this kind of code:

char[9] stringval;
dtostrf(doubleval, 8, 4, stringval);

where 8 is presicion and 4 is number of digits after point.

More info: http://forum.arduino.cc/index.php?topic=282250.0

1
  • How would I implement that into my code? Sorry I'm new to Arduino and all of this has been trial and error for the most part with lots of Googling
    – Web Cobra
    Mar 30, 2017 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.