Struggling with an Array..

Does anyone have any guidance on why the datacache[3][5] read of the array is 0.00? Sometimes it is also OVF.
The write to the array is here

void Datalogger2() {
  
  int cachemax=5;
  float datacache[cachemax][6]; // rows columns
 //------------Button Tracking------------------------------------------------------------------
     pushbuttonState = digitalRead(A1);// read the pushbutton input pin:
   if (pushbuttonState != lastpushButtonState) {
      if (pushbuttonState== LOW){
      runnum++;
     //memset(datacache,0,sizeof(datacache));
     //cachecount=0;
      }
   }
    lastpushButtonState = pushbuttonState;
//------------Send Data to Buffer-------------------
 if (pushbuttonState==LOW){
  if (cachecount<cachemax){
    datacache[cachecount][0]=millis();//RPMs;
    datacache[cachecount][1]=Torq;
    datacache[cachecount][2]=HP;
    datacache[cachecount][3]=EGT;
    datacache[cachecount][4]=CHT;
    datacache[cachecount][5]=5; //afr;
    Serial.print(cachecount);
    Serial.print(datacache[cachecount][5]);
    cachecount++;
 }

 }
//---------------------------------------------------

//-------Write Buffer to SD Card---------------------
if (cachecount==cachemax) {
  digitalWrite(A0, HIGH); // Turn the LED on
//------------Create Data file-----------------------------------------------------------
  String dfname="Run_"+String(runnum)+(".txt");//file name
  if (fpass == true){
      //Build new file
      File dataFile = SD.open(dfname, FILE_WRITE);
      dataFile.print("RPM");
      dataFile.print("\t");
      dataFile.print("Torq");
      dataFile.print("\t");
      dataFile.print("HP");
      dataFile.print("\t");
      dataFile.print("EGT");
      dataFile.print("\t");
      dataFile.print("CHT");
      dataFile.print("\t");
      dataFile.println("AFR");
      dataFile.close();
      fpass = false;
    }

//-------------Write Data--------------------------------------------------------------
  File dataFile=SD.open(dfname, FILE_WRITE);
    if (dataFile) {
      for(int i=0;i<cachemax;i++){
        dataFile.print(datacache[i][0]);
          dataFile.print("\t");
        dataFile.print(datacache[i][1]);
          dataFile.print("\t");
        dataFile.print(datacache[i][2]);
          dataFile.print("\t");
        dataFile.print(datacache[i][3]);
          dataFile.print("\t");
        dataFile.print(datacache[i][4]);
          dataFile.print("\t");
        dataFile.println(datacache[i][5]);
        Serial.println(i);
        Serial.println(datacache[i][5]); 
      }
      
      dataFile.close();
       
     }
//-------------------------------------------------
     digitalWrite(A0, LOW); // Turn the LED off
     memset(datacache,0,sizeof(datacache));
     cachecount=0;
}
}

Serial out-
05.0015.0025.0035.0045.000
5.00
1
5.00
2
5.00
3
0.00
4
5.00

At a guess, without seeing your complete sketch you are putting the wrong data type in the array. Please post a complete sketch that illustrates the problem

Because of the way that you posted it the forum software has mangled your code

Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

The formatting is very sketchy.
What does this even mean?

Int cachecount=0
Int cachemax=5

Does it compile like that?
Post the whole code.

I’m new to C… I thought this: “Int cachemax=5” would initialize and set cachemax as an integer value of 5.

Yes it compiles.

UKHeliBob:
At a guess, without seeing your complete sketch you are putting the wrong data type in the array. Please post a complete sketch that illustrates the problem

Thank you for the guidance on how to post the code. I edited my post.
The reason why I'm stumped is I fixed the input (datacache[cachecount][5]=5)
Then I watch it in serial monitor and each array spot in [5] gets 5.
When watching it print the array back out spot [3][5] becomes 0.00 or OVF.
The rest of the array reads and prints back out fine.

Your 'datacache' is a local variable inside Datalogger2(). It gets re-created, uninitialized, on the stack each time Datalogger2() is called. It looks like your intent was:

  const int cachemax = 5;
  static float datacache[cachemax][6]; // rows columns

The 'static' keyword tells the compiler to set aside permanent space for your array so each call to Datalogger2() is working on the same array. The 'const' keyword on 'cachemax' is necessary because you can't set side permanent space if you don't know what size it will be at compile time.

johnwasser:
Fixed! Thanks now a few more commands to read up on!!!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.