hi I am trying to transmit some data from one arduino to another using Xbees and then save this data in a sd card. I used this code to transmit the data
// Example of receiving numbers by Serial
// Author: Nick Gammon
// Date: 31 March 2012
const char startOfNumberDelimiter = '<';
const char endOfNumberDelimiter = '>';
void setup ()
{
Serial.begin (115200);
Serial.println ("Starting ...");
} // end of setup
void processNumber (const long n)
{
Serial.println (n);
} // end of processNumber
void processInput ()
{
static long receivedNumber = 0;
static boolean negative = false;
byte c = Serial.read ();
switch (c)
{
case endOfNumberDelimiter:
if (negative)
processNumber (- receivedNumber);
else
processNumber (receivedNumber);
// fall through to start a new number
case startOfNumberDelimiter:
receivedNumber = 0;
negative = false;
break;
case '0' ... '9':
receivedNumber *= 10;
receivedNumber += c - '0';
break;
case '-':
negative = true;
break;
} // end of switch
} // end of processInput
void loop ()
{
if (Serial.available ())
processInput ();
// do other stuff here
} // end of loop
and it transmits the data very well, but then I modified to try to store the values from the serial monitor on a sd card, but although the data is still transmitted the right way, the wrong values are stored on the card. I am guessing it is because I changed the void function to an int function so I could store in a variable. how can I fix this? here is the modified code.
// Example of receiving numbers by Serial
// Author: Nick Gammon
// Date: 31 March 2012
#include <SD.h>
const char startOfNumberDelimiter = '<';
const char endOfNumberDelimiter = '>';
const int chipSelect = 10;
void setup ()
{
Serial.begin (57600);
Serial.println ("Starting ...");
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
} // end of setup
void processNumber (const long n)
{
Serial.println (n);
} // end of processNumber
int processInput ()
{
static long receivedNumber = 0;
static boolean negative = false;
byte c = Serial.read ();
switch (c)
{
case endOfNumberDelimiter:
if (negative)
processNumber (- receivedNumber);
else
processNumber (receivedNumber);
// fall through to start a new number
case startOfNumberDelimiter:
receivedNumber = 0;
negative = false;
break;
case '0' ... '9':
receivedNumber *= 10;
receivedNumber += c - '0';
break;
case '-':
negative = true;
break;
} // end of switch
} // end of processInput
void loop (){
File myFile;
int info;
if (Serial.available ())
info= processInput ();
myFile=SD.open("path.txt",FILE_WRITE);
myFile.print(info);
myFile.print(" , ");
myFile.close();
// do other stuff here
} // end of loop