Changing 2 dimension array size from sd card

Good day,
Following code is a part of the project which i trimmed so as to post on the forum and focus on the issue.
The code works such the there are two text files on the sd card.

  1. One named set.txt which values 0016, 0004 which is converted to 16 (column) and 4 9row) respectively for 2 dimension array named sequence[row][col];

  2. Second file contains led blink pattern in decimal format
    eg:
    240,15,28,192,174,186,0,0,170,238,56,145,
    255,255,227,70,207,6,151,13,130,174,3,124,

everything imports fine but all i cannot change the column and row value

please guide or point me a direction
thanking you.

#include <SPI.h>
#include <SD.h>
#define CS_PIN 10

int latchPin = 8;  
int clockPin = 7; 
int dataPin = 6;  
int row;
int roww;
int column;

const int COL_COUNT = 12;    //-------------------------------------> this column i want to change from sd card.
const int ROW_COUNT = 4;     //-------------------------------------> this row i want to change from sd card.
int sequence[ROW_COUNT][COL_COUNT]= {
240,15,28,192,174,186,0,0,170,238,56,145,
255,255,227,70,207,6,151,13,130,174,3,124,
171,25,153,149,65,192,213,229,130,162,3,12,
255,255,255,255,255,255,255,255,255,255,255,255
};

void setup() {
  Serial.begin(9600);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  
  if (!SD.begin(CS_PIN)) {
    ("begin failed");
  }
  
File dataFile = SD.open("set.txt");

  if (dataFile)
  {
    char a [4] = {dataFile.read(),dataFile.read(),dataFile.read(),dataFile.read()};  
    int column = atoi(a);
    Serial.println(column);
    char b = dataFile.read();   // to forget the delimiter ","
    char e [4] = {dataFile.read(),dataFile.read(),dataFile.read(),dataFile.read()};  
    int roww = atoi(e);
    Serial.println(roww);
    dataFile.close();
    return (roww, column);
 }
 return (roww, column);
}

/*
int COL_COUNT = column;                                this is want i was trying
int ROW_COUNT = roww;                                   this is want i was trying
*/

void loop() {
      for (int row = 0; row < ROW_COUNT; row++)
      {
      for (int col = 0; col < COL_COUNT; col++) 
      {
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, sequence[row][4]);   // deleted above 11 shift register to post
      shiftOut(dataPin, clockPin, LSBFIRST, sequence[row][3]);
      shiftOut(dataPin, clockPin, LSBFIRST, sequence[row][2]);
      shiftOut(dataPin, clockPin, LSBFIRST, sequence[row][1]);
      shiftOut(dataPin, clockPin, LSBFIRST, sequence[row][0]);
      digitalWrite(latchPin, HIGH);
      delay(100);
    }  
}
}
const int COL_COUNT = 12;    //-------------------------------------> this column i want to change from sd card.
const int ROW_COUNT = 4;

const means that you can't change the values.

If you really think that it is appropriate to be changing the size of the array at run time, you'll need to use malloc() or new to dynamically allocate the array (memory), and free() or delete when you are done with the memory.

You still have to, of course, live within the available memory on the Arduino. So, using an int array to hold byte sized values is not a good idea.

You also have this alternative for defining the bounds an array dynamically. I saw this some time ago so it's given me an opportunity to test it.

// dynamic array - stack
// based on https://stackoverflow.com/questions/26441916/dynamic-array-allocation-on-stack-in-c

// dynamic array - stack

void arrayHandler( int dim0 , int dim1 ) {

  int myArray[ dim0 ] [ dim1 ] ;

  // initialise array example
  for ( int i = 0 ; i < dim0 ; i++ ) {
    for ( int j = 0 ; j < dim1 ; j++ ) {
      myArray[i][j] = i + j ;  // just an example
    }
  }

  for ( int i = 0 ; i < dim0 ; i++ ) {
    for ( int j = 0 ; j < dim1 ; j++ ) {
      Serial.println( myArray[i][j] ) ;
    }
  }
}

void setup() {
  Serial.begin(9600) ;
  arrayHandler( 2, 3 ) ;
}

void loop() { }

please guide or point me a direction

What is the largest array that you ever expect to have to deal with ? Declare the array that size in the program then, instead of trying to dynamically adjust the size of the array just have a variable holding each of the 2 dimensions and use them when you need to refer to the array bounds.

If you do not have enough memory to do that then the program will crash at some point anyway.

With the code sample you gave you could do yourself a favour by declaring the array as type byte rather than int as the file does not contain values larger than 255. Voila ! You just saved half of the memory needed to hold the array in the first place.

UKHeliBob and other,

Thanks for the direction and advice,

"Declare the array that size in the program then, instead of trying to dynamically adjust the size of the array just have a variable holding each of the 2 dimensions and use them when you need to refer to the array bounds."

I declared the array size large enough [16],[18] and used the setting value from the txt file 16, 4 at increment the row at counting the sequence. Wow thanks a ton.

Working on the next problem see you soon.

code:

int x = 4;
for (int row = 0; row < x; row++)

{
for (int col = 0; col < COL_COUNT; col++)
{
digitalWrite(latchPin, LOW);