Array dynamic memory issues

Hello All,

I'm running into some issues with dynamic memory and am looking for some tips on bringing the coding down in size.

Currently I'm using several large arrays (160 in each). I'd imagine there's a better way to write it to bring the size down somewhat.

The context: I'm finalizing a project with 160 PC fans which turn ON and OFF. Each fan is accompanied with a plastic bag which inflates and deflates. There are laid out in somewhat of a matrix and depending on the array called up the pattern changes. Almost like a physical and analog version of a LED matrix. The fans are controlled through 74595 chips across 5x 32 Channel Mosfet boards courtesy of CrossRoads...32 MOSFET board for high currents - Exhibition / Gallery - Arduino Forum

Below is a version of the code I have going. I'd like to add several more patterns yet. Maybe hex is an option? SPI... although I'm not familiar with that? Thanks!

int latchPin = 12; //is SS
int clockPin = 13; //is CLK
int dataPin = 11; //is DI (DO)

int bitVal = 0;

int patternOFF[] = {

  0,0,0,0,0,0,0,0,  //1-8
  0,0,0,0,0,0,0,0,  //9-16
  0,0,0,0,0,0,0,0,  //17-24 
  0,0,0,0,0,0,0,0,  //25-32
  0,0,0,0,0,0,0,0,  //33-40
  0,0,0,0,0,0,0,0,  //41-48
  0,0,0,0,0,0,0,0,  //49-56
  0,0,0,0,0,0,0,0,  //57-64
  0,0,0,0,0,0,0,0,  //65-72
  0,0,0,0,0,0,0,0,  //73-80
  0,0,0,0,0,0,0,0,  //81-88
  0,0,0,0,0,0,0,0,  //82-96
  0,0,0,0,0,0,0,0,  //97-104
  0,0,0,0,0,0,0,0,  //105-112
  0,0,0,0,0,0,0,0,  //113-120
  0,0,0,0,0,0,0,0,  //121-128
  0,0,0,0,0,0,0,0,  //129-136
  0,0,0,0,0,0,0,0,  //137-144
  0,0,0,0,0,0,0,0,  //145-152
  0,0,0,0,0,0,0,0   //153-160
};

int patternTest1[] = {

  0,0,0,0,0,0,0,0,  //1-8
  0,0,0,0,0,0,0,0,  //9-16
  0,0,0,0,0,0,0,0,  //17-24 
  0,0,0,0,0,0,0,0,  //25-32
  0,0,0,1,1,0,0,0,  //33-40
  0,0,1,0,0,1,0,0,  //41-48
  0,1,0,0,0,1,0,0,  //49-56
  1,0,0,0,0,0,1,0,  //57-64
  0,1,0,0,0,0,0,1,  //65-72
  0,0,0,0,0,0,1,0,  //73-80
  0,0,0,0,0,1,0,0,  //81-88
  0,0,0,0,1,0,0,0,  //82-96
  0,0,0,1,0,0,0,0,  //97-104
  0,0,0,1,0,0,0,0,  //105-112
  0,0,0,1,0,0,0,0,  //113-120
  0,0,0,1,1,1,1,1,  //121-128
  0,0,0,0,0,1,0,0,  //129-136
  0,0,0,0,0,1,0,0,  //137-144
  0,0,0,0,0,1,0,0,  //145-152
  1,0,0,0,0,1,0,0   //153-160
};

int patternTest2[] = {

  0,0,0,0,0,0,0,0,  //1-8
  0,0,0,0,0,1,0,0,  //9-16
  0,0,0,0,0,0,0,0,  //17-24 
  0,0,0,0,0,0,0,0,  //25-32
  0,0,0,0,0,0,0,0,  //33-40
  0,0,0,0,0,0,0,0,  //41-48
  1,1,1,1,1,1,1,1,  //49-56
  0,0,0,0,0,0,0,0,  //57-64
  0,0,0,0,0,0,0,0,  //65-72
  0,0,0,0,0,0,0,0,  //73-80
  1,1,1,1,1,1,1,1,  //81-88
  0,0,0,0,0,0,0,0,  //82-96
  0,0,0,0,0,0,0,0,  //97-104
  0,0,0,0,0,0,0,0,  //105-112
  0,0,0,0,0,0,0,0,  //113-120
  0,0,0,0,0,0,1,0,  //121-128
  0,0,0,0,0,0,0,0,  //129-136
  0,0,0,0,0,0,0,0,  //137-144
  0,0,0,0,0,0,0,0,  //145-152
  0,0,0,0,0,0,0,0   //153-160
};

