#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);
}
mega:SCL
mega:SDA
mega:AREF
mega:GND.1
mega:13
mega:12
mega:11
mega:10
mega:9
mega:8
mega:7
mega:6
mega:5
mega:4
mega:3
mega:2
mega:1
mega:0
mega:14
mega:15
mega:16
mega:17
mega:18
mega:19
mega:20
mega:21
mega:5V.1
mega:5V.2
mega:22
mega:23
mega:24
mega:25
mega:26
mega:27
mega:28
mega:29
mega:30
mega:31
mega:32
mega:33
mega:34
mega:35
mega:36
mega:37
mega:38
mega:39
mega:40
mega:41
mega:42
mega:43
mega:44
mega:45
mega:46
mega:47
mega:48
mega:49
mega:50
mega:51
mega:52
mega:53
mega:GND.4
mega:GND.5
mega:IOREF
mega:RESET
mega:3.3V
mega:5V
mega:GND.2
mega:GND.3
mega:VIN
mega:A0
mega:A1
mega:A2
mega:A3
mega:A4
mega:A5
mega:A6
mega:A7
mega:A8
mega:A9
mega:A10
mega:A11
mega:A12
mega:A13
mega:A14
mega:A15
sd1:CD
sd1:DO
sd1:GND
sd1:SCK
sd1:VCC
sd1:DI
sd1:CS