Home › Forums › Mayfly Data Logger › Recording data from pressure sensors on an SD card › Reply To: Recording data from pressure sensors on an SD card
2017-05-05 at 4:04 PM
#2220
1 2 3 4 5 6 7 |
#include <SPI.h> #include <SD.h> #include <Wire.h> #include <Adafruit_MPL115A2.h> Adafruit_MPL115A2 mpl115a2; //Digital pin 12 is the |
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
#include <SPI.h> #include <SD.h> #include <Wire.h> #include <Adafruit_MPL115A2.h> Adafruit_MPL115A2 mpl115a2; //Digital pin 12 is the MicroSD slave select pin on the Mayfly #define SD_SS_PIN 12 //The data log file #define FILE_NAME "DataLog5.txt" //Data header (these lines get written to the beginning of a file when it's created) #define LOGGERNAME "Mayfly Writing pressure and temp data to microSD card" #define DATA_HEADER "SampleNumber(time?) , Air presure (kPa), Air Temperature(C)" int sampleinterval = 10; //time between samples, in seconds int samplenum = 1; // declare the variable "samplenum" and start with 1 //THIS NEEDS CHANGING FOR PRESSURE SENSOR int batteryPin = A6; // on the Mayfly board, pin A6 is connected to a resistor divider on the battery input //Define the values that will be read by pressure sensor float airPressureSenseValue; float airTempSenseValue; void setup() { //Initialise the serial connection Serial.begin(9600); delay(3000); mpl115a2.begin(); //Initialise log file setupLogFile(); //Echo the data header to the serial connection Serial.println(DATA_HEADER); } void loop() { String dataRec = createDataRecord(); //Save the data record to the ial connection Serial.println(dataRec); delay(sampleinterval*1000); //multiply by 1000 to convert from milliseconds to seconds } void setupLogFile() { //Initialise the SD card if (!SD.begin(SD_SS_PIN)) { Serial.println("Error: SD card failed to initialise or is missing."); //Hang // while (true); } //Check if the file already exists bool oldFile = SD.exists(FILE_NAME); //Open the file in write mode File logFile = SD.open(FILE_NAME, FILE_WRITE); //Add header information if the file did not already exist if (!oldFile) { logFile.println(LOGGERNAME); logFile.println(DATA_HEADER); } //Close the file to save it logFile.close(); } void logData(String rec) { //Re-open the file File logFile = SD.open(FILE_NAME, FILE_WRITE); //Write the CSV data logFile.println(rec); //Close the file to save it logFile.close(); } String createDataRecord() { //Create a String type data record in csv format //SampleNumber, Air pressure, Air temperature String data = ""; data += samplenum; //creates a string called "data", put in the sample number data += ","; //adds a comma between values airPressureSenseValue = mpl115a2.getPressure(); //assigns pressure to a variable airTempSenseValue = mpl115a2.getTemperature(); //assigns temp to a variable //Write pressure, temp to record data += airPressureSenseValue; data += ","; data += airTempSenseValue; samplenum++; //increment the sample number return data; } |
This is our current sketch. It will produce the data into the serial monitor. It will also record the header to the SD but not any of the readings from the sensors. Can anybody see any errors in the sketch or know why we can record the header but not the readings?
Thanks