I altered the data logging example code for SD cards trying to log information from some temp sensors and an RTC. The temp sensors read fine and the RTC as well but I can't seem to get the data onto the sd card. The program seems to be working completely according to my serial monitor but when I pull the sd card out I find no changes to what was already on it.
Code Below:
#include <Wire.h>
#include <DS3231.h>
DS3231 rtc(SDA, SCL);
float temp_read1()
{
Wire.requestFrom(0x48, 2);
byte h = Wire.read();
byte l = Wire.read();
return ((h << 3) | (l >> 5)) * 0.125;
}
float temp_read2()
{
Wire.requestFrom(0x49,2);
byte a = Wire.read();
byte b = Wire.read();
return ((a << 3) | (b >> 5)) * 0.125;
}
float temp_read3()
{
Wire.requestFrom(0x4B,2);
byte d = Wire.read();
byte c = Wire.read();
return ((d << 3) | (c >> 5)) * 0.125;
}
//====================================================
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
Wire.begin();
rtc.begin();
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
}
void loop() {
SD.begin(4);
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
dataString += String(" Temp1: ");
dataString += String(temp_read1());
dataString += String(" Temp2: ");
dataString += String(temp_read2());
dataString += String(" Temp3: ");
dataString += String(temp_read3());
dataString += String(" Time: ");
dataString += String(rtc.getTimeStr());
dataString += String(" Date: ");
dataString += String(rtc.getDateStr());
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("temp_log.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
delay(1000);
}
attempt_3.ino (2.51 KB)