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;
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.
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;
}
}