Hello everyone,
I'm new on arduino's and I'm using an arduino one to read and writte moisture content with sensors. My code works to read and writte in the SD card, but after a few time recording the data its stop. I being trying modifying the code and change the SD card and SD card reader, but without results.
Ps. I'm also using a RTC clock to have the time of the reading cause the idea is using the arduino with battery and not connected to the computer (it works perfectly reading in the computer).
I leave my code here to see if you can help me with this
Thanks !!
//first calibrate dry sensor value and pure water. We might want to consider calibrating under extremely dry conditions if we want more accuracy in drier values.
const int dry = 560; // value for dry sensor
const int wet = 305; // value for wet sensor
const int chipSelect = 4;
#include "RTClib.h"
#include <SPI.h>
#include <SD.h>
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Wednesday", "Thursday", "Friday", "Saturday"};
int moist1;
// SD CARD READER
File myFile;
void setup() {
Serial.begin(9600); //com enable
delay (1000);
if (!rtc.begin ()) {
Serial.println ("couldn't find RTC");
Serial.flush();
while (1) delay(1000);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
//wait for serial port to connect.
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.");
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop() {
//make a string for assembling the data to log:
String dataString = "";
DateTime now = rtc.now();
Serial.print (now.year(), DEC);
Serial.print ("/");
Serial.print (now.month(), DEC);
Serial.print ("(");
Serial.print (daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print (")");
Serial.print (now.hour(), DEC);
Serial.print (":");
Serial.print (now.minute(), DEC);
Serial.print (":");
Serial.print (now.second(), DEC);
Serial.print (",");
{
int sensorValue1 = analogRead(A1);
int moist1 = analogRead(A1);
moist1 = analogRead(A1); //take reading 1
int percentMoisture1 = map(sensorValue1, wet, dry, 100, 0);
Serial.print(moist1);
Serial.print(",");
Serial.print(percentMoisture1);
Serial.print(",");
Serial.println("%");
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
myFile.print (now.year(), DEC);
myFile.print ("/");
myFile.print (now.month(), DEC);
myFile.print ("(");
myFile.print (daysOfTheWeek[now.dayOfTheWeek()]);
myFile.print (")");
myFile.print (now.hour(), DEC);
myFile.print (":");
myFile.print (now.minute(), DEC);
myFile.print (":");
myFile.print (now.second(), DEC);
myFile.print (",");
myFile.print(moist1);
myFile.print(",");
myFile.print(percentMoisture1);
myFile.println("%");
myFile.close(); // close the file
}
// if the file didn't open, print an error:
else {
Serial.println("error opening test.txt");
}
}
delay(1000);
}