Hello!
Hoping folks can help me sort out my issue with the OpenLog read command for extracting information from a micro SD cartd. I want to implement the read file command as provided in https://learn.sparkfun.com/tutorials/op … ommand-set.
Specifically: "Read from start 0 with length of 5:
Example: read LOG00004.txt 0 5
Output: Accel"
For my implementation, I need to extract 5 characters of ASCII (all numbers) at a time. I’ll convert those 5 numerical ASCII characters into INT, and use that 5 digit value to command a stepper motor movement. Below is my code, most of which I obtained from the beerware licence over at https://github.com/sparkfun/OpenLog/blo … adFile.ino. The Example 3 runs just fine, but when I modify it to support my implementation, I’m having significant difficulty. I can get the code to pull the ASCII characters, and save them in CHAR[6] character strings, but I can’t get them to convert into INTs for use on the stepper motor.
When I define CHAR[6] character strings within the code, I can manipulate those into INTs, but not when I extract them from the SD card. Clearly, the CHAR[6] that I define does not have the same characteristics as the one derived from the card.
Any suggestions are much appreciated!
#include <SoftwareSerial.h>
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//Connect TXO of OpenLog to pin 3, RXI to pin 2
SoftwareSerial OpenLog(5,4); // Soft RX on 5, Soft TX out on 4
//SoftwareSerial(rxPin, txPin)
int resetOpenLog = 6; //This pin resets OpenLog. Connect pin 6 to pin GRN on OpenLog.
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
int statLED = 13;
char tempString[6];
void setup() {
// put your setup code here, to run once:
pinMode(statLED, OUTPUT);
Serial.begin(9600);
setupOpenLog(); //Resets logger and waits for the '<' I'm alive character
Serial.println("OpenLog online");
}
void loop() {
gotoCommandMode(); //Puts OpenLog in command mode
Serial.println("Should be 91234"); // That's the first 5 ASCII characters in the file, which should display next (and does).
// Begin code from Sparkfun OpenLogRead()
OpenLog.print("read ");
OpenLog.print("6.csv"); // I have the identical data saved as a CSV and as a TXT. Neither one works as desired.
//OpenLog.print("7.txt");
OpenLog.print(" 0 5");
OpenLog.write(13); //This is \r
//The OpenLog echos the commands we send it by default so we have 'read log823.txt\r' sitting
//in the RX buffer. Let's try to not print this.
while(1) {
if(OpenLog.available())
if(OpenLog.read() == '\r') break;
}
//This will listen for characters coming from OpenLog and print them to the terminal
//This relies heavily on the SoftSerial buffer not overrunning. This will probably not work
//above 38400bps.
//This loop will stop listening after 1 second of no characters received
for(int timeOut = 0 ; timeOut < 1000 ; timeOut++) {
while(OpenLog.available()) {
int spot = 0;
while(OpenLog.available()) {
tempString[spot] = OpenLog.read();
spot++;
if(spot > 98) break;
}
tempString[spot] = '\0';
Serial.write(tempString); //Take the string from OpenLog and push it to the Arduino terminal, successfully outputs "90009" in ASCII
timeOut = 0;
}
delay(1);
}
// End code from Sparkfun OpenLogRead()
//char tempString[6] = "91234"; // When this line is uncommented, the below code works. Clearly the Char[6] derived from the CSV file is NOT the same as the Char[6] defined when this line is active
int Dir = tempString[0] - '0'; // The first number (index 0) of the INT indicates stepper direction, 9 is clockwise (positive) and 1 is CCW (negative)
Serial.println("Should be 9");
Serial.println(Dir); //Dir Should be 9, but it is -48
int PulseInt = 0; //This will hold the actual stepper motor pulse commands
int TempInt = 0; //Temporary integer value used in the below For loop.
for(int Spot = 1 ; Spot < 5 ; Spot++){ // This For loop converts the ASCII Char into Integer (skipping index 0). It takes the indices 1-4 and determines the desired stepper location, in pulses.
TempInt = tempString[Spot] - '0';
PulseInt = PulseInt + TempInt*pow(10,(4-Spot));
}
Serial.println("Should be 1234");
Serial.println(PulseInt); // This should be 9, but is
//Infinite loop
Serial.println("Yay!");
while(1) {
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
delay(250);
}
}
void gotoCommandMode(void) {
//Send three control z to enter OpenLog command mode
//Works with Arduino v1.0
OpenLog.write(26);
OpenLog.write(26);
OpenLog.write(26);
//Wait for OpenLog to respond with '>' to indicate we are in command mode
while(1) {
if(OpenLog.available())
if(OpenLog.read() == '>') break;
}
}
void setupOpenLog(void) {
pinMode(resetOpenLog, OUTPUT);
OpenLog.begin(9600);
//Reset OpenLog
digitalWrite(resetOpenLog, LOW);
delay(100);
digitalWrite(resetOpenLog, HIGH);
//Wait for OpenLog to respond with '<' to indicate it is alive and recording to a file
while(1) {
if(OpenLog.available())
if(OpenLog.read() == '<') break;
}
}