Temperature and time data store with SD card &RTC module with Arduino mega 2560

I have made one simple program related to my project,that is when ever ON the toggle switch (non returnable Switch), SD card will store the temperature and date and time.

component used:-

1] Arduino MEGA 2560

2] SD card module

3] RTC Module

4] Toggle switch.

I have facing the problem is that as per my code SD card store the data continuously while switch in ON position,but i want store the data only one time if switch is in ON (HIGH) position.

Here is my code:-

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SD.h>
#include <SPI.h>
#define ONE_WIRE_BUS 22
#include <DS3231.h>

int CS_pin = 53; // Pin 10 on Arduino MEGA
OneWire oneWire(ONE_WIRE_BUS);
DS3231  rtc(SDA, SCL);
DallasTemperature sensors(&oneWire);
const int toggle = 8;
float Celcius=0;  

File sdcard_file;
void setup() {
  Serial.begin(9600);
  pinMode(toggle,INPUT_PULLUP);
  pinMode(CS_pin, OUTPUT);
  rtc.begin(); 
  sensors.begin();
  // SD Card Initialization
  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
  
  Serial.print("Date  ");   
  Serial.print("      ");
  Serial.print("   Time  ");
  Serial.print("     ");
  Serial.print("   Temp   ");
  Serial.println("     ");
  sdcard_file = SD.open("data.txt", FILE_WRITE);
  if (sdcard_file) { 
    sdcard_file.print("Date  ");   
    sdcard_file.print("      ");
    sdcard_file.print("   Time  ");
    sdcard_file.print("     ");
    sdcard_file.print("   Temp   ");
    sdcard_file.println("     ");
    sdcard_file.close(); // close the file
  }
  // if the file didn't open, print an error:
  else {
    Serial.println("error opening test.txt");
  }
}

void loop() {
  if (digitalRead(toggle) == HIGH){
  
  sensors.requestTemperatures(); 
  Celcius=sensors.getTempCByIndex(0);
  Serial.print(rtc.getDateStr());
  Serial.print("     ");
  Serial.print(rtc.getTimeStr());
  Serial.print("      ");
  Serial.println(Celcius);
 
  sdcard_file = SD.open("data.txt", FILE_WRITE);
  if (sdcard_file) {    
    sdcard_file.print(rtc.getDateStr());
    sdcard_file.print("     ");   
    sdcard_file.print(rtc.getTimeStr());
    sdcard_file.print("     ");
    sdcard_file.println(Celcius);
    sdcard_file.close(); // close the file
  }}
  // if the file didn't open, print an error:
  else {
    Serial.println("error opening test.txt");
  }
  delay(3000);
}

Please advice me how can solve the problem.

Have a look at the State Change Detection example (File->Examlpes->02. Digital->StateChangeDetection).
You want to do something when the switch changes state (OFF->ON), not do something just because the switch is currently ON.

1. Use a push button which will return back after making a connection.

2. Connect one sibe of the push button at 5V and other side with DPin-8.

3. Connect a 2.2k pull-down resistor between DPin-8 and GND.

4. Use following code to set the direction of DPin-8.

pinMode(toggle, INPUT);

5. Check that in the loop() function, your some code lines are like the following:

 if (sdcard_file)
    {
      sdcard_file.print(rtc.getDateStr());
      sdcard_file.print("     ");
      sdcard_file.print(rtc.getTimeStr());
      sdcard_file.print("     ");
      sdcard_file.println(Celcius);
      sdcard_file.close(); // close the file
    }
    // if the file didn't open, print an error:
    else
    {
      Serial.println("error opening test.txt");
    }

GolamMostafa:
1. Use a push button which will return back after making a connection.

2. Connect one sibe of the push button at 5V and other side with DPin-8.

