What I'd like to do is reading temperature values from a sensor and write it to a file.
Here is my program so far.
#include <Arduino.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
const int chipSelect = 10;
const int sensorPin = 0;
RTC_DS1307 RTC;
void setup()
{
Serial.begin(57600);
Wire.begin();
RTC.begin();
if (!RTC.isrunning()) {
Serial.println("RTC is not running!");
// set RTC
RTC.adjust(DateTime(__DATE__, __TIME__));
}
while (!Serial) {
;
}
Serial.print("\ninitializing SD card...");
pinMode(10, OUTPUT);
//see if the card is present and can be initialized:
if(!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized");
}
void loop()
{
DateTime now = RTC.now();
// print date and time
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(' ');
String dataString = "";
dataString += String(now.day());
dataString += '/';
dataString += String(now.month());
dataString += '/';
dataString += String(now.year());
dataString += ' ';
dataString += String(now.hour());
dataString += ':';
dataString += String(now.minute());
dataString += ':';
dataString += String(now.second());
dataString += ' ';
float temperature = analogRead(sensorPin);
temperature = (5 * temperature * 100.0) / 1024.0;
char temp[10];
// read sensor value and append to dataString
dtostrf(temperature,1,2,temp);
dataString += String(temp);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
} else {
// back up
/*
What if there is a power cut?
*/
//
//setup();
Serial.println("error opening datalog.txt");
}
delay(5000);
}
Well, the program works fine if everything goes well.
But what if there is a power cut. To test the power cut I simply plug out USB cable. After that when I plug in the program doesn't continue to record data anymore.
If I reset the Arduino manually it works again as expected.
What should I do, so that when there is a power failure I do not need to reset Arduino manually.
akuz:
Anyway I think, I found a solution. I used software method to reset Arduino.
But there was a problem with this method. It was continuously resetting Arduino in half second. So that it was recording values in half seconds instead of 5 seconds.
WDTTO_8S is a constant that means threshold interval of 8 seconds. (It is defined inside header file). If Watchdog receive no "pat" signal for this period, it will "bite" back - reboot the MCU.
I think what I was doing was exactly the opposite
Here is the corrected program.
#include <Arduino.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <avr/wdt.h>
const int chipSelect = 10;
const int sensorPin = 0;
RTC_DS1307 RTC;
void setup()
{
wdt_enable(WDTO_500MS);
Serial.begin(57600);
Wire.begin();
RTC.begin();
if (!RTC.isrunning()) {
Serial.println("RTC is not running!");
// set RTC
RTC.adjust(DateTime(__DATE__, __TIME__));
}
while (!Serial) {
;
}
Serial.print("\ninitializing SD card...");
pinMode(10, OUTPUT);
//see if the card is present and can be initialized:
if(!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized");
}
void loop()
{
DateTime now = RTC.now();
// print date and time
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(' ');
String dataString = "";
dataString += String(now.day());
dataString += '/';
dataString += String(now.month());
dataString += '/';
dataString += String(now.year());
dataString += ' ';
dataString += String(now.hour());
dataString += ':';
dataString += String(now.minute());
dataString += ':';
dataString += String(now.second());
dataString += ' ';
float temperature = analogRead(sensorPin);
temperature = (5 * temperature * 100.0) / 1024.0;
char temp[10];
// read sensor value and append to dataString
dtostrf(temperature,1,2,temp);
dataString += String(temp);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
// tell watchdog that everything is ok
wdt_reset();
} else {
Serial.println("error opening datalog.txt");
dataFile.close();
/*
What if there is a power cut?
*/
for (;;) {
}
}
// tell watchdog that everything is ok
wdt_reset();
delay(5000);
}