//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);
if (Serial.read() == 'p') PrintSD();
}
void PrintSD(){
String s = "-> ";
myFile = SD.open("test.txt");
if (myFile) {
while (myFile.available()) {
char c = myFile.read();
if (c != 10) s += c;
else {
Serial.println(s);
s = "-> ";
}
}
myFile.close();
}
}