So I think I'm declaring a variable incorrectly (or something else completely wrong) in my sketch which is a mix of several sketches.
All I want to record to the SD car shield from the MPL3115A2 is temperature (SCL PIN 5, SDA PIN 4).
The serial output is correct. Between 23.8 *C and 24 *C.
But when I open the log file what I see is 25 and 24 until the time I stop the logging. No decimal or anything (I only care of 1 decimal point). It actually doesn't seem be rounding up either. Just a series of 25 and 24.
Thanking anyone with the patience to review the sketch!
The sketch follows (I have done away with comments to shorten it):
#include <SD.h>
#include <Wire.h>
#include <Adafruit_MPL3115A2.h>
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
const int chipSelect = 10;
float dataString = 0.0;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
delay(2000);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
delay(2000);
if (SD.exists("datalog.txt"))//if the datalog.txt file is already on the disk
{
SD.remove("datalog.txt");//delete it. This prevents that the data is appended to an already existing file.
}
}
void loop()
{ if (! baro.begin()) {
Serial.println("Couldnt find sensor");
return;
}
{
// make a string for assembling the data to log:
String dataString = "";
{
int analogPin = 5;
int sensor = analogRead(analogPin);
dataString += String(sensor);
{
dataString += ",";
}
}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
float tempC = baro.getTemperature();
Serial.print(tempC,1); Serial.println("*C");
//Serial.println(dataString);
delay(5000);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
}