HII...EVERYONE..
im having doubt on programming line by line sd card data read...my code reading data on sd card first line only...after tat its doing continuous loop and reading first line only...i think we need to add index it to read next line from file...but i couldnt get it to make it work...i have uploaded my code ..pls check it..
#include <SD.h>
const byte numChars = 15;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
char gas[numChars] = {0};
char breaks[numChars] = {0};
char delim[] = ",";
int count;
boolean newData = false;
const int CS_PIN = 10;
const int POW_PIN = 8;
int data;
int sensorPin = A2; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int recNum = 0;
void setup() {
pinMode(ledPin, OUTPUT);
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);
//Card will draw power from pin 8, so set it high
pinMode(POW_PIN, OUTPUT);
digitalWrite(POW_PIN, HIGH);
if (!SD.begin(CS_PIN))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
File myFile = SD.open("speed.txt");
if (myFile) {
Serial.println("speed.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
sensorValue = analogRead(sensorPin);
String datastring = "dinesh viji";
File check = SD.open("speed.txt");
if (check)
{
Serial.println("data found");
if (check.available())
{
Serial.println("data");
}
}
else
{
Serial.println("data not found");
}
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
parseData();
showParsedData();
newData = false;
delay(500);
}
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
int count;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
File check = SD.open("speed.txt");
check.seek(count * -1);
while (check.available() > 0 && newData == false) {
rc = check.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
count++;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, delim); // get the first part - the string
strcpy(messageFromPC, strtokIndx);// copy it to messageFromPC
//value = atoi(inChar);
// analogWrite(3, messageFromPC);
strtokIndx = strtok(NULL, delim); // this continues where the previous call left off
strcpy(gas, strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, delim);
strcpy(breaks, strtokIndx); // convert this part to a float
//pch = strtok (str," ,.-");
}
//============
void showParsedData() {
Serial.print("acclerator ");
int acc = atoi(messageFromPC);
analogWrite(9, acc);
Serial.println(acc);
Serial.print("gas ");
int gas1 = atoi(gas);
analogWrite(10, gas1);
Serial.println(gas1);
Serial.print("breaks ");
int breaks1 = atoi(breaks);
analogWrite(11, breaks1);
Serial.println(breaks1);
Serial.println(count);
}
// put your main code here, to run repeatedly:
sd_card_learn.ino (1.13 KB)