Need assistance with a button matrix sketch

Hello, I have built a 8x8 button matrix and I believe I properly wired them up.
This is what I came up with

const int g_column[] = {8 pins here};
const int g_row[] = {8 pins};

bool state[8][8] = {0};


void setup() {
  for (int i = 0; i < 8; i++) {
    pinMode(g_column[i], OUTPUT);
    pinMode(g_row[i], INPUT);
  }
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  while (!Serial) {
    delay(100);
  }
  digitalWrite(LED_BUILTIN, HIGH);
}


void loop() {
  work();
  delay(100);
}


void work() {
  for (int column = 0; column < 8; column++) {
    digitalWrite(g_column[column], HIGH);
    for (int row = 0; row < 8; row++) {
      bool val = digitalRead(g_row[row]);
      if (val != state[column][row]) {
        state[column][row] = val;
        String action = "";
        if (val) action = "press";
        else action = "release";
        String str = "Button " + action + " Column: " + String(column) + " Row: " + String(row) + "\n";
        Serial.print(str);
      }
    }
    digitalWrite[g_column[column], LOW];
  }

Now what this does is when I press a button at one row, it triggers for each column.
Doing a quick google search led me to this code which worked while testing.
I don't know what is wrong with my implementation and staring at it for half an hour didn't provide me an answer so here I am. What did I do wrong?

try with

  for (int i = 0; i < 8; i++) {
    pinMode(g_column[i], OUTPUT);
    digitalWrite(g_column[i], HIGH);
    pinMode(g_row[i], INPUT_PULLUP);
  }

and inversion of all the states including default value is HIGH and make it LOW one by one and test against LOW to see a press

void work() {
  for (int column = 0; column < 8; column++) {
    digitalWrite(g_column[column], LOW);
    for (int row = 0; row < 8; row++) {
      bool val = digitalRead(g_row[row]);
      if (val != state[column][row]) {
        state[column][row] = val;
        String action  "release";
        if (!val)  action = "press";
        String str = "Button " + action + " Column: " + String(column) + " Row: " + String(row) + "\n"; // what a waste of memory... just do multiple print
        Serial.print(str);
      }
    }
    digitalWrite(g_column[column], HIGH);
  }
}

Hi, @noideaplusplus ,
one of the correct ways to define an array looks like this:

(Replace the numbers with the ones you need to use in your project.)

const int g_column [] = {1,2,3,4,5,6,7,8};
const int g_row [] = {11,12,13,14,15,16,17,18};

RV mineirin

gond point - I had assumed the OP had changed this to match his setup...

I do have my pins set up correctly, that's how I copied it when sending here.
I don't know why but even after reversing the default values, it's still same.

You have a problem on this line :

digitalWrite[g_column[column], HIGH]

You used square brackets instead of parenthesis

And I'm surprised that it compiles without errors, can someone explain why ?

I have been looking at the same code expecting some kind of logic error for hours now, turns out it's a mistype. I need to get my eyes checked.

I have not checked if it compiles at all or without warning, did you? (I’m on my mobile)

(A function name is also a pointer, so may be (a big maybe) with the array notation you can get to a few bytes further in memory. I will be seen as digitalWrite[1] and then as it’s a useless statement it’s thrown away by the optimizer…)

1 Like