I am not too familiar with arduino
I not only need to read from the text file, but i need to store the data into an array.
I want to do something similar to what loadtxt() does in python
basically myArray[]=loadtxt('myFile')
text file contains :
1 11
106 5
52 7
31 20
....
- (values do not matter/are random, number of values are unknown/ can change depending on text file)
this is where i think i am having trouble -->Serial.println(dataFile.read());
Serial.write(dataFile.read()) prints the right values, however i need to store these values in a variable of some sort
#include <SPI.h> //SD card headers
#include <SD.h>
using namespace std;
/////////////////////////////////////////////////////
const int buttonPin = 2; //button pin = Pin 2, input pin
const int ledPin = 12; // output pin=pin12, lights up led
int buttonPinState=0; //button presses or not pressed, HIGH/LOW
bool programEnd= false; // has reached end of array
/*
int Array [] [] = {
//as many vals as dim1
{2,5},
{3,7},
{6,8},
{9,10},
{11,13}//as many rows as dim0
};
*/
int counter=1; // used to compare values executed vs values stored
volatile int sizeArray=0; //global varable, size of array text file
int index1=0; //array index
int index2=0; //array index 2
File dataFile; //file variable name
/////////////////////////////////////////////////////////////////////////////////////
void setup() {
int i = 0;
int j = 0;
///////////////////////////////////////////////////////////////////////////
pinMode(buttonPin, INPUT); //button as input
pinMode(ledPin, OUTPUT); //led as output
Serial.begin(9600); //start serial
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
////////////////////////////////////////////////////////////////////////
//initalize SD card
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
} else {
Serial.println("example.txt doesn't exist.");
}
/////////////////////////////////////////////////////////////////////////
dataFile = SD.open("example.txt"); // open txt file
////////////////////////////////////////////////////////////////////////
// import data to an array
if (dataFile){
while (dataFile.available()){
Serial.println(dataFile.read());
}//end of while loop
}//end of if statement
//sizeArray=sizeof(textValues)/4; //array size is 5
}
void loop(){
buttonPinState=digitalRead(buttonPin);
if(buttonPinState==HIGH && programEnd==false){
digitalWrite(ledPin, HIGH);
//Serial.print(textValues[index1][index2]);
index2++;
Serial.print("\t");
//Serial.println(textValues[index1][index2]);
delay(2000);
index2++;
if (counter>=sizeArray){
programEnd=true;
}
counter++;
}
digitalWrite(ledPin, LOW);
delay(500);
}