8x8 Matrix - Hall sensor error

You could reduce your code size by about 90% if you take a bit of time and learn about arrays rather than the massive amount of duplication reading/reacting to each pin.

/* Include the LedControl library */
#include "LedControl.h"
// Create a LedControl for the first 8 devices...
LedControl lc1 = LedControl(16, 18, 17, 1);

const byte pins[] = {
  6, 5, 38, 36, A7, A8, 37, 39, // A
  7, 4, 40, 34, A6, A9, 35, 41, // B
  8, 3, 42, 32, A5, A10, 33, 43, // C
  9, 2, 44, 30, A4, A11, 31, 45, // D
  10, 1, 46, 28, A3, A12, 29, 47, // E
  11, 0, 48, 26, A2, A13, 27, 49, // F
  12, 14, 50, 24, A1, A14, 25, 51, // G
  13, 15, 52, 22, A0, A15, 23, 53, // H
}

const byte nPins = sizeof(pins) / sizeof(pins[0]);

void setup() {

  // all pins are INPUT by default

  for (int index = 0; index < lc1.getDeviceCount(); index++) {
    lc1.shutdown(index, false);
  }
}

void loop() {

  for ( int i = 0; i < nPins; ++i ) {
    int hallState = digitalRead( pins[i] );
    int state = false;
    int row =  i / 8;
    int col = i % 8;
    if (hallState == LOW) {
      state = true;
    }
    lc1.setLed(0, row, col, state);
  }
}

But then you still have the problem of the pins used for the lcd control
LedControl lc1 = LedControl(16, 18, 17, 1); can NOT be used for your inputs.

2 Likes