I'm trying to take the temperature using the DS18B20 sensor, and then record that along with the time onto a micro SD card. However the code will not compile and I keep getting a range of different error codes. The code seemed pretty good to me, but I'm a beginner so I guess not. Any ideas or anyone have code that works for these sensors?
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
RTC_DS3231 rtc;
File myFile;
File myFile;
void setup() {
Serial.begin(9600);
delay(3000);
sensors.begin();
while (!Serial) {
;
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
}
myFile = SD.open("today.txt", FILE_WRITE);
if (myFile) {
Serial.print("Writing to today.txt...");
myFile.print("Year/Month/Day");
myFile.print(",");
myFile.print("Hour");
myFile.print(':');
myFile.print("Minute");
myFile.print(",");
myFile.print("Temperature")
myFile.close();
}
}
void loop() {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), 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.println("done");
Serial.print(" Requesting temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");
Serial.print("Temperature is: ");
Serial.println(sensors.getTempCByIndex(0));
myFile = SD.open("today.txt", FILE_WRITE);
if (myFile) {
Serial.print("Writing to today.txt...");
myFile.print(now.year(), DEC);
myFile.print('/');
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.day(), DEC);
myFile.print(",");
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(",");
myFile.println(sensors.getTempCByIndex(0));
myFile.close();}
delay(3000);
}