I am switching a pump periodically by a relay with the help of a RTC ds1307 module using a sd card module as a datalogger.
When I upload the code it works fine, but after sometime it stops writing to it.
I also have a lcd attached to it showing the time and status of sd card(writing to it or not).
The thing I have noticed is that when the pump is switched by the relay, sd card cant be accesed by the arduino. And sometines the time also jumps ahead 10 minutes.
To solve it I have to
reupload the code,
or open the serial monitor port ,
or switch off the power of arduino and then switch it on back again.
Doing any of the above steps corrects the time and arduino starts writing to the sd card.
Where lies the problem?
Thanks for your reply.
Can you pleae show me how to use C null terminated string?
Do you want me to create strings like charc[100]; and add data to the end of it using strcat(a,b)?
The sd card usually stops when the pump is switched by the relay.
Can you pleae show me how to use C null terminated string?
OK! Please, follow these steps carefully to learn 'character string' manipulation. We will use few C functions and SD Card for this purpose.
1. Connect the SD Card only with the UNO as per following diagram.
Fig-1:
2. Open IDE and create/copy-paste the following source program:
This program writes the text GolamMostafa in a character array named char src[]; and also onto SD card. The program also stores AUST in the array named char saveRead[];. The program reads the string from SD card and appends it at the end of the string of saveRead[] using strcat(d, s); function.
#include<SD.h>
#include<SPI.h>
//C function prototype: char *strcat(char *dest, const char *src);
char src[] = "GolamMostafa"; //null charcater is automatically inserted by Compiler
char saveRead[50] = "AUST";//data will be saved here that will be read from SD card
char x[50];
int i=0;
File myData; //File pointer for the file to be opened in SD Card
const int chipSelect = 10; //CS-pin of SD Card is connected to DPin-10 of UNO
void setup()
{
Serial.begin(9600);
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present");//SD Crad initialization.
// don't do anything more:
return;
}
Serial.println("card initialized.");
//-----------------------------------------------------------------------------
SD.remove("Testfile.txt"); //remove if any file named Testfile.txt exists
myData = SD.open("Testfile.txt", FILE_WRITE); //file named Testfile.txt opened for writing
myData.println(src); //GolamMostafa is written into SD Card
myData.close(); //the file being pointed by pointer myData is closed
//----------------------------------------------------------------------------
myData = SD.open("Testfile.txt", FILE_READ); //file named Testfile.txt opened for reading
while (myData.available()!=0)
{
x[i] = (char)myData.read(); //x[] contains ASCII codes of characters coming from SD
Serial.print(x[i]); //show charctaers on SM (Serial Monitor)
i++;
}
myData.close(); //file closed
strcat(saveRead, x); //the content of an array can be appended at the end of another array
Serial.println();
Serial.print(saveRead); //SM prints: ASUTGolamMostafa
}
void loop()
{
}
**BTW:**I have not seen any function in the C's string.h file which can append (concatenate) a single character at the end of a string -- a job that String supports. strcat(); function supports string concatenation as is evident from this function prototype: char *strcat(char *dest, const char *src);.