groundFungus:
Here is my best effort to illustrate the advice given to you. Namely, send the data with start and end markers, use a software serial port* on the Uno to receive the data and use the methods from the serial input basics to receive the data to be written to SD card. The code has been tested using 2 Unos connected by software serial.
This sketch is just to send sample data to the Uno with the SD card attached. It shows how the data packet is formed and sent.
// program to transmit sample test data to another Uno to be written to
// an SD card.
// by C. Goulding
#include <SoftwareSerial.h>
SoftwareSerial ssPort(7, 4);
const int BUFFER_SIZE = 32;
int value1 = 26;
int value2 = 15;
void setup()
{
Serial.begin(115200);
ssPort.begin(9600);
Serial.println(F("starting test data transmission"));
delay(3000);
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 2000;
if(millis() - timer >= interval)
{
timer = millis();
char buffer[BUFFER_SIZE];
sprintf(buffer, "<%d.%d,%d.%d,%lu>", value1, value2, value1, value2, millis());
Serial.print(F("Transmitting "));
Serial.print(buffer);
ssPort.println(buffer);
Serial.println(F(" ****** transmission complete"));
}
}
This sketch receives the data through a software serial port on pins 4 and 7. Each time that it receives a full packet it writes the packet to SD card.
// 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 = 10;
File testDataFile;
SoftwareSerial ssPort(4, 7);
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(115200);
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("done."));
}
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)
{
rc = ssPort.read();
if (recvInProgress == true)
{
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker)
{
recvInProgress = true;
}
}
}
void initSD()
{
Serial.print(F("Initializing SD card..."));
if (!SD.begin(csSD))
{
Serial.println(F("############ initialization failed! ########"));
while (1);
}
Serial.println(F("initialization done."));
}
Maybe you can get some ideas from this that will help with your project.
* There are "better" software serial libraries, but I use SoftwareSerial, here, because it is simplest and comes with the IDE.
Thank you for the code. I will execute it and inform you about it. However, Can I declare the multiple TX,RX for arduino UNO? So if i declare two digital pins using software serial, will it be used as TX and RX? I wanted to confirm it with you.