int patternTest3[] = {

  0,1,0,0,0,1,0,0,  //1-8
  0,1,0,0,0,1,0,0,  //9-16
  0,1,0,0,0,1,0,0,  //17-24 
  0,1,0,0,0,1,0,0,  //25-32
  0,1,0,0,0,1,0,0,  //33-40
  0,1,0,0,0,1,0,0,  //41-48
  0,1,0,0,0,1,0,0,  //49-56
  0,1,0,0,0,1,0,0,  //57-64
  0,1,0,0,0,1,0,0,  //65-72
  0,1,0,0,0,1,0,0,  //73-80
  0,1,0,0,0,1,0,0,  //81-88
  0,0,1,0,0,1,0,0,  //82-96
  0,0,0,1,0,1,0,0,  //97-104
  0,0,0,0,1,1,0,0,  //105-112
  0,0,0,0,0,1,0,0,  //113-120
  0,0,0,0,0,1,1,0,  //121-128
  0,0,0,0,0,1,0,0,  //129-136
  0,0,0,0,0,1,0,0,  //137-144
  0,0,0,0,0,1,0,0,  //145-152
  0,0,0,0,0,1,0,0   //153-160
};

int patternTest4[] = {

  0,0,0,0,0,0,0,0,  //1-8
  0,0,0,0,0,1,0,0,  //9-16
  0,0,0,0,0,0,0,0,  //17-24 
  0,0,0,0,0,0,0,0,  //25-32
  0,0,0,0,0,0,0,0,  //33-40
  0,0,0,0,0,0,0,0,  //41-48
  0,0,0,0,0,0,0,0,  //49-56
  0,0,0,0,0,0,0,0,  //57-64
  0,1,0,0,0,0,0,0,  //65-72
  0,0,0,0,0,0,0,0,  //73-80
  0,0,0,0,0,0,0,0,  //81-88
  0,0,0,0,0,0,0,0,  //82-96
  0,0,0,0,0,0,0,0,  //97-104
  0,0,0,0,0,0,0,0,  //105-112
  0,0,0,0,0,0,0,0,  //113-120
  0,0,0,0,0,0,0,0,  //121-128
  0,0,0,0,0,0,0,0,  //129-136
  0,0,0,0,0,0,0,0,  //137-144
  0,0,0,0,0,0,0,0,  //145-152
  0,0,0,0,0,0,0,0   //153-160
};


void setup() { 

  Serial.begin(9600);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  

      runPatternOFF();
      delay(15000);
      runPatternTest1();
      delay(15000);
      runPatternTest2();
      delay(15000);
      runPatternTest3();
      delay(12000);
      runPatternTest4();
      delay(15000);
} 




void runPatternOFF(){

   digitalWrite(latchPin, LOW); //ground latchPin and hold low for as long as you are transmitting
   
    for (int i = 160; i >= 0; i--){
     bitVal = patternOFF[i];
    Serial.print(bitVal);
    digitalWrite(clockPin, LOW);
    digitalWrite(dataPin, bitVal);   
    digitalWrite(clockPin, HIGH);
    }
    Serial.println(" = PatternOFF");
    digitalWrite(latchPin, HIGH); //return the latch pin high to signal chip that it
    
}

void runPatternTest1(){

   digitalWrite(latchPin, LOW); //ground latchPin and hold low for as long as you are transmitting
   
    for (int i = 160; i >= 0; i--){
     bitVal = patternTest1[i];
    Serial.print(bitVal);
    digitalWrite(clockPin, LOW);
    digitalWrite(dataPin, bitVal);   
    digitalWrite(clockPin, HIGH);
    }
    Serial.println(" = PatternTest1");
    digitalWrite(latchPin, HIGH); //return the latch pin high to signal chip that it   
}

void runPatternTest2(){

   digitalWrite(latchPin, LOW); //ground latchPin and hold low for as long as you are transmitting
   
    for (int i = 160; i >= 0; i--){
     bitVal = patternTest2[i];
    Serial.print(bitVal);
    digitalWrite(clockPin, LOW);
    digitalWrite(dataPin, bitVal);   
    digitalWrite(clockPin, HIGH);
    }
    Serial.println(" = PatternTest2");
    digitalWrite(latchPin, HIGH); //return the latch pin high to signal chip that it   
}

