Problem saving a serial read to an SD card

Hi all,

I'm having a issue with a project I'm building and would appreciate some help.

Outline of the project is to take a distance reading from a Fluke 414D distance meter via a porcupine electronics LR4 interface board and save the distance on an SD card.

The code that's on there website (LR4 Sample Software — Porcupine Labs) with a couple of tweaks works and I can print the distance to the serial monitor fine.

The problem comes when saving to the SD card. It just prints a blank line.

I know the issue is something to do with how the data type is being handled (ie the measurement is a string, I think!) but dont know how to convert it to something i can write to an SD card.

This is the section i'm having trouble with. I've also attached the working porcupine labs code.

if (mySerial.available()) {
char ch = mySerial.read();

if (ch == '\r') {
measureBuf[offset] - '0'; // measureBuf now contains a string like "12345"
File myFile = SD.open("Survey1.txt", FILE_WRITE);
if (myFile) {
myFile.println(measureBuf);
myFile.close();
// print to the serial port too:
Serial.println(measureBuf);
}
offset = 0;

Any ideas?

LR4_code_porcupines.ino (872 Bytes)

measureBuf[offset] - '0';

The WTF meter pegged!

Any ideas?

Yes.

Serial.println(measureBuf);

Anonymous printing is a bad idea. When something appears on the Serial Monitor, you have no idea what that represents. So, don't do it.

Serial.print("measureBuf: [");
Serial.print(measureBuf);
Serial.println("]");

will convey a LOT more information.

Yeah sorry my code is not the best as im still new to this.

The problem i having is not printing to the serial monitor, its saving the measureBuf value to the SD card.

If i type

myFile.println(measureBuf);

I get nothing on the SD card.
If i Type

myFile.println("measureBuf");

I get measureBuf saved on the card.

And if I type

Serial.println(measureBuf);

i get the value of measureBuf printed on the serial monitor.

i need to get the value of measureBuf saved on the card but its not saving it as i would expect in the first instance.

i need to get the value of measureBuf saved on the card but its not saving it as i would expect in the first instance.

Well, I need to see ALL of your code.

Yeah sorry, as I said its kind messy I think and not commented but any help would be great.

I think its something to do with the way myFile.println(measureBuf); handles a string.

#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>

char measureBuf[8]; // enough space for five digits plus a zero byte
int offset;


File myFile;
SoftwareSerial mySerial(6, 7); // TX, RX


void setup()
{
    Serial.begin(9600);
    mySerial.begin(9600);
    while (!Serial){
      ; // Wait untilSerial is ready (for Leonardo)
    }
    
    mySerial.print("g"); // tell the LR4 to go
    offset = 0;

    Serial.print("Initializing SD card...");
  
    pinMode(10, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
    }
    Serial.println("initialization done.");

  
  mySerial.print("g"); // tell the LR4 to go
  offset = 0;

  }

void loop()
{

    if (mySerial.available()) {
        char ch = mySerial.read();

        if (ch == '\r') {
            measureBuf[offset] - '0'; // measureBuf now contains a string like "12345"
              File myFile = SD.open("Survey1.txt", FILE_WRITE);
                  if (myFile) {
                    myFile.println(measureBuf);
                    myFile.close();
                    // print to the serial port too:
                    Serial.println(measureBuf);
                    }
            offset = 0;
        
        if (ch == '\n')
            offset = 0; // nothing to do, set offset to zero just to be safe

        if (offset < 7)
            measureBuf[offset++] = ch;
             
        // if the file isn't open, pop up an error:
        else {
        Serial.println("error opening Survey1.txt");
        }
      }
    }
}
            measureBuf[offset] - '0'; // measureBuf now contains a string like "12345"

Subtract '0' from a position in the array, and through away the result. Why? That's like putting masking tape over the result window on your calculator.

And, that comment is rubbish.

        if (offset < 7)
            measureBuf[offset++] = ch;
             
        // if the file isn't open, pop up an error:
        else {
        Serial.println("error opening Survey1.txt");
        }

That else goes with the if(offset < 7) statement, which makes no sense.

To be honist i used the code form porcupine labs and added a Serial.println(measureBuf) and loadded it and it worked.

All Im trying to do is save the value to the SD card but my codeing isnt greatest so this is why im here.

How would you go about writing the code to get this to work?

I presumed if the porcupine labs code could send the right info to the serial monitor (which it is currently) i could just send it to the SD card.

I presumed if the porcupine labs code could send the right info to the serial monitor (which it is currently) i could just send it to the SD card.

That sounds correct.

#include <SoftwareSerial.h>

char measureBuf[8]; // enough space for five digits plus a zero byte
int offset;

SoftwareSerial mySerial(6, 7); // TX, RX

void setup()
{
    Serial.begin(19200);
    mySerial.begin(9600);
    while (!Serial); // Wait untilSerial is ready (for Leonardo)
    mySerial.print("g"); // tell the LR4 to go
    offset = 0;

    Serial.print("Initializing SD card...");
  
    pinMode(10, OUTPUT);

    if (!SD.begin(4))
    {
       Serial.println("initialization failed!");
       return;
    }
    Serial.println("initialization done.");
}

void loop()
{
    if (mySerial.available()) {
        char ch = mySerial.read();

        if (ch == '\r') {
            measureBuf[offset] = '\0'; // measureBuf now contains a string like "12345"
            Serial.println(measureBuf);
            offset = 0;

            // Write to SD card
            File myFile = SD.open("Survey1.txt", FILE_WRITE);
            if (myFile)
            {
               myFile.println(measureBuf);
               myFile.close();

               Serial.print("Wrote ["
               Serial.print(measureBuf);
               Serial.println("] to SD card...");
            }
        }

        if (ch == '\n')
            offset = 0; // nothing to do, set offset to zero just to be safe

        if (offset < 7)
            measureBuf[offset++] = ch;
    }
}

So, what does this do?

Exactly what I wanted it to do in the first place.

Thanks so much.

I can move on to combining all my different sections now. :slight_smile: