I have written a code with the help of [Write to sd card from phone app via bluetooth] discussion
// program to receive test data Iinto a null termnated character
// array (string) and write the string to a SD card
// using methods from Robin2's serial input basics
// by C. Goulding
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
const byte csSD = 4;//10;
File testDataFile;
SoftwareSerial ssPort(0,1); //(RX, TX)
const byte numChars = 32; //**** adjust this number for the longest data set
// can be up to 255.
char receivedChars[numChars];
boolean newData = false; // true whenever new data comes in
// must be set to false before new data can be read
void setup()
{
Serial.begin(9600);
ssPort.begin(9600);
initSD();
Serial.println(F("starting test data reception"));
delay(3000);
}
void loop()
{
recvWithStartEndMarkers(); // get new data if it is there
if (newData == true) // got new data, save it to SD
{
//Serial.print(F("This data just received "));
Serial.println(receivedChars);
testDataFile = SD.open("test.txt", FILE_WRITE);
if (testDataFile)
{
//Serial.print(F("Writing to test.txt..."));
testDataFile.println(receivedChars);
testDataFile.close();
// Serial.println(F("-1"));
}
else
{
Serial.println(F(" $$$$$ error opening test.txt $$$$"));
}
newData = false; // OK ready for more data
}
}
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '@';//'<'
char endMarker = '$'; //’>’
char rc;
while (ssPort.available() > 0 && newData == false)
//while (Serial.available() > 0 && newData == false)
{
rc = ssPort.read();
//rc = Serial.read();
if (recvInProgress == true)
{
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
//receivedChars[ndx] = ‘\0’; // terminate the string
//receivedChars[ndx] = ‘\0’;
recvInProgress = false;
ndx = 0;
//delay(500);
newData = true;
}
}
else if (rc == startMarker)
{
//delay(500);
recvInProgress = true;
}
}
}
void initSD()
{
if (!SD.begin(csSD))
{
Serial.println("############ initialization failed! ########");
while (1);
}
Serial.println("############ initialization done! ########");
}
The text file which I want to send to sd card is as below
6,0805,1,w.wav,last bell
6,0805,2,r.wav,last bell
6,0805,3,p.wav,last bell
6,0805,4,p.wav,last bell
6,0805,5,p.wav,last bell
6,0805,1,w.wav,last bell
6,0805,2,r.wav,last bell
6,0805,3,p.wav,last bell
6,0805,4,p.wav,last bell
6,0805,5,p.wav,last bell
-1
but result is
############ initialization done! ########
starting test data reception
6,0805,1,w.wav,last bell
6,0805,1,w.wav,last bell
6,0805,1@6,0805,1,w.wav,last belB
6,0805,1,w.wav,last belllast belB
6,0805,1,w.wav,last belllast belB
6,0805,.wav,last bellelllast belB
6,0805,1,wv,last bellelllast belB
6,0805,1,w.wav,last bell@6,0805lB
6,0805,1,w.wav,last bell@6,0805lB
6,0805,1,w.wav,last bell@6,0805lB
6,0805,.wav,last bellell@6,0805lB
6,0805,1,w6,0805,1,w.wav,last blB
the android app is developed in Mit App Inventor 2

Please resolve the issue so that data may be send properly to sd card without corruption of data`Preformatted text`
@i4infotech, your topic was moved to a more suitable location on the foum.
Please edit your post, select all code and click </>
to apply code tags to your code. Next save the post again.
First quesdtion is why you use SoftwareSerial on the hardware serial port.
Then what should I use? Please guide me
Some other pins that you don't use; e.g.
SoftwareSerial ssPort(2,3);
And hook your bluetooth module up to pins 2 and 3.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.