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