Hi i have this really strange (well for me anyway) problem that the first value in one of my arrays changes for "no reason" as you can see i define 3 arrays of size 20, infoFileArray is the one that changes (first value). There are 3 functions that are called in the setup, the first one reads a SD card file and loads the data into the array infoFileArray. All the values are correct. I then call another function that has nothing to do with infoFileArray and the values are still correct but when i call the 3rd function (also not having anything to do with infoFileArray the first value of infoFileArray changes from 100 to 0!!!
// these 3 arrays are used to update the dontTouch file that stores how many kVA each power source has giving in the last period.
int powerSource; //where the power is coming from, i.e. gen set 1,2,3... trasnformer, battery-inverter.....
int infoFileArray[20]; //where the power characteristics are stored for each power supply
float kVAarrayRead[20];
float kVAarrayWrite[20];
bool DTF = false; //used to oscillate between the two dontOpen files
int outsideTemp;
const float reductionPercent = 0.000001;
As you can see at the end of this function i have put in 3 debug statements, debug1 returns "100", debug2 returns "100" for every run of the while loop except for the last run of the while loop...!!!? then it returns "0"! debug3 also returns "0". I managed to find out that if i change int to float for the infoFileArray then it works fine... but this is very strange because all the numbers in the SD file are max 3 digits long.... can anyone tell me why this is happening and also why it happens in part of the code that has nothing to do with this array...?
void dontTouchRead()
{
int sizeDontOpn1 = 0;
int sizeDontOpn2 = 0;
int j=0;
char readLine[100]; // making an array of characters (char's) to store ecah character that is read from the file
int middleTemp = 0;
if(SD.exists("dontOpn1.csv")) { // finds out if the file exists and how big it is
dontOpn1 = SD.open("dontOpn1.csv");
delay(10);
sizeDontOpn1 = dontOpn1.size();
dontOpn1.close();
delay(10);
Serial.println(sizeDontOpn1);
}
if(SD.exists("dontOpn2.csv")) { // finds out if the file exists and how big it is
dontOpn2 = SD.open("dontOpn2.csv");
delay(10);
sizeDontOpn2 = dontOpn2.size();
dontOpn2.close();
delay(10);
Serial.println(sizeDontOpn2);
}
if ((sizeDontOpn1 <= 1) && (sizeDontOpn2 <= 1)){ //checks that there is at least data in one of the files, jumps out of the function otherwise
}
else{
if (sizeDontOpn1 > sizeDontOpn2){
Serial.println("entered size one biggger than 2");
dontOpn1 = SD.open("dontOpn1.csv", FILE_READ); // open "infoFile.csv" to read data
if (dontOpn1) { //checking that the file opened
for (int i=0; i<100; i++) readLine[i] = dontOpn1.read();// loading characters into the char array
dontOpn1.close();
}
}
else{
Serial.println("entered size 2 biggger than 1");
dontOpn2 = SD.open("dontOpn2.csv", FILE_READ); // open "infoFile.csv" to read data
if (dontOpn2) { //checking that the file opened
for (int i=0; i<100; i++) readLine[i] = dontOpn2.read();// loading characters into the char array
dontOpn2.close();
}
}
Serial.println("debug 1");
Serial.println(infoFileArray[0]);
char* tokenValue; //creating a pointer that will point to the first section of the array between the two ";"
tokenValue = strtok(readLine, ";"); //there is also something going on here with putting a /0 at the end so it can find where to start next time
while (tokenValue != NULL)
{
kVAarrayRead[j]= atof(tokenValue); // changing from char string to int and storing in array
j=j+1;
tokenValue = strtok (NULL, ";");
Serial.println("debug 2");
Serial.println(infoFileArray[0]);
}
Serial.println("debug 3");
Serial.println(infoFileArray[0]);
// unixTime2000= kVAarrayRead[0];
// depreciationTime1 = rtc.getUnixTime(rtc.getTime())-946684800 - unixTime2000;
// middleTemp = (outsideTemp + kVAarrayRead[1])/2; //getting the average of the temp when the system went down and when it started again
// for (int i = 0; i<18; i++){
// kVAarrayWrite[i+2] = kVAarrayRead[i+2] - (infoFileArray[i] * reductionPercent * depreciationTime1 * ((25+100)/(middleTemp+100))); //
// }
// test22 = rtc.getUnixTime(rtc.getTime())-946684800;
}
}
Here is also the function for reading into the infoFileArray.... thanks for any help
void readInfoFile() {
int j=0;
char readLine[30]; // making an array of characters (char's) to store ecah character that is read from the file
File infoFile = SD.open("infoFile.csv", FILE_READ); // open "infoFile.csv" to read data
if (infoFile) { //checking that the file opened
for (int i=0; i<30; i++){
readLine[i] = infoFile.read();// loading characters into the char array
}
infoFile.close();
}
char* tokenValue; //creating a pointer that will point to the first section of the array between the two ";"
tokenValue = strtok(readLine, ";"); //there is also something going on here with putting a /0 at the end so it can find where to start next time
while (tokenValue != NULL)
{
infoFileArray[j]= atoi(tokenValue); // changing from char string to int and storing in array
j=j+1;
tokenValue = strtok (NULL, ";");
}
}