SD card module and multiple water level sensor: HOW to save value

hi i am a newbie in arduino i just want to know/ have an advise about our project . We have x3 water level sensor and i am having trouble storing data in micro sd card and functioning the sensors. here is my code

#include <SD.h>
#include <SPI.h>
#include <DS3231.h>
DS3231 rtc(SDA, SCL);

File myFile;
String fileName = "water.csv ";

int pinCS = 53; // //the input pin (for a SD card)
int waterval1 = 0;
int waterval2 = 0;
int waterval3 = 0;
int sensor1 = A0; //the input pin (for sensor1)
int sensor2 = A1; //the input pin (for sensor2)
int sensor3 = A2; //the input pin (for sensor3)
int yellow = 3; //the input pin (for LED yellow)
int red = 4; //the input pin (for LED red)
int green = 2; //the input pin (for LED green)
int alarm = 7; //the input pin (for a buzzer)
int button = 5; //the input pin (for a pushbutton)

void setup() {

Serial.begin(9600);

pinMode(pinCS,OUTPUT);
pinMode(button,INPUT); // declare pushbutton as input
pinMode (red,OUTPUT); // LED red
pinMode (yellow,OUTPUT); // LED yellow
pinMode (green,OUTPUT); // LED green
pinMode(alarm,OUTPUT); // Buzzer

digitalWrite(alarm, LOW);
digitalWrite(red, LOW);
digitalWrite(yellow, LOW);
digitalWrite(green, LOW);

// SD Card Initialization
if (SD.begin())
{
Serial.println(“SD card is ready to use.”);
} else
{
Serial.println(“SD card initialization failed”);
return;
}
rtc.begin();
}

