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-01 at 2:56 PM
#2192
Anthony,
I have run that Sample Code but do not have success when I try to merge it in with my code.
This is my current code:
Aq_Bar_Pressure.ino
Arduino
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
#include <Wire.h> #include <SparkFun_MS5803_I2C.h> #include <Wire.h> #include <Adafruit_MPL115A2.h> Adafruit_MPL115A2 mpl115a2; #include <Wire.h> #include "Sodaq_DS3231.h" char weekDay[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; //year, month, date, hour, min, sec and week-day(starts from 0 and goes to 6) //writing any non-existent time-data may interfere with normal operation of the RTC. //Take care of week-day also. DateTime dt(2017, 04, 24, 15, 04, 0, 1); // Begin class with selected address // available addresses (selected by jumper on board) // default is ADDRESS_HIGH // ADDRESS_HIGH = 0x76 // ADDRESS_LOW = 0x77 MS5803 sensor(ADDRESS_HIGH); //Create variables to store results float temperature_c, temperature_f; double pressure_abs, pressure_relative, altitude_delta, pressure_baseline; // Create Variable to store altitude in (m) for calculations; double base_altitude = 1655.0; // Altitude of SparkFun's HQ in Boulder, CO. in (m) /* * Simple data logger. */ #include <SPI.h> #include "SdFat.h" // SD chip select pin. Be sure to disable any other SPI devices such as Enet. const uint8_t chipSelect = SS; // Interval between data records in milliseconds. // The interval must be greater than the maximum SD write latency plus the // time to acquire and write data to the SD to avoid overrun errors. // Run the bench example to check the quality of your SD card. const uint32_t SAMPLE_INTERVAL_MS = 1000; // Log file base name. Must be six characters or less. #define FILE_BASE_NAME "Data" //------------------------------------------------------------------------------ // File system object. SdFat sd; // Log file. SdFile file; // Time in micros for next data record. uint32_t logTime; //============================================================================== // User functions. Edit writeHeader() and logData() for your requirements. const uint8_t ANALOG_COUNT = 4; //------------------------------------------------------------------------------ // Write data header. void writeHeader() { file.print(F("micros")); for (uint8_t i = 0; i < ANALOG_COUNT; i++) { file.print(F(",adc")); file.print(i, DEC); } file.println(); } //------------------------------------------------------------------------------ // Log a data record. void logData() { uint16_t data[ANALOG_COUNT]; // Read all channels to avoid SD write latency between readings. for (uint8_t i = 0; i < ANALOG_COUNT; i++) { data[i] = analogRead(i); } // Write data to file. Start with log time in micros. file.print(logTime); // Write ADC data to CSV record. for (uint8_t i = 0; i < ANALOG_COUNT; i++) { file.write(','); file.print(data[i]); } file.println(); } //============================================================================== // Error messages stored in flash. #define error(msg) sd.errorHalt(F(msg)) //------------------------------------------------------------------------------ void setup() { const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1; char fileName[13] = FILE_BASE_NAME "00.csv"; Serial.begin(9600); // Wait for USB Serial while (!Serial) { SysCall::yield(); } delay(1000); Serial.println(F("Type any character to start")); while (!Serial.available()) { SysCall::yield(); } // Initialize the SD card at SPI_HALF_SPEED to avoid bus errors with // breadboards. use SPI_FULL_SPEED for better performance. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) { sd.initErrorHalt(); } // Find an unused file name. if (BASE_NAME_SIZE > 6) { error("FILE_BASE_NAME too long"); } while (sd.exists(fileName)) { if (fileName[BASE_NAME_SIZE + 1] != '9') { fileName[BASE_NAME_SIZE + 1]++; } else if (fileName[BASE_NAME_SIZE] != '9') { fileName[BASE_NAME_SIZE + 1] = '0'; fileName[BASE_NAME_SIZE]++; } else { error("Can't create file name"); } } if (!file.open(fileName, O_CREAT | O_WRITE | O_EXCL)) { error("file.open"); } // Read any Serial data. do { delay(10); } while (Serial.available() && Serial.read() >= 0); Serial.print(F("Logging to: ")); Serial.println(fileName); Serial.println(F("Type any character to stop")); // Write data header. writeHeader(); // Start on a multiple of the sample interval. logTime = micros()/(1000UL*SAMPLE_INTERVAL_MS) + 1; logTime *= 1000UL*SAMPLE_INTERVAL_MS; Serial.begin(9600); Wire.begin(); rtc.begin(); rtc.setDateTime(dt); //Adjust date-time as defined 'dt' above Serial.begin(9600); //Retrieve calibration constants for conversion math. sensor.reset(); sensor.begin(); pressure_baseline = sensor.getPressure(ADC_4096); Serial.begin(9600); Serial.println("Hello!"); Serial.println("Getting barometric pressure ..."); mpl115a2.begin(); } void loop() { // Time for next record. logTime += 1000UL*SAMPLE_INTERVAL_MS; // Wait for log time. int32_t diff; do { diff = micros() - logTime; } while (diff < 0); // Check for data rate too high. if (diff > 10) { error("Missed data record"); } logData(); // Force data to SD and update the directory entry to avoid data loss. if (!file.sync() || file.getWriteError()) { error("write error"); } if (Serial.available()) { // Close file and stop. file.close(); Serial.println(F("Done")); SysCall::halt(); } DateTime now = rtc.now(); //get the current date-time Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.date(), DEC); Serial.print(' '); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.println(); Serial.print(weekDay[now.dayOfWeek()]); Serial.println(); // To measure to higher degrees of precision use the following sensor settings: // ADC_256 // ADC_512 // ADC_1024 // ADC_2048 // ADC_4096 // Read temperature from the sensor in deg C. This operation takes about temperature_c = sensor.getTemperature(CELSIUS, ADC_512); // Read temperature from the sensor in deg F. Converting // to Fahrenheit is not internal to the sensor. // Additional math is done to convert a Celsius reading. temperature_f = sensor.getTemperature(FAHRENHEIT, ADC_512); // Read pressure from the sensor in mbar. pressure_abs = sensor.getPressure(ADC_4096); // Report values via UART Serial.print("Temperature C = "); Serial.println(temperature_c); Serial.print("Temperature F = "); Serial.println(temperature_f); Serial.print("Water Pressure abs (mbar)= "); Serial.println(pressure_abs); float pressureKPA = 0, temperatureC = 0; pressureKPA = mpl115a2.getPressure(); Serial.print("Atmospheric Pressure (kPa): "); Serial.print(pressureKPA, 4); Serial.println(" kPa"); delay(10000); } |