void runPatternTest3(){

   digitalWrite(latchPin, LOW); //ground latchPin and hold low for as long as you are transmitting
   
    for (int i = 160; i >= 0; i--){
     bitVal = patternTest3[i];
    Serial.print(bitVal);
    digitalWrite(clockPin, LOW);
    digitalWrite(dataPin, bitVal);   
    digitalWrite(clockPin, HIGH);
    }
    Serial.println(" = PatternTest3");
    digitalWrite(latchPin, HIGH); //return the latch pin high to signal chip that it   
}

void runPatternTest4(){

   digitalWrite(latchPin, LOW); //ground latchPin and hold low for as long as you are transmitting
   
    for (int i = 160; i >= 0; i--){
     bitVal = patternTest4[i];
    Serial.print(bitVal);
    digitalWrite(clockPin, LOW);
    digitalWrite(dataPin, bitVal);   
    digitalWrite(clockPin, HIGH);
    }
    Serial.println(" = PatternTest4");
    digitalWrite(latchPin, HIGH); //return the latch pin high to signal chip that it   
}
int patternTest4[] = {

  0,0,0,0,0,0,0,0,  //1-8
  0,0,0,0,0,1,0,0,  //9-16
  0,0,0,0,0,0,0,0,  //17-24 
  0,0,0,0,0,0,0,0,  //25-32
  0,0,0,0,0,0,0,0,  //33-40
  0,0,0,0,0,0,0,0,  //41-48
  0,0,0,0,0,0,0,0,  //49-56
  0,0,0,0,0,0,0,0,  //57-64
  0,1,0,0,0,0,0,0,  //65-72
  0,0,0,0,0,0,0,0,  //73-80
  0,0,0,0,0,0,0,0,  //81-88
  0,0,0,0,0,0,0,0,  //82-96
  0,0,0,0,0,0,0,0,  //97-104
  0,0,0,0,0,0,0,0,  //105-112
  0,0,0,0,0,0,0,0,  //113-120
  0,0,0,0,0,0,0,0,  //121-128
  0,0,0,0,0,0,0,0,  //129-136
  0,0,0,0,0,0,0,0,  //137-144
  0,0,0,0,0,0,0,0,  //145-152
  0,0,0,0,0,0,0,0   //153-160
};

why not just use 20 bytes instead of 320?

