Go Down

Topic: storing data from the serial monitor (Read 466 times) previous topic - next topic

victorfb

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
Code: [Select]
// 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.
Code: [Select]
// 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

PaulS

When you change the return type of a function from void to int, float, char, etc., you need to add a return statement to actually return something. The function can not infer what you want to return.


Nick Gammon

What PaulS said. Plus you are writing the file thousands of times a second. It would be surprising if you haven't worn out the SD card:

Code: [Select]

  if (Serial.available ())
   info= processInput ();

  myFile=SD.open("path.txt",FILE_WRITE);
  myFile.print(info);
  myFile.print(" , ");
  myFile.close();


Whether or not you get input you are creating a file and writing "info" to it. And as PaulS said, you aren't putting anything in info. You are doing it all in the wrong place basically. That should be in processNumber, like this:

Code: [Select]

void processNumber (const long n)
  {
  File myFile;
  myFile=SD.open("path.txt",FILE_WRITE);
  myFile.print(n);
  myFile.print(" , ");
  myFile.close();
  }  // end of processNumber

http://www.gammon.com.au/electronics

victorfb

I changed the code and I put the writing part on the processNumber the way you said:
Code: [Select]
// 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)
  {
  File myFile; 
  Serial.println (n);
  myFile=SD.open("path.txt",FILE_WRITE);
  myFile.print(n);
  myFile.print(" , ");
  myFile.close();
  }  // 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

but it still stores some data when there is nothing being transmitted and when I transmit something the values stored are completely different. This is what I see on the file that was created:

60 , 0 , 0 , 4 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 4 , 60 , 0 , 0 , 0 , 5 , 60 , 0 , 0 , 60 , 0 , 0 , 4 , 60 , 0 , 0

do you know why this is happening?

Nick Gammon

http://www.gammon.com.au/electronics

victorfb

Quote
Did you delete the old file?

yes

victorfb

I got it fixed, the problem was that when I was sending the data I was printing in the serial monitor with Serial.print() and on the receiver end I was writing to the serial monitor using Serial.println(). After I changed the last one to Serial.print everything worked.
Thank you!

Go Up