#include <SPI.h>
#include <SD.h>
File myFile;
char buf[80];
byte pos = 0;
byte index = 0;
#define string_length 10
char *strings[string_length]; // an array of pointers to the pieces of the above array after strtok()
char *ptr = NULL;
volatile long Xpoint[5] = {0, 1, 2, 3, 4};
volatile long Zpoint[5] = {5, 6, 7, 8, 9};
// change this to match your SD shield or module;
const int chipSelect = 53;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial)
{
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
if (!SD.begin())
{
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("position.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile)
{
String testing = "100, 200, 300, 400, 500, 600, 700, 800, 900, 1000";
Serial.print("Writing to position.txt...");
myFile.println(testing);
// close the file:
myFile.close();
Serial.println("done.");
}
else
{
// if the file didn't open, print an error:
Serial.println("error opening position.txt");
}
// re-open the file for reading:
myFile = SD.open("position.txt");
if (myFile)
{
Serial.println("position.txt:");
// read from the file until there's nothing else in it:
while (myFile.available())
{
buf[pos++] = myFile.read();
}
for (int i = 0; i < pos; i++)
{
Serial.write(buf[i]);
}
// close the file:
myFile.close();
}
else
{
// if the file didn't open, print an error:
Serial.println("error opening position.txt");
}
for (uint8_t i = 0; i < pos; i++)
{
Serial.print(buf[i]);
}
Serial.println("");
delay(3000);
ptr = strtok(buf, ","); // delimiter
while (ptr != NULL)
{
strings[index] = ptr;
index++;
ptr = strtok(NULL, ",");
}
// Serial.println(index);
// print all the parts
Serial.println("The Pieces separated by strtok()");
for (int n = 0; n < index; n++)
{
Serial.print(n);
Serial.print(" ");
Serial.println(strings[n]);
}
}
void loop()
{
// Those values are Sunlight,Humidity,Temperature,Pressure,Dewpoint.
// use the atoi() and atof() functions to convert ASCII strings to numbers.
uint8_t bobo1 = 0, bobo2 = 0;
for (int n = 0; n < index; n++)
{
if (n%2){
Zpoint[bobo1] = atoi(strings[n]);
bobo1 ++;
}
else{
Xpoint[bobo2] = atoi(strings[n]);
bobo2 ++;
}
}
for (uint8_t i = 0; i < 5; i++)
{
Serial.print(Xpoint[i]);
Serial.print(" , ");
Serial.print(Zpoint[i]);
Serial.println("");
}
delay(3000);
}