Hello everyone,
I'm having a hard time getting a variable to come out of my search function correctly. I am pulling from a text file that has integer values for a, b, and c but no d. I define all 4 of the variables at the very beginning and a, b, c get written over after the search function is executed. I want my default value for d to be used if it doesn't show up in my text file. I want to still look in the function for d in the event that my text file is updated and d gets entered. Right now I have it "hardcoded" so that I get the default value for d when it is not in the text file but what if I change the variable name or add more variables to the function? Each of these new variables would have their own default value and I don't want them to be written over by d in the event they also are not in the text file. I tried methods of just jumping out of the program if d isn't present but it prints a very high number(546) instead of the default for d. I have been trying StringToInt to no avail. Any tips?
Thank you,
Kelsey
#include <SPI.h>
#include <SD.h>
const int chipSelect = 53;
int a = 10;
int b = 20;
int c = 30;
int d = 40;
void setup(){
Serial.begin(9600);
if(!SD.begin(chipSelect)){
Serial.println("card failed:");
}
else{
Serial.println("card loaded:");
a = search("a");
Serial.println(a);
c = search("c");
Serial.println(c);
b = search("b");
Serial.println(b);
d = search("d");
Serial.println(d);
}
}
int search(String temp){
File myFile = SD.open("D.txt");
char test[3];
temp.toCharArray(test,2);
boolean presence = myFile.find(test);
if (presence == 1)
{
int Q = myFile.parseInt();
return Q;
myFile.close();
}
else if (presence == 0)
{
return d; //only works for D. need to expand for all.
myFile.close();
}
}
void loop(){
}