byte patternTest4[] = {

  0b00000000,  //1-8
  0b00000000,  //9-16
etc...
int patternOFF[]

Why use int when the only value that they hold is 0 or 1.
Byte would be better.
Memory use halved in a single stroke

Why even use byte when you could use the bits of a single byte for each level ?
Memory use now one sixteenth of the original ints

Why are you using 16 bit "ints" to hold a 1 bit number? 8 bit "bytes" would halve your mem requirement, but you only need 80 bytes (640 bits).

Yes! This has cut things in half. Thanks!!

The pattern I have up right now (10 rows of eight) is only a template. Depending on how this project is installed the shape of the pattern might change to something more organic:

1,1,1,1,
0,0,0,0,0,0,0,0
1,0,0,0,0,0,0,0
0,0,0,0,1,1,1
0,1,1,1,1,1,1,1,1
..... etc

The advantage of laying the array out this way is that it's visual and makes it easier to adjust the ON/OFF of the individual fans in the physical project.

BulldogLowell, I just came across a reply of yours I'd missed from a few months ago. You'd mentioned Bit Field. Might that work here?

If it can't be easily wrapped into units of 8 then don't use a bit field. Keep using whole bytes.

You may need to look into Progmem to put your fixed patterns into program memory instead of SRAM.

MorganS:
If it can't be easily wrapped into units of 8 then don't use a bit field. Keep using whole bytes.

You may need to look into Progmem to put your fixed patterns into program memory instead of SRAM.

Thanks MorganS!

I dropped the arrays down into the program itself which took care of the issue. Between this and the change from 'int' to 'byte' I'm down to 10% rather than 107% and the pattern layout within the array can be adjustable.

All in where it's parked.

  • Appreciated all.
int latchPin = 12; //is SS
int clockPin = 13; //is CLK
int dataPin = 11; //is DI


int bitVal = 0;

void setup() { 

  Serial.begin(9600);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  

      runPatternOFF();
      delay(15000);
      runPatternTest1();
      delay(15000);
      runPatternTest2();
      delay(15000);
      runPatternTest3();
      delay(12000);
      runPatternTest4();
      delay(15000);
} 




void runPatternOFF(){

byte patternOFF[] = {

  0,0,0,0,0,0,0,0,  //1-8
  0,0,0,0,0,0,0,0,  //9-16
  0,0,0,0,0,0,0,0,  //17-24 
  0,0,0,0,0,0,0,0,  //25-32
  0,0,0,0,0,0,0,0,  //33-40
  0,0,0,0,0,0,0,0,  //41-48
  0,0,0,0,0,0,0,0,  //49-56
  0,0,0,0,0,0,0,0,  //57-64
  0,0,0,0,0,0,0,0,  //65-72
  0,0,0,0,0,0,0,0,  //73-80
  0,0,0,0,0,0,0,0,  //81-88
  0,0,0,0,0,0,0,0,  //82-96
  0,0,0,0,0,0,0,0,  //97-104
  0,0,0,0,0,0,0,0,  //105-112
  0,0,0,0,0,0,0,0,  //113-120
  0,0,0,0,0,0,0,0,  //121-128
  0,0,0,0,0,0,0,0,  //129-136
  0,0,0,0,0,0,0,0,  //137-144
  0,0,0,0,0,0,0,0,  //145-152
  0,0,0,0,0,0,0,0   //153-160
};

   digitalWrite(latchPin, LOW); //ground latchPin and hold low for as long as you are transmitting
   
    for (int i = 160; i >= 0; i--){
     bitVal = patternOFF[i];
    Serial.print(bitVal);
    digitalWrite(clockPin, LOW);
    digitalWrite(dataPin, bitVal);   
    digitalWrite(clockPin, HIGH);
    }
    Serial.println(" = PatternOFF");
    digitalWrite(latchPin, HIGH); //return the latch pin high to signal chip that it
    
}

void runPatternTest1(){

  byte patternTest1[] = {

  0,0,0,0,0,0,0,0,  //1-8
  0,0,0,0,0,0,0,0,  //9-16
  0,0,0,0,0,0,0,0,  //17-24 
  0,0,0,0,0,0,0,0,  //25-32
  0,0,0,1,1,0,0,0,  //33-40
  0,0,1,0,0,1,0,0,  //41-48
  0,1,0,0,0,1,0,0,  //49-56
  1,0,0,0,0,0,1,0,  //57-64
  0,1,0,0,0,0,0,1,  //65-72
  0,0,0,0,0,0,1,0,  //73-80
  0,0,0,0,0,1,0,0,  //81-88
  0,0,0,0,1,0,0,0,  //82-96
  0,0,0,1,0,0,0,0,  //97-104
  0,0,0,1,0,0,0,0,  //105-112
  0,0,0,1,0,0,0,0,  //113-120
  0,0,0,1,1,1,1,1,  //121-128
  0,0,0,0,0,1,0,0,  //129-136
  0,0,0,0,0,1,0,0,  //137-144
  0,0,0,0,0,1,0,0,  //145-152
  1,0,0,0,0,1,0,0   //153-160
};


   digitalWrite(latchPin, LOW); //ground latchPin and hold low for as long as you are transmitting
   
    for (int i = 160; i >= 0; i--){
     bitVal = patternTest1[i];
    Serial.print(bitVal);
    digitalWrite(clockPin, LOW);
    digitalWrite(dataPin, bitVal);   
    digitalWrite(clockPin, HIGH);
    }
    Serial.println(" = PatternTest1");
    digitalWrite(latchPin, HIGH); //return the latch pin high to signal chip that it   
}

void runPatternTest2(){

  byte patternTest2[] = {

  0,0,0,0,0,0,0,0,  //1-8
  0,0,0,0,0,1,0,0,  //9-16
  0,0,0,0,0,0,0,0,  //17-24 
  0,0,0,0,0,0,0,0,  //25-32
  0,0,0,0,0,0,0,0,  //33-40
  0,0,0,0,0,0,0,0,  //41-48
  1,1,1,1,1,1,1,1,  //49-56
  0,0,0,0,0,0,0,0,  //57-64
  0,0,0,0,0,0,0,0,  //65-72
  0,0,0,0,0,0,0,0,  //73-80
  1,1,1,1,1,1,1,1,  //81-88
  0,0,0,0,0,0,0,0,  //82-96
  0,0,0,0,0,0,0,0,  //97-104
  0,0,0,0,0,0,0,0,  //105-112
  0,0,0,0,0,0,0,0,  //113-120
  0,0,0,0,0,0,1,0,  //121-128
  0,0,0,0,0,0,0,0,  //129-136
  0,0,0,0,0,0,0,0,  //137-144
  0,0,0,0,0,0,0,0,  //145-152
  0,0,0,0,0,0,0,0   //153-160
};


   digitalWrite(latchPin, LOW); //ground latchPin and hold low for as long as you are transmitting
   
    for (int i = 160; i >= 0; i--){
     bitVal = patternTest2[i];
    Serial.print(bitVal);
    digitalWrite(clockPin, LOW);
    digitalWrite(dataPin, bitVal);   
    digitalWrite(clockPin, HIGH);
    }
    Serial.println(" = PatternTest2");
    digitalWrite(latchPin, HIGH); //return the latch pin high to signal chip that it   
}

void runPatternTest3(){

byte patternTest3[] = {

  0,1,0,0,0,1,0,0,  //1-8
  0,1,0,0,0,1,0,0,  //9-16
  0,1,0,0,0,1,0,0,  //17-24 
  0,1,0,0,0,1,0,0,  //25-32
  0,1,0,0,0,1,0,0,  //33-40
  0,1,0,0,0,1,0,0,  //41-48
  0,1,0,0,0,1,0,0,  //49-56
  0,1,0,0,0,1,0,0,  //57-64
  0,1,0,0,0,1,0,0,  //65-72
  0,1,0,0,0,1,0,0,  //73-80
  0,1,0,0,0,1,0,0,  //81-88
  0,0,1,0,0,1,0,0,  //82-96
  0,0,0,1,0,1,0,0,  //97-104
  0,0,0,0,1,1,0,0,  //105-112
  0,0,0,0,0,1,0,0,  //113-120
  0,0,0,0,0,1,1,0,  //121-128
  0,0,0,0,0,1,0,0,  //129-136
  0,0,0,0,0,1,0,0,  //137-144
  0,0,0,0,0,1,0,0,  //145-152
  0,0,0,0,0,1,0,0   //153-160
};

   digitalWrite(latchPin, LOW); //ground latchPin and hold low for as long as you are transmitting
   
    for (int i = 160; i >= 0; i--){
     bitVal = patternTest3[i];
    Serial.print(bitVal);
    digitalWrite(clockPin, LOW);
    digitalWrite(dataPin, bitVal);   
    digitalWrite(clockPin, HIGH);
    }
    Serial.println(" = PatternTest3");
    digitalWrite(latchPin, HIGH); //return the latch pin high to signal chip that it   
}

void runPatternTest4(){

byte patternTest4[] = {

  0,0,0,0,0,0,0,0,  //1-8
  0,0,0,0,0,1,0,0,  //9-16
  0,0,0,0,0,0,0,0,  //17-24 
  0,0,0,0,0,0,0,0,  //25-32
  0,0,0,0,0,0,0,0,  //33-40
  0,0,0,0,0,0,0,0,  //41-48
  0,0,0,0,0,0,0,0,  //49-56
  0,0,0,0,0,0,0,0,  //57-64
  0,1,0,0,0,0,0,0,  //65-72
  0,0,0,0,0,0,0,0,  //73-80
  0,0,0,0,0,0,0,0,  //81-88
  0,0,0,0,0,0,0,0,  //82-96
  0,0,0,0,0,0,0,0,  //97-104
  0,0,0,0,0,0,0,0,  //105-112
  0,0,0,0,0,0,0,0,  //113-120
  0,0,0,0,0,0,0,0,  //121-128
  0,0,0,0,0,0,0,0,  //129-136
  0,0,0,0,0,0,0,0,  //137-144
  0,0,0,0,0,0,0,0,  //145-152
  0,0,0,0,0,0,0,0   //153-160
};
   digitalWrite(latchPin, LOW); //ground latchPin and hold low for as long as you are transmitting
   
    for (int i = 160; i >= 0; i--){
     bitVal = patternTest4[i];
    Serial.print(bitVal);
    digitalWrite(clockPin, LOW);
    digitalWrite(dataPin, bitVal);   
    digitalWrite(clockPin, HIGH);
    }
    Serial.println(" = PatternTest4");
    digitalWrite(latchPin, HIGH); //return the latch pin high to signal chip that it   
}