Thank for your program, I am a newbie in Arduino. I have used your program to read a data file and import it to the program but it has a very weird bug.
#include <SPI.h>
#include <SD.h> // SD card
File file;
bool readLine(File &f, char* line, size_t maxLen) {
for (size_t n = 0; n < maxLen; n++) {
int c = f.read();
if ( c < 0 && n == 0) return false; // EOF
if (c < 0 || c == '\n') {
line[n] = 0;
return true;
}
line[n] = c;
}
return false; // line too long
}
bool readVals(float* v1, float* v2) {
char line[100], *ptr, *str;
if (!readLine(file, line, sizeof(line))) {
return false; // EOF or too long
}
*v1 = strtod(line, &ptr);
if (ptr == line) return false; // bad number if equal
while (*ptr) {
if (*ptr++ == ',') break;
}
*v2 = strtod(ptr, &str);
return str != ptr; // true if number found
}
float qnom = 1500;
float sOc;
float qrel;
float qrelnew;
void setup(){
float VOC[100]; // result1
float SOC[100]; // result2
int i = 0;
float x, y;
Serial.begin(9600);
if (!SD.begin(4)) {
Serial.println("begin error");
return;
}
file = SD.open("test.csv", FILE_READ);
if (!file) {
Serial.println("open error");
return;
}
while (readVals(&x, &y)) {
VOC = x;
_ Serial.print(VOC*,4);_
_ Serial.print(": ");_
_ SOC = y;
Serial.println(SOC,4);
i++;*_
* }*
* file.close();*
* Serial.println("done");*
* float voltage_sensor=0.0;
_ float closet;
float V;*_
//voltage_sensor = (analogRead(A0)/1023.0)4.97; // read the sensor voltage
voltage_sensor = 3.2;
V = voltage_sensor;
_ Serial.println(V);*_
* closet = VOC[0];*
* sOc = SOC[0];*
* for ( i = 0 ; i < 99; i++) {*
_ if ((VOC - V) == 0) {
sOc = SOC*;
break;
}
else {*_
* if ( (abs(VOC[i+1] - V)) < (abs(closet - V))) {*
* closet = VOC[i+1];*
* sOc = SOC[i+1];}*
* }*
* }*
* int soc;*
_ soc = sOc100;
qrel = qnomsOc;
* Serial.print("SOC:");
Serial.print(soc);
Serial.println("%");
Serial.println("done");*_
}
* float voltage_sensor=0.0;
float sensor_current=0.0;
_ float deltaT = 1;
int z;
float t = 0.0;*_
void loop() {
* //sensor_current = (voltage_sensor - 2.5)/(0.3267); // convert the voltage to current beased on the relationship between I&V in the datasheet*
* sensor_current = 3;*
* float iread1 = sensor_current;
_ delay (1000);_
float iread2 = sensor_current;
_ //Serial.println(qrel);*_
_ /
qrelnew = qrel;
qrel = qrelnew - (((iread1+iread2)/2)1000(deltaT/3600));
sOc = (qrel)/(qnom);
z = sOc100;
* Serial.print("SOC: "); // shows the voltage measured*
* Serial.print(z); // the '3' after voltage allows you to display 3 digits after decimal point*
* Serial.println("%");*
*/_
_ /
if (qrel<=0) {_
voltage_sensor = 0;
sensor_current=0;
_ iread1 = 0;
iread2 = 0;
}
if ((iread1 >=0)&&(iread2>=0)) {
t = ((qrel60)/(iread21000));
}
else if ((iread1 < 0)&&(iread2 < 0)) {
t = (((qnom - qrel)60)/(-iread21000));
}*_
* Serial.print("Time Remaining: "); // shows the voltage measured*
* Serial.print(t); // the '3' after voltage allows you to display 3 digits after decimal point*
* Serial.println(" mins");*
* delay(1000);*
_ */_
* }*
[/quote]
The above code works well (I did comment out almost void loop). However, when I go to the void loop, it occurs an error, and I can not open the file any more. I really dont understand because the void loop did not use the file from sd card to work, so why it can not work and the file could not able to read any more?
Thank you so much for your help, it is very important for me and I could not looking for anyone to help me.
Here is the code want to run, and it has the error:
*> #include <SPI.h> *
> #include <SD.h> // SD card
>
>
> File file;
>
> bool readLine(File &f, char* line, size_t maxLen) {
> for (size_t n = 0; n < maxLen; n++) {
> int c = f.read();
> if ( c < 0 && n == 0) return false; // EOF
> if (c < 0 || c == '\n') {
> line[n] = 0;
> return true;
> }
> line[n] = c;
> }
> return false; // line too long
> }
>
> bool readVals(float* v1, float* v2) {
> char line[100], *ptr, *str;
> if (!readLine(file, line, sizeof(line))) {
> return false; // EOF or too long
> }
> *v1 = strtod(line, &ptr);
> if (ptr == line) return false; // bad number if equal
> while (*ptr) {
> if (*ptr++ == ',') break;
> }
> *v2 = strtod(ptr, &str);
> return str != ptr; // true if number found
> }
>
> float qnom = 1500;
> float sOc;
> float qrel;
> float qrelnew;
*> *
> void setup(){
> float VOC[100]; // result1
> float SOC[100]; // result2
> int i = 0;
>
> float x, y;
> Serial.begin(9600);
> if (!SD.begin(4)) {
> Serial.println("begin error");
> return;
> }
> file = SD.open("test.csv", FILE_READ);
> if (!file) {
> Serial.println("open error");
> return;
> }
> while (readVals(&x, &y)) {
> VOC = x;
> _ Serial.print(VOC*,4);_
> _ Serial.print(": ");
> SOC = y;
> Serial.println(SOC,4);
> i++;
>
> }
> file.close();
> Serial.println("done");
>_
> float voltage_sensor=0.0;
> _ float closet;
> float V;
>_
> //voltage_sensor = (analogRead(A0)/1023.0)4.97; // read the sensor voltage
> voltage_sensor = 3.2;
> * V = voltage_sensor;
> _ Serial.println(V);
>
> closet = VOC[0];
> sOc = SOC[0];
>
>
> for ( i = 0 ; i < 99; i++) {
> if ((VOC - V) == 0) {
> sOc = SOC;
> break;
> }
> else {
>
> if ( (abs(VOC[i+1] - V)) < (abs(closet - V))) {
> closet = VOC[i+1];
> sOc = SOC[i+1];}
> }
>
> }
> int soc;
> soc = sOc100;
> qrel = qnomsOc;
> Serial.print("SOC:");
> Serial.print(soc);
> Serial.println("%");
> Serial.println("done");
>
> }_
> float voltage_sensor=0.0;
> float sensor_current=0.0;
> _ float deltaT = 1;
> int z;
> float t = 0.0;
>
>
> void loop() {
>_
> //sensor_current = (voltage_sensor - 2.5)/(0.3267); // convert the voltage to current beased on the relationship between I&V in the datasheet*
> * sensor_current = 3;
_>_
> float iread1 = sensor_current;
> _ delay (1000);_
> float iread2 = sensor_current;
> _ //Serial.println(qrel);
>
>
> qrelnew = qrel;
> qrel = qrelnew - (((iread1+iread2)/2)1000(deltaT/3600));
> sOc = (qrel)/(qnom);
> z = sOc100;
> * Serial.print("SOC: "); // shows the voltage measured*
> * Serial.print(z); // the '3' after voltage allows you to display 3 digits after decimal point*
> * Serial.println("%");
>
>
> /
> * if (qrel<=0) {_
> voltage_sensor = 0;
> sensor_current=0;
> _ iread1 = 0;
> iread2 = 0;
> }
> if ((iread1 >=0)&&(iread2>=0)) {
> t = ((qrel60)/(iread21000));
> }
> else if ((iread1 < 0)&&(iread2 < 0)) {
> t = (((qnom - qrel)60)/(-iread21000));
> }
>
> Serial.print("Time Remaining: "); // shows the voltage measured*
> * Serial.print(t); // the '3' after voltage allows you to display 3 digits after decimal point*
> * Serial.println(" mins");
> delay(1000);
> /
>
> * }
> [/quote]*_