Barometric Pressure Sensor to SD card on Arduino Uno.
Update 2:
Updated debugging code
Update:
void setup works fine.
Despite working fine and recording data from my BMP sensor, void loop no longer likes my character arrays (or the sensor), and I was able to get the text file to be written to if I comment out all the arrays and sensor stuff.
The moment I add a single character array and a sprintf() with one array in it, I get the "cannot open Zorro.txt" error again.
Original post:
I built this code up piecemeal, adding each array one at a time, integrating each array into sprintf(), and everything was working just fine until I added the last two header titles.
The SD card works (Arduino's SD examples "Datalogger" works, and "Read/Write" works... meaning I can open a test.txt file and write Testing 1,2,3 to it).
I've tried:
- commenting out arrays and removing them from sprintf
- replacing header strings with simple text
- increasing hypstr buffer size to 256
- changing text file names, moving original text file into another folder (short of erasing the SD card, I don't feel this is necessary)
- disconnecting sensor, running code (proper error message works) ,reconnecting sensor
Up to the last moment, I was able to write milliseconds + seven arrays of data... and then everything broke:
145596,25.90,78.62,18841,1517.53,4978.46,1570.41,5152.56
147802,25.90,78.62,18836,1518.11,4980.36,1571.18,5154.47
150006,25.90,78.62,18839,1517.82,4980.04,1570.79,5153.21
152211,25.90,78.62,18841,1517.43,4976.87,1570.60,5153.21
Any advice would be fantastic, I'm a noob, so I really can't tell what went wrong. Hardware/wires seem to work (replaced the cs = 4 wire just in case, but again, simple codes do write to the SD card).
/* C
SD Card Template to use for testing
REMEMBER: Pin 10 must be set as an output and cannot be used for anything!!! (SD card code stuff)
SD card setup
** MOSI (DI) - pin 11
** MISO (DO) - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
*/
//Libraries
#include <SPI.h>
#include <SD.h>
#include <Adafruit_BMP085.h>
//BMP Definitions
int press; //int value is from -32768 to 32767
Adafruit_BMP085 bmp;
uint32_t timeStamp = 0;
//LED <---note to arduino forum, not used just yet
const int ledPin = 9;
// DO NOT USE PIN 10
//Document Info
String header1 = ("Begin BMP Testing"); //update as neededβtest done
String header2 = ("Time,TempC,TempF,Pressure(Pa),Alt(m,1013.25mb),Alt(ft,1013.25mb),rAlt(m,1019.9mb),rAlt(ft,1019.9mb)");
//SD Card
File dataFile; //name of file object
const int cS = 4; //SD card write
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(ledPin, OUTPUT);
pinMode(10, OUTPUT); //Required for SD card module, do not use
//SD CARD
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(cS)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1)
;
}
Serial.println("card initialized.");
//Print initial startup
dataFile = SD.open("zorro.txt", FILE_WRITE);
if (dataFile) {
dataFile.println("");
dataFile.println(header1); //headers
dataFile.println(header2);
dataFile.close();
// debug
Serial.println("");
Serial.println(header1);
Serial.println(header2);
}
//Start BMP
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}
}
void loop() {
timeStamp = millis(); // this declaration must be in void loop
// SD CARD
dataFile = SD.open("zorro.txt", FILE_WRITE);
if (dataFile) {
dataFile.print(timeStamp);
dataFile.print(",");
//debug
Serial.print(timeStamp);
Serial.print(",");
//BMP Sensor
//tempC
char tempCStr[7];
float tempC = bmp.readTemperature();
dtostrf(tempC, 1, 2, tempCStr);
//tempF
char tempFStr[7];
float tempF = bmp.readTemperature() * (9.0 / 5.0) + 32;
dtostrf(tempF, 1, 2, tempFStr);
//pressure
press = bmp.readPressure();
//alt (m) β 101325 Pa
char altMStr[12];
float altM = bmp.readAltitude();
dtostrf(altM, 1, 2, altMStr);
//alt (ft) β 101325 Pa
char altFtStr[12];
float altFt = bmp.readAltitude() * 3.28084;
dtostrf(altFt, 1, 2, altFtStr);
//real alt (m) β 101500 Pa
char raltMStr[12];
float raltM = bmp.readAltitude(101990);
dtostrf(raltM, 1, 2, raltMStr);
//real alt (ft) β 101300 Pa
char raltFtStr[12];
float raltFt = bmp.readAltitude(101990) * 3.28084;
dtostrf(raltFt, 1, 2, raltFtStr);
char hypstr[256];
sprintf(hypstr, "%s,%s,%d,%s,%s,%s,%s", tempCStr, tempFStr, press, altMStr, altFtStr, raltMStr, raltFtStr);
dataFile.println(hypstr);
Serial.println(hypstr);
dataFile.close();
} else {
Serial.println("error opening zorro.txt");
}
//END SD card write
delay(2000);
}