Datalogger with Nano

Hi,

i have a Project where i need a datalogger.
My Materials are:

  • electronic Balance/scale with RS232 Output (7Bit - Even Parity - Stopbit)
  • Arduino Nano
  • mcrobot M2 RS232/TTL converter
  • 5V ready Mirco-SD Breakout Board+

My Programcode is:

#include <SD.h>

File myFile;
const byte numChars = 32;
char receivedChars[numChars];  // an array to store the received data

boolean newData = false;

void setup() {
  Serial.begin(2400, SERIAL_7E1);
  pinMode(10, OUTPUT); //5V Reader Mirco-SD Breakout Board+ Output Pin
  if (!SD.begin(10));  {
    return;
  }
  myFile = SD.open("Test.txt", FILE_WRITE);
  myFile.println("Test Start");
  myFile.close();
}

void loop() {
  recvWithEndMarker();
  showNewData();
}

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;
  
           while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}

void showNewData() {
  if (newData == true) {
    myFile = SD.open("Test.txt", FILE_WRITE);
    myFile.println(receivedChars);
    myFile.close();
    newData = false;
  }
}

i got it from Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking and edited my SD Reader in.

I get every Second about 12 characters, something like "SD 0,05 g" upto "SD 1524,12 g".
So what i want is, to write every second the charaters in a Textfile data.
Whats my Problem? it doesn't even create a textfile :confused:

In what way is it not working?

it doesn't create a textfile or write something if it's already created.
Is there an error in the code?

I checked the code with the SD alone and with another code:

void loop() {
 char c;
 if (Serial.available() > 0) {
  c = Serial.read();
  Serial.println(c);
 }
}

but the result wasn't readable. The SD-Code did work'd but not with the newest code :confused:

i read that i could use something like
myFile = SD.Open("Text.txt", O_CREAT | O_WRITE | O_APPEND);

What does that actuall mean and why is it better? Why is there a line "|" instead of a comma ","?

Cashan:
i read that i could use something like
myFile = SD.Open("Text.txt", O_CREAT | O_WRITE | O_APPEND);

What does that actuall mean and why is it better? Why is there a line "|" instead of a comma ","?

SD.open( ) has mode available of FILE_READ or FILE_WRITE.
http://www.arduino.cc/en/Reference/SDopen

Cashan:
i read that i could use something like
myFile = SD.open("Text.txt", O_CREAT | O_WRITE | O_APPEND);

What does that actuall mean and why is it better? Why is there a line "|" instead of a comma ","?

In SD.h you will find:

#define FILE_WRITE (O_READ | O_WRITE | O_CREAT)

so the only difference is O_APPEND in place of O_READ. The "O_APPEND" says to add new data to the end of the file but that is also true of O_WRITE so it's not needed when O_WRITE is used.

If it is not creating the file and not appending to the file if it already exists then something is going wrong with the SD card. Try some of the SD library examples until you figure out why your file is not getting created.

I found the Error in my Code, but it's still not logging anything.
Can it be, that i cant get Serial Input with pin 0 and 1?
Can the command serial.read get input on Pin 1 and Pin 0 for Arduino nano?
Or is SERIAL_7E1 not possible?

Code to highlight things:

#include <SD.h>

File myFile;
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data

boolean newData = false;

void setup() {
Serial.begin(2400, SERIAL_7E1);
pinMode(10, OUTPUT); //5V Reader Mirco-SD Breakout Board+ Output Pin
if (!SD.begin(10)); { //This ';' made my program to jump directly to return, instead of checking if the card is inserted.
return;
}
myFile = SD.open("Test.txt", FILE_WRITE);
myFile.println("Test Start");
myFile.close();
}

void loop() {
recvWithEndMarker();
showNewData();
}

void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;

while (Serial.available() > 0 && newData == false) {
rc = Serial.read();

if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
//I changed '\0' to 'g'
ndx = 0;
newData = true;
}
}
}

void showNewData() {
if (newData == true) {
myFile = SD.open("Test.txt", FILE_WRITE);
myFile.println(receivedChars);
myFile.close();
newData = false;
}
}