Delay for Soil Moisture Sensor

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);
}

Where is the code for that function?

This is why we ask that you post all the code.

Just posted the full code.

Doesn't do anything, or does something once per hour?

I kept it on for about 10 hours and it only showed the data for the first hour.

I'm not sure if this is the cause, but it's certainly wrong.

Also it looks like you create a file and then remove it if it exists.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.