SD Card Print Question

Hello

What I am trying to do is :

I have a water sensor and I want to collect data write it on the sd card, but only when level of the water changed

like 20%, 40% ... every time when when water level change to get time and date and the level

Here is the part of the loop

  if(digitalRead(OHP)  ==  LOW){
    Serial.println("WATER LEVEL at 100%");
    delay(1000);
    }
  else if(digitalRead(ETHP)  ==  LOW){
    Serial.println("WATER LEVEL at 80%");
    delay(1000);
    }

In order to detect when something changes you have to save the value then compare it with the next time you take a reading. If the reading has chanced by a significant amount then you can act on it

UKHeliBob:
In order to detect when something changes you have to save the value then compare it with the next time you take a reading. If the reading has chanced by a significant amount then you can act on it

Hello

I am still a new in C Do you have any example code so I can take a look at it.

Thank you

In the IDE: File->Examples->02.Digital->StateChangeDetection

johnwasser:
In the IDE: File->Examples->02.Digital->StateChangeDetection

Thank you. Now I know what to look for :slight_smile:

OK, now it is working, but …

     myFile = SD.open("data.txt", FILE_WRITE);
        if (myFile) {
          myFile.print(rtc.getDateStr());
          myFile.print(",");
          myFile.print(rtc.getTimeStr());
          myFile.print(",");
          myFile.println(digitalRead(levelState));
          myFile.close(); // close the file
        }
        else {
          Serial.println("error opening data.txt");
        }

now it is printing on the sd card in any water level state

02.04.2020,12:40:35,1
02.04.2020,12:40:39,1
02.04.2020,12:40:40,1
02.04.2020,12:40:41,1

How can I replace “1” with the current state that is on

Nothing in the code snippet that you posted tests for a change of state.
We need to see the whole program

UKHeliBob:
Nothing in the code snippet that you posted tests for a change of state.
We need to see the whole program

Here is the code

#include <SD.h>
#include <SPI.h>
#include <DS3231.h> //Library by Henning Karlsen


//Constants Won't Change.
const int  OHP = 9; //100%
const int  ETHP = 8;  //80%
const int  STHP = 7;  //60%
const int  FTHP = 6;  //40%
const int  TTHP = 5;  //20%
const int  LLP = 4; //0%

//Variables Will Change:
int levelState = 0;
int lastLevelState = 0;


// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);

//SD Card Pin -- Arduino Pro Mini Pin = D10
int pinCS = 10;
File myFile;




void setup() {

  // opens serial port, sets data rate to 115200 bps
  Serial.begin(115200);
  // SD Card Initialization
  pinMode(pinCS, OUTPUT);

  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
  // Initialize the rtc object
  rtc.begin();


  //Water Level Sensor
  pinMode(OHP, INPUT);
  pinMode(ETHP, INPUT);
  pinMode(STHP, INPUT);
  pinMode(FTHP, INPUT);
  pinMode(TTHP, INPUT);
  pinMode(LLP, INPUT);

}


void loop() {
  //Read Level State
  levelState = digitalRead(OHP && ETHP && STHP && FTHP && TTHP && LLP);

  //Serial Print
  Serial.print(rtc.getDateStr());
  Serial.print(",");
  Serial.print(rtc.getTimeStr());
  Serial.print(",");
  Serial.println(int(rtc.getTemp()));
  Serial.print(",");


  if (levelState != lastLevelState) {
    if (digitalRead(OHP)  ==  LOW) {
      Serial.println("Water Level at 100%");  // prints a label
      delay(1000);
    }
    else if (digitalRead(ETHP)  ==  LOW) {
      Serial.println("Water Level at 80%");  // prints a label
      delay(1000);
    }
    else if (digitalRead(STHP)  ==  LOW) {
      Serial.println("Water Level at 60%");  // prints a label
      delay(1000);
    }
    else if (digitalRead(FTHP)  ==  LOW) {
      Serial.println("Water Level at 40%");  // prints a label
      delay(1000);
    }
    else if (digitalRead(TTHP)  ==  LOW) {
      Serial.println("Water Level at 20%");  // prints a label
      delay(1000);
    }
    else if (digitalRead(LLP)  ==  LOW) {
     Serial.println("Water Level at 0%");  // prints a label
      delay(1000);
    }
    else {
      Serial.println("Error 101");  // prints an error
      delay(1000);
    }
      myFile = SD.open("data.txt", FILE_WRITE);
          if (myFile) {
            myFile.print(rtc.getDateStr());
            myFile.print(",");
            myFile.print(rtc.getTimeStr());
            myFile.print(",");
            myFile.println(digitalRead(levelState));
            myFile.close(); // close the file
          }
          else {
            Serial.println("error opening data.txt");
          }
  }
  else {
    }
}
  levelState = digitalRead(OHP && ETHP && STHP && FTHP && TTHP && LLP);

That won't work. What did you want to calculate? How many of the pins read as HIGH? That would be:

  levelState = digitalRead(OHP) + digitalRead(ETHP) + digitalRead(STHP) + 
    digitalRead(FTHP) + digitalRead(TTHP) + digitalRead( LLP);
            myFile.println(digitalRead(levelState));

The variable 'levelState' is NOT a pin number so that won't work.

Look like you should start loop() with:

  if (digitalRead(OHP)  ==  LOW)
  {
    levelState = 100;
  }
  else if (digitalRead(ETHP)  ==  LOW) 
  {
    levelState = 80;
  }
  else if (digitalRead(STHP)  ==  LOW) 
  {
    levelState = 60;
  }
  else if (digitalRead(FTHP)  ==  LOW) 
  {
    levelState = 40;
  }
  else if (digitalRead(TTHP)  ==  LOW) 
  {
    levelState = 20;
  }
  else if (digitalRead(LLP)  ==  LOW) 
  {
    levelState = 0;
  }
  else {
    levelState = -1
  }

Then you can compare that to the previous state.

johnwasser:

  levelState = digitalRead(OHP && ETHP && STHP && FTHP && TTHP && LLP);

That won't work. What did you want to calculate? How many of the pins read as HIGH? That would be:

  levelState = digitalRead(OHP) + digitalRead(ETHP) + digitalRead(STHP) + 

digitalRead(FTHP) + digitalRead(TTHP) + digitalRead( LLP);

Witch pin reads as LOW

and then get current date and time from DS3231 and write it on the SD Card once
if and when status change write the next pin that read as LOW

This way I can collect data in this pattern

year.month.day,hour.min,waterLevel

and imported to excel converted into graph

Here is the schematic of the sensor is very simple

One thing I don’t understand

What arduino consider as “LOW” in the voltage level

because I used multi meter to get the voltage going to the arduino D9 pin says 5.02v
when water hit the 1st level i.e D9 pin reads 0.02mv

As I am new this might seem stupid question, but for me is one step forward. :slight_smile:

anyone