I'm trying to read serial from an external sensor, split it into a few substrings (it comes in as a long string but I don't need all the characters that are output by the sensor), and then print those strings into a txt file onto an sd card. I saw a lot of forum posts talking about reading from an sd card but nothing about the opposite. I've taken a look at the datalogger and the Read/Write examples in the IDE they were some help but not quite enough. I'm also using a sparkfun micro SD shield. Any help will be appreciated.
Here is my code. (sorry about all the commented out stuff. I have tried a lot of things but didn't want to delete anything in case I needed to come back to something.)
// include the SD library:
#include <SPI.h>
#include <SD.h>
const byte numChars = 145;
char recievedChars[numChars];
static byte ndx = 0;
char c;
//String test = [1,2,3,4];
File myFile;
File dataFile;
// set up variables using the SD utility library functions:
//Sd2Card card;
//SdVolume volume;
//SdFile root;
// The Sparkfun microSD shield uses pin 8 for CS
const int chipSelect = 8;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(38400);
//while (!Serial) {
//; // wait for serial port to connect. Needed for Leonardo only
//}
Serial.print("\nInitializing SD card...");
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
// we'll use the initialization code from the utility libraries
// since we're just testing if the card is working!
//if (!card.init(SPI_HALF_SPEED, chipSelect)) {
if (!SD.begin(8)) {
Serial.println("initialization failed. Things to check:");
//Serial.println("* is a card is inserted?");
//Serial.println("* Is your wiring correct?");
//Serial.println("* did you change the chipSelect pin to match your shield or module?");
//return;
} else {
Serial.println("Wiring is correct and a card is present.");
}
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
}
else {
Serial.println("ERROR Writing to Card");
}
}
void loop() {
while (Serial.available()>0 ) {
c = Serial.read();
if (c !='\n') {
recievedChars[ndx] = c;
ndx++;
if (ndx >= numChars){
ndx = numChars -1;
}
}
else {
recievedChars[ndx] = '\0';
ndx=0;
}
}
// make a string for assembling the data to log:
String s = "";
s = String(recievedChars);
String x = s.substring(30,38);
String y = s.substring(45,53);
String z = s.substring(60,68);
// read three sensors and append to the string:
//for (int analogPin = 0; analogPin < 3; analogPin++) {
//int sensor = analogRead(analogPin);
//dataString += String(sensor);
//if (analogPin < 2) {
//dataString += ",";
//}
//}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
//Serial.print("Writing to dataFile");
//dataFile.println(s);
Serial.print(s);
dataFile.println(x);
//Serial.println(x);
//dataFile.println(y);
//Serial.println(y);
//dataFile.println(z);
//Serial.println(z);
//dataFile.close();
//Serial.println("Done Printing to DATALOG");
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
What are the (symptoms of) your problems? It does not work will not help us to help you ![]()
Which board are you using? Sharing Serial between the PC and the sensor is not a good idea. For Uno and Nano, you eill probably need SoftwareSerial which is limited in baudrate, for other boards you can use e.g. Serial1 for communication with the sensor.
You never seem to close the file but repeatedly open it; you used to have it but currently it's commented out.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.