I have a soil moisture sensor that outputs the data into a excel sheet through a sd card. The problem that occurs is that it only prints out the data once and than doesn't do anything. It works fine with a delay of 5000.
#include <stdio.h>
#include <SD.h>
#include <time.h>
File MyFile;
String fileName = "SIX.csv";
int value = 0;
int sensor = A2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {};
initializeCard();
if (SD.exists(fileName))
{
SD.remove(fileName);
}
writeHeader();
}
void initializeCard()
{
Serial.println("Beginning initialization of SD Card: ");
if (!SD.begin(10))
{
Serial.print("failed");
while (1);
}
Serial.println(" successfull");
}
void writeHeader()
{
MyFile = SD.open(fileName, FILE_WRITE);
if (MyFile)
{
Serial.println("----------------------\n");
Serial.println( + fileName + ": ");
MyFile.println("Time, Moisture Value");
MyFile.close();
}
else
{
Serial.println("error opening" + fileName);
}
}
void writeData()
{
MyFile = SD.open(fileName, FILE_WRITE);
if (MyFile)
{
MyFile.print((millis() / 1000) / 60) / 60;
MyFile.print(",");
MyFile.println(value);
MyFile.close();
}
else
{
Serial.println("error opening " + fileName);
}
}
void loop() {
// put your main code here, to run repeatedly:
value = analogRead(sensor);
Serial.print("Moisture value: ");
Serial.println(value);
writeData();
delay(60UL * 60UL * 1000UL);
}