void loop() {
waterval1= analogRead(A0); //read water level value for sensor 1
waterval2= analogRead(A1); //read water level value for sensor 2
waterval3= analogRead(A2); //read water level value for sensor 3
Serial.print(waterval1);
Serial.print(waterval2);
Serial.print(waterval3);
//***********************************************************

button =digitalRead(5); //button

//***********************************************************

digitalWrite(alarm, LOW); //buzzer

//***********************************************************

//For Sensor 1
if ((waterval1 > 0) && (waterval1 <= 400))
{
digitalWrite (red,LOW);
digitalWrite(yellow,LOW);
digitalWrite(green,LOW);
digitalWrite(alarm,LOW);

}
else if (waterval1 > 400) {
Serial.println(“Water Level 1 is 3.2cm”);
digitalWrite (red,LOW);
digitalWrite(yellow,LOW);
digitalWrite(green,HIGH);
digitalWrite(alarm,HIGH);
tone(alarm, 10000, 300);
delay(3300);

}
// Send Day-of-Week
Serial.print(rtc.getDOWStr());
Serial.print(" ");

// Send date
Serial.print(rtc.getDateStr());
Serial.print(" – ");

// Send time
Serial.println(rtc.getTimeStr());
Serial.print(" ");

myFile = SD.open(“test.txt”, FILE_WRITE);
if (myFile) {
myFile.print(rtc.getTimeStr());
myFile.print(",");
myFile.print(rtc.getDOWStr());
myFile.print(" “);
myFile.print(rtc.getDateStr());
myFile.print(” – ");
myFile.println(waterval1);

myFile.close(); // close the file
}
// if the file didn’t open, print an error:
else {
Serial.println(“error opening test.txt”);
}
delay(5000);

//For Sensor 2
if ((waterval2 < 0) && (waterval2 <= 400)) {
Serial.println(“Water Level 2 is 6.4cm”);
digitalWrite (red,LOW);
digitalWrite(yellow,LOW);
digitalWrite(green,LOW);
digitalWrite(alarm,LOW);

}
else if (waterval2 > 400)
{
digitalWrite (red,LOW);
digitalWrite(yellow,HIGH);
digitalWrite(green,LOW);
digitalWrite(alarm,HIGH);
tone(alarm, 10000, 500);
delay(2500);

}
// Send Day-of-Week
Serial.print(rtc.getDOWStr());
Serial.print(" ");

// Send date
Serial.print(rtc.getDateStr());
Serial.print(" – ");

// Send time
Serial.println(rtc.getTimeStr());
Serial.print(" ");

myFile = SD.open(“test.txt”, FILE_WRITE);
if (myFile) {
myFile.print(rtc.getTimeStr());
myFile.print(",");
myFile.print(rtc.getDOWStr());
myFile.print(" “);
myFile.print(rtc.getDateStr());
myFile.print(” – ");
myFile.println(waterval2);

myFile.close(); // close the file
}
// if the file didn’t open, print an error:
else {
Serial.println(“error opening test.txt”);
}
delay(5000);

//For Sensor 3
if ((waterval3 < 0) && (waterval3 <= 400))
{
Serial.println(“Water Level 3 is 9.4cm”);
digitalWrite (red,LOW);
digitalWrite(yellow,LOW);
digitalWrite(green,LOW);
digitalWrite(alarm,LOW);

}
else if (waterval3 > 400) {
digitalWrite (red,HIGH);
digitalWrite(yellow,LOW);
digitalWrite(green,LOW);
digitalWrite(alarm,HIGH);
tone(alarm, 10000, 300);
delay(1300);

}
// Send Day-of-Week
Serial.print(rtc.getDOWStr());
Serial.print(" ");

// Send date
Serial.print(rtc.getDateStr());
Serial.print(" – ");

// Send time
Serial.println(rtc.getTimeStr());
Serial.print(" ");

myFile = SD.open(“test.txt”, FILE_WRITE);
if (myFile) {
myFile.print(rtc.getTimeStr());
myFile.print(",");
myFile.print(rtc.getDOWStr());
myFile.print(" “);
myFile.print(rtc.getDateStr());
myFile.print(” – ");
myFile.println(waterval3);

myFile.close(); // close the file
}
// if the file didn’t open, print an error:
else {
Serial.println(“error opening test.txt”);
}
delay(5000);
//For Button
if (button ==1)
{
digitalWrite(red, HIGH);
tone(alarm, 10000, 300);
delay(3300);
}
else
{
digitalWrite(red, LOW);
tone(alarm,0,0);
}

}

Read the first topics like "How to use this Forum", "How to attache code", "How to attache pictures, drawings".
What does "trouble" look like?
Does reading the water level sensors work?
For g-s sake, add comments to the code! Nobody will understand what green, yellow etc does. It's a birdnest, impossible to understand.

sorry like what I've said I'm just a newbie and there is a comment in my codes

If you are having trouble doing something, describe THAT trouble and what you have tried to correct it.

Paul

i dont know what happening but my sensors is not functioning here is the sample code…

void loop() {

waterval1= analogRead(A0); //read water level value for sensor 1
waterval2= analogRead(A1); //read water level value for sensor 2
waterval3= analogRead(A2); //read water level value for sensor 3

//***********************************************************

button =digitalRead(5); //button

//***********************************************************

digitalWrite(alarm, LOW); //buzzer

//***********************************************************

//For Sensor 1
  if ((waterval1 > 0) && (waterval1 <= 400)) 
  {
    digitalWrite (red,LOW);
    digitalWrite(yellow,LOW);
    digitalWrite(green,LOW);
    digitalWrite(alarm,LOW);

    }
    else if (waterval1 > 400) {
    Serial.println("Water Level 1 is 3.2cm");
    digitalWrite (red,LOW);
    digitalWrite(yellow,LOW);
    digitalWrite(green,HIGH);
    digitalWrite(alarm,HIGH);
    tone(alarm, 50000, 300);
    delay(5300);

    }
    
//For Sensor 2
      if ((waterval2 < 0) && (waterval2 <= 400)) {
    Serial.println("Water Level 2 is 6.4cm");
    digitalWrite (red,LOW);
    digitalWrite(yellow,LOW);
    digitalWrite(green,LOW);
    digitalWrite(alarm,LOW);

    }
     else if (waterval2 > 400)
     {
    digitalWrite (red,LOW);
    digitalWrite(yellow,HIGH);
    digitalWrite(green,LOW);
    digitalWrite(alarm,HIGH);
    tone(alarm, 50000, 500);
    delay(3500);

    }
    
//For Sensor 3
      if ((waterval3 < 0) && (waterval3 <= 400)) 
  {
    Serial.println("Water Level 3 is 9.4cm");
    digitalWrite (red,LOW);
    digitalWrite(yellow,LOW);
    digitalWrite(green,LOW);
    digitalWrite(alarm,LOW);

    }
    else if (waterval3 > 400) {
    digitalWrite (red,HIGH);
    digitalWrite(yellow,LOW);
    digitalWrite(green,LOW);
    digitalWrite(alarm,HIGH);
    tone(alarm, 50000, 300);
    delay(1300);

    }


}

@yoll3

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Here’s a few things I’d check…

int pinCS = 53; //the input pin (for a SD card)   <<-- No that the "chip select" pin. The I/O is through the SPI bus.
 

if (SD.begin())   <<--- Shouldn't this be..

if (SD.begin(pinCS))

int button = 5;    //the input pin (for a pushbutton)

You initialize button as a pin number. Then later you use it as a button value?

button = digitalRead(5); //button

Then have this..

// For Button
  if (button == 1)
  {

Why not lessen the confusion. and do it this way…

int button = 5;    //the input pin (for a pushbutton)  <<-- GO ahead and create the pin number as you did.

pinMode(button, INPUT);   // declare pushbutton as input <<-- GO ahead and do your setup call.


button = digitalRead(5); //button <<-- DELETE THIS BIT.



 if (digitalRead(button))   <<-- Then just do this in loop()
  {
    digitalWrite(red, HIGH);
    tone(alarm, 10000, 300);
    delay(3300);
  }
  else
  {
    digitalWrite(red, LOW);
    tone(alarm, 0, 0);
  }

-jim lee