[

Sir i can't use push button in place of toggle switch ...bcs switch uded in my project like that only (pressure switch).

Please advice me how can solve the problem.

Use a global boolean control variable initialized as false

boolean beenThereDoneThat = false;

The condition the SD card write with

if (digitalRead(toggle) == HIGH && beenThereDoneThat == false)
   {
     beenThereDoneThat = true;
     //rest of code to doe once with toggle switch thrown
    }

Anytime you want to re-enable the action, set the boolean contol back to false.

The following short tutorial explains the 'state change' strategy which could be used to sense the active condition of a switch.

1. OP's switch is connected with DPin-8 with MEGA's internal pull-up enabled. Therefore, the connection of the switch must be of the following type (Fig-1).
megaIntP.png
Figure-1:

2. When S1 remains closed, LOW is at DPin-8; L is OFF.

3. When S1 is opened, HIGH is at DPin-8, L is ON.

4. To make L OFF, close S1 and then open it.

In the above events, the flag beenThereDoneThat plays the key role.

Sketch:

bool beenThereDoneThat = true;

void setup()
{
 Serial.begin(9600);
 pinMode(8, INPUT_PULLUP);

 pinMode(13, OUTPUT);

 digitalWrite(13, LOW);
}

void loop()
{
 if ((digitalRead(8)) && (beenThereDoneThat))
 {
   digitalWrite(13, !digitalRead(13));
   beenThereDoneThat = false;
 }
 else
 {
   if (digitalRead(8) == LOW)
   {
     beenThereDoneThat = true;
   }
 }
 delay(3000);
}

The OP can easily incorporate the above statechange strategy in his Data Logger Project.

megaIntP.png

cattledog:
Use a global boolean control variable initialized as false

boolean beenThereDoneThat = false;

The condition the SD card write with

if (digitalRead(toggle) == HIGH && beenThereDoneThat == false)

{
    beenThereDoneThat = true;
    //rest of code to doe once with toggle switch thrown
   }




Anytime you want to re-enable the action, set the boolean contol back to false.

Its not working.

Here is my code,

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SD.h>
#include <SPI.h>
#define ONE_WIRE_BUS A0
#include <DS3231.h>

int CS_pin = 53; // Pin 10 on Arduino MEGA
OneWire oneWire(ONE_WIRE_BUS);
DS3231  rtc(SDA, SCL);
DallasTemperature sensors(&oneWire);
const int toggle = 8;
float Celcius=0;  
//int led = 22;
bool Button_state = false;
File sdcard_file;
void setup() {
  Serial.begin(9600);
  pinMode(toggle,INPUT_PULLUP);
  pinMode(CS_pin, OUTPUT);
 // pinMode(led, OUTPUT);
  rtc.begin(); 
  sensors.begin();
  // SD Card Initialization
  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
  
  Serial.print("Date  ");   
  Serial.print("      ");
  Serial.print("   Time  ");
  Serial.print("     ");
  Serial.print("   Temp   ");
  Serial.println("     ");
  sdcard_file = SD.open("data.txt", FILE_WRITE);
  if (sdcard_file) { 
    sdcard_file.print("Date  ");   
    sdcard_file.print("      ");
    sdcard_file.print("   Time  ");
    sdcard_file.print("     ");
    sdcard_file.print("   Temp   ");
    sdcard_file.println("     ");
    sdcard_file.close(); // close the file
  }
  // if the file didn't open, print an error:
  else {
    Serial.println("error opening test.txt");
  }
}

void loop() {
  
  if (digitalRead(toggle) == HIGH && Button_state == false ){
  Button_state = true;
  sensors.requestTemperatures(); 
  Celcius=sensors.getTempCByIndex(0);
  Serial.print(rtc.getDateStr());
  Serial.print("     ");
  Serial.print(rtc.getTimeStr());
  Serial.print("      ");
  Serial.println(Celcius);
 delay(1000);
  sdcard_file = SD.open("data.txt", FILE_WRITE);
  if (sdcard_file) {    
    sdcard_file.print(rtc.getDateStr());
    sdcard_file.print("     ");   
    sdcard_file.print(rtc.getTimeStr());
    sdcard_file.print("     ");
    sdcard_file.println(Celcius);
    sdcard_file.close(); // close the file
  }
 
   Button_state = true;
  }
  // if the file didn't open, print an error:
  else {

    if (digitalRead(toggle) == LOW)
   {
    
     Button_state = false;
    Serial.println("error opening test.txt");
  }
  }
  delay(1000);
}


please correct me. if i have made  any mistakes.

GolamMostafa:
4. To make L OFF, close S1 and then open it.

thanks,

in my project toggle type pressure actuated switch is used.SD card will record the data only one time at the time of switch is operated(become HIGH).

sreekanthmp:
Its not working.

Here is my code,

#include <OneWire.h>

#include <DallasTemperature.h>
#include <SD.h>
#include <SPI.h>
#define ONE_WIRE_BUS A0
#include <DS3231.h>

int CS_pin = 53; // Pin 10 on Arduino MEGA
OneWire oneWire(ONE_WIRE_BUS);
DS3231  rtc(SDA, SCL);
DallasTemperature sensors(&oneWire);
const int toggle = 8;
float Celcius=0; 
//int led = 22;
bool Button_state = false;
File sdcard_file;
void setup() {
  Serial.begin(9600);
  pinMode(toggle,INPUT_PULLUP);
  pinMode(CS_pin, OUTPUT);
// pinMode(led, OUTPUT);
  rtc.begin();
  sensors.begin();
  // SD Card Initialization
  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
 
  Serial.print("Date  "); 
  Serial.print("      ");
  Serial.print("  Time  ");
  Serial.print("    ");
  Serial.print("  Temp  ");
  Serial.println("    ");
  sdcard_file = SD.open("data.txt", FILE_WRITE);
  if (sdcard_file) {
    sdcard_file.print("Date  "); 
    sdcard_file.print("      ");
    sdcard_file.print("  Time  ");
    sdcard_file.print("    ");
    sdcard_file.print("  Temp  ");
    sdcard_file.println("    ");
    sdcard_file.close(); // close the file
  }
  // if the file didn't open, print an error:
  else {
    Serial.println("error opening test.txt");
  }
}

void loop() {
 
  if (digitalRead(toggle) == HIGH && Button_state == false ){
  Button_state = true;
  sensors.requestTemperatures();
  Celcius=sensors.getTempCByIndex(0);
  Serial.print(rtc.getDateStr());
  Serial.print("    ");
  Serial.print(rtc.getTimeStr());
  Serial.print("      ");
  Serial.println(Celcius);
delay(1000);
  sdcard_file = SD.open("data.txt", FILE_WRITE);
  if (sdcard_file) {   
    sdcard_file.print(rtc.getDateStr());
    sdcard_file.print("    "); 
    sdcard_file.print(rtc.getTimeStr());
    sdcard_file.print("    ");
    sdcard_file.println(Celcius);
    sdcard_file.close(); // close the file
  }

Button_state = true;
  }
  // if the file didn't open, print an error:
  else {

if (digitalRead(toggle) == LOW)
  {
   
    Button_state = false;
    Serial.println("error opening test.txt");
  }
  }
  delay(1000);
}

please correct me. if i have made  any mistakes.

Please do a couple things to help us help you.

Step 1. Open your code in the Arduino IDE and press Control-T. That will format your code for you and line the blocks up so it's easier to read and see what goes with what.

Step 2. Describe what you mean by "not working". Is it still logging continuously? Is it not logging at all? Be descriptive. You're asking someone who can't see it or touch it for help.

Its not working.

Please explain more. What does the code do that you do not want it to do? What does the code not do that you want it to do?

How is the toggle switch wired. Have you confirmed that the digitalRead() of the switch is as expected? Please explain how you use the toggle switch?

If you comment out this section which resets the control boolean, do you get the one SD write you want.

else 
  {

    if (digitalRead(toggle) == LOW)
    {
      Button_state = false;
      Serial.println("reset button_state back to false");
    }
  }

cattledog:
Please explain more. What does the code do that you do not want it to do? What does the code not do that you want it to do?

How is the toggle switch wired. Have you confirmed that the digitalRead() of the switch is as expected? Please explain how you use the toggle switch?

If you comment out this section which resets the control boolean, do you get the one SD write you want.

else 

{

if (digitalRead(toggle) == LOW)
   {
     Button_state = false;
     Serial.println("reset button_state back to false");
   }
 }





Thanks !!!
its working.Previously there was a loose contact in my circuit .After solving that issues, its working good.