Trying to SPEED UP column scanning, working with ARRAYS

Hi everybody, this is my first post so bear over with me, I try to follow the rules.

  • Arduino Mega 2560 and IDE 1.8.7
  • 8x8 LED Matrix made from NeoPixel LED strips
  • 8x8 Mercury switches as "knock" sensors arranged with

Knocking over a Mercury switch will turn on the underlying LED

I run the code and everything is working.
However, I need the code to run faster as the mercury switch is not always sensed if it is only knocked over for a brief moment.

Who can guess why the code is running slow?

  • Too large grid?
  • Slow Neopixel libraries?
  • Bad code architecture?
//Libraries
#include <Adafruit_NeoPixel.h>
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>

//Matrix data pin
#define PIN 6

//Setup of Matrix start and direction
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, PIN,
  NEO_MATRIX_TOP + NEO_MATRIX_LEFT +
  NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG,
  NEO_GRB            + NEO_KHZ800);

int row[] = {1,2,3,4,5,6,7,8};        // Defining row pins of mercury switches connected to Arduino pins
int col[] = {14,15,16,17,18,19,20,21}; // Defining column pins of mercury switches connected to Arduino


int r = 8; //number of rows
int c = 8; //number of columns
int i, j;                             // Two counter variables to count inside for loop - row and column
int col_scan;                         // Variable to hold value of scanned columns
int LEDmatrix[8][8];                  // Array of 8x8 integer value from 0 to 255, value will light on
[b]
void setup()[/b]
{
  Serial.begin(9600);
  for (i = 0; i < r; i++){
    pinMode(row[i], OUTPUT);
    }
  for (j = 0; j < c; j++) {
    pinMode(col[j], INPUT);
    }
    matrix.begin();
    matrix.setBrightness(255);
    
    //Startup animation
    matrix.fillScreen(0);
    matrix.show();
    delay(500);
    matrix.fillScreen(matrix.Color(255, 255, 255));
    matrix.show();
    delay(500);
    matrix.fillScreen(0);
    matrix.show();
}

[b]void loop() {[/b]
  mercuryRead();  // Reads which switches are turned and max the integer value to 255
  writeLED();     // Writes values from matrix to light on LED strip/matrix
  dimLED();       // Reduces the value of each integer in the matrix
}

[b]
void mercuryRead(){[/b]
  for (i = 0; i < r; i++){
    digitalWrite(row[i], HIGH);
    for (j = 0; j < c; j++){
      col_scan = digitalRead(col[j]);
      if (col_scan == HIGH){
        LEDmatrix[i][j] = 255;
        }
      }
      digitalWrite(row[i], LOW);
    }
}
[b]
void writeLED(){[/b]
  for(i = 0; i < r; i++){
    for(j = 0; j < c; j++){
      matrix.drawPixel(i, j, matrix.Color(LEDmatrix[i][j], LEDmatrix[i][j], LEDmatrix[i][j]));
      matrix.show();
      }
    }
}

[b]void dimLED(){[/b]
  for (i=0; i < r; i++){
    for (j = 0; j < c; j++){
      if (LEDmatrix[i][j] > 0){
        LEDmatrix[i][j] = LEDmatrix[i][j] - 1;
      }else{
        LEDmatrix[i][j] = 0;
        }
      }
}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
How have you got the switches wired, do you have pull up or pull down resistors fitted?

Thanks... Tom... :slight_smile:

      matrix.show();

Why is this inside the for loop ?

@TomGeorge Thank you, I just fixed it - and hope it is good now.

@UKHeliBob Wow yeah good question! I will let you know if I need it to speed up further after I try it out tonight. Thanks! :smiley:

Would require small bit of rewiring, but if you set you row/ column pins per open port, you could use direct port calls.