Looks like large arrays loose data while code is running

I have this code which writes 52-bytes data to 6 different shift register. For now, I'm setting all of my data bits to 1's (i.e. all 52 bytes are uint_8t 0xFF or uint_8t 255). But when I try to print this data and see the serial monitor output it shows somehow the data inside the array "spidata_6" is changed. Can someone please take a look? TIA

#define COL1_select 7
#define COL2_select 6
#define COL3_select 5

#define ROW1_select 4
#define ROW2_select 3


#include "BitArray.h"
#include <SPI.h>


 uint8_t spidata_1_1[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_1_2[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_1_3[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_1_4[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_1_5[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_1_6[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_1_7[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_1_8[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_1_9[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_1_10[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_1_11[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_1_12[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_1_13[4] = {0xFF,0xFF,0xFF,0xFF};


 uint8_t spidata_2_1[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_2_2[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_2_3[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_2_4[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_2_5[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_2_6[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_2_7[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_2_8[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_2_9[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_2_10[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_2_11[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_2_12[4] = {0xFF,0xFF,0xFF,0xFF};
 uint8_t spidata_2_13[4] = {0xFF,0xFF,0xFF,0xFF};
                            
                            
     //uint8_t spidata_2[52];
void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(COL1_select, OUTPUT);
    pinMode(COL2_select, OUTPUT);
    pinMode(COL3_select, OUTPUT);
    pinMode(ROW1_select, OUTPUT);
    pinMode(ROW2_select, OUTPUT);


    digitalWrite(COL1_select, LOW);
    digitalWrite(COL2_select, LOW);
    digitalWrite(COL3_select, LOW);
    digitalWrite(ROW1_select, LOW);
    digitalWrite(ROW2_select, LOW);

    //char buffer[52] = {};
    chipselect(1,1);  // chipselect(Row, Col)

    
    
  pinMode(10, OUTPUT); // set the SS pin as an output
  SPISettings(128000, MSBFIRST, SPI_MODE0);
  SPI.begin();         // initialize the SPI library
  Serial.begin(9600);
  while (! Serial); // Wait until Serial is ready - Leonardo
  Serial.println("Enter LED Number 0 to 7 or 'x' to clear");


  

 
}


void loop() {

uint8_t spidata_1[52] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                                  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                                  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                                  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                                  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                                  0xFF,0xFF};
                            

uint8_t spidata_2[52] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF};

                            
uint8_t spidata_3[52] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF};


uint8_t spidata_4[52] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF};

 uint8_t spidata_5[52] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                            0xFF,0xFF};


 /*uint8_t spidata_6[52] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                     0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                     0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                     0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                     0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
                     0xFF,0xFF};*/


uint8_t spidata_6[52] = {255,255,255,255,255,255,255,255,255,255,
                         255,255,255,255,255,255,255,255,255,255,
                         255,255,255,255,255,255,255,255,255,255,
                         255,255,255,255,255,255,255,255,255,255,
                         255,255,255,255,255,255,255,255,255,255,
                         255,255};
                           

Serial.println("In loop ...");



                            
  int period = 10000; // Half period in milli sec
  int period2 = 1000; // Half period in milli sec


 delay(5000);
 
  resetCol(1);
  resetCol(2);
  resetCol(3);

  //delay(600000);
  delay(10);
  
  for (int col=1;col<=4;col++)
  {
    if (col==1)
    {
      /*%%%%%%%%%%%%%%%%%%%%% ROW 2 %%%%%%%%%%%%%%%%%%%%%%%%*/
      chipselect(1,col);
      //digitalWrite(10, LOW);            // set the SS pin to LOW 
      SPI.beginTransaction(SPISettings(125000, MSBFIRST, SPI_MODE3));
      //SPI.transfer(spidata_1,52);
      
      SPI.transfer(spidata_1_1,4); //1
      SPI.transfer(spidata_1_2,4);//2
      delay(1);
      SPI.transfer(spidata_1_3,4);//3
      SPI.transfer(spidata_1_4,4);//4
      delay(1);
      SPI.transfer(spidata_1_5,4);//5
      SPI.transfer(spidata_1_6,4);//6
      delay(1);
      SPI.transfer(spidata_1_7,4);//7
      SPI.transfer(spidata_1_8,4);//8
      delay(1);
      SPI.transfer(spidata_1_9,4);//9
      SPI.transfer(spidata_1_10,4);//10
      delay(1);
      SPI.transfer(spidata_1_11,4);//11
      SPI.transfer(spidata_1_12,4);//12
      delay(1);
      SPI.transfer(spidata_1_13,4);//13
      
      SPI.endTransaction();
      /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
     
      
      /*%%%%%%%%%%%%%%%%%%%%% ROW 1 %%%%%%%%%%%%%%%%%%%%%%%%*/
      chipselect(2,col);
      //digitalWrite(10, LOW);            // set the SS pin to LOW 
      SPI.beginTransaction(SPISettings(125000, MSBFIRST, SPI_MODE3));
      //SPI.transfer(spidata_2,52);
      
      SPI.transfer(spidata_2_1,4); //1
      SPI.transfer(spidata_2_2,4);//2
      delay(1);
      SPI.transfer(spidata_2_3,4);//3
      SPI.transfer(spidata_2_4,4);//4
      delay(1);
      SPI.transfer(spidata_2_5,4);//5
      SPI.transfer(spidata_2_6,4);//6
      delay(1);
      SPI.transfer(spidata_2_7,4);//7
      SPI.transfer(spidata_2_8,4);//8
      delay(1);
      SPI.transfer(spidata_2_9,4);//9
      SPI.transfer(spidata_2_10,4);//10
      delay(1);
      SPI.transfer(spidata_2_11,4);//11
      SPI.transfer(spidata_2_12,4);//12
      delay(1);
      SPI.transfer(spidata_2_13,4);//13
      SPI.endTransaction();
      /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
      
      delay(5);
    }

       if (col==2)
    {
      /*%%%%%%%%%%%%%%%%%%%%% ROW 2 %%%%%%%%%%%%%%%%%%%%%%%%*/
      chipselect(1,col);
      //digitalWrite(10, LOW);            // set the SS pin to LOW 
      SPI.beginTransaction(SPISettings(125000, MSBFIRST, SPI_MODE3));
      SPI.transfer(spidata_3,52);
      SPI.endTransaction();
      /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
     
      
      /*%%%%%%%%%%%%%%%%%%%%% ROW 1 %%%%%%%%%%%%%%%%%%%%%%%%*/
      chipselect(2,col);
      //digitalWrite(10, LOW);            // set the SS pin to LOW 
      SPI.beginTransaction(SPISettings(125000, MSBFIRST, SPI_MODE3));
      SPI.transfer(spidata_4,52);
      SPI.endTransaction();
      /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
      
      delay(5);
    }

       if (col==3)
    {
      /*%%%%%%%%%%%%%%%%%%%%% ROW 2 %%%%%%%%%%%%%%%%%%%%%%%%*/
      chipselect(1,col);
      //digitalWrite(10, LOW);            // set the SS pin to LOW 
      SPI.beginTransaction(SPISettings(125000, MSBFIRST, SPI_MODE3));
      SPI.transfer(spidata_5,52);
      SPI.endTransaction();
      /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
     
      
      /*%%%%%%%%%%%%%%%%%%%%% ROW 1 %%%%%%%%%%%%%%%%%%%%%%%%*/
      chipselect(2,col);
      //digitalWrite(10, LOW);            // set the SS pin to LOW 
      SPI.beginTransaction(SPISettings(125000, MSBFIRST, SPI_MODE3));
      SPI.transfer(spidata_6,52);
      SPI.endTransaction();
      Serial.println(sizeof(spidata_6));
      Serial.println(sizeof(spidata_6) / sizeof(spidata_6[0]));
      for(int i=0; i<sizeof(spidata_6); i++){
        Serial.print(" 0x");
        Serial.print(spidata_6[i],HEX);
    }
      
      /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
      
      delay(5);
    }


     else if(col>=4)
     {
      
      //delay(6000000);
      delay(5000);
     Serial.println("\nDoing delays");
      resetCol(1);
      resetCol(2);
      resetCol(3);
    
      delay(10);
      resetCol(1);
      resetCol(2);
      resetCol(3);
 
      
      }

    
    
  }

  
  delay(5000); // Hold 5 sec
  
}



void chipselect(int row, int col)
{
    digitalWrite(COL1_select, LOW);
    digitalWrite(COL2_select, LOW);
    digitalWrite(COL3_select, LOW);
    digitalWrite(ROW1_select, LOW);
    digitalWrite(ROW2_select, LOW);
    delay(5);

  if (row ==1)
  {
    digitalWrite(ROW1_select, HIGH);
    }
    else if (row ==2)
    {
      digitalWrite(ROW2_select, HIGH);
      }

  if(col ==1)
  {
    digitalWrite(COL1_select, HIGH);
    }
    else if (col ==2)
    {
      digitalWrite(COL2_select, HIGH);
      }
    else if(col ==3)
    {
      digitalWrite(COL3_select, HIGH);
      }
  
  
  }


  void resetCol(int col)
  {
     uint8_t spireset_1[52] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                            0x00,0x00};

    uint8_t spireset_2[52] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                            0x00,0x00};
      
      chipselect(2,col);
      digitalWrite(10, LOW);            // set the SS pin to LOW 
      SPI.beginTransaction(SPISettings(125000, MSBFIRST, SPI_MODE3));
      SPI.transfer(spireset_2,52);
      //SPI.transfer(spi4,4);
      SPI.endTransaction();


      chipselect(1,col);
      digitalWrite(10, LOW);            // set the SS pin to LOW 
      SPI.beginTransaction(SPISettings(125000, MSBFIRST, SPI_MODE3));
      SPI.transfer(spireset_1,52);
      //SPI.transfer(spi4,4);
      SPI.endTransaction();
    
    }

Might be a memory problem, or writing outside of array boundaries. Which Arduino board?

1 Like

You really really need to read on multi dimensional arrays.
And start to get used to have your first col/row numbered as 0.
It will save you tons of code and it will make correcting errors way easier...

1 Like

I do not think you need two arrays filled with zeros to reset your chips...

1 Like

I see. I'm using UNO R3. that's what I was thinking. Even tried splitting my arrays to smaller 4-byte arrays, but the issues persists with those too.

you're absolutely right. but when I started to have this issue with data changing. I tried to make copies as a result this code has reptations. The idea was to use new variable each time which seemed to work (to an extent).

If you have them all in the same scope, splitting will not help to reduce memory needs...

1 Like

From SPI.transfer() - Arduino Reference :

Could this be the problem?

1 Like

I thought so. I googled and soon enough found a word in the search, always:

SPI always sends data in both directions on each transfer.

+1 on the entirety of #3. Multidimensional arrays in particular. And by the time you've learnt about that, you will have seen some code in examples that will help with the code you are working now.

a7

1 Like

Thanks a lot @alto777 and @Rintin for sharing this. I was googling this last night and found he same :slight_smile: I have now updated my code to loop through the arrays and transmit SPI data byte-by-byte with a dummy unit8_t variable (val) to store the returned value.

      SPI.beginTransaction(SPISettings(125000, MSBFIRST, SPI_MODE3));
     for (int i=0;i<sizeof(spidata_6);i++)
      {
        val = SPI.transfer(spidata_6[i]);
        
        }
      SPI.endTransaction();

I'll try to explore the multidimensional arrays too. Would be nice to have a single concise array to program all of my chips.

Once again, thanks everyone your valuable input and help!

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