Button matrix not working

I've been trying to figure out why this isnt working, when I run it, it prints test even when I don't press a button. I'm sure its something related to the pinModes but I'm still learning. Thanks for any help!

int colPins[] = {5, 4, 3, 2};
int rowPins[] = {7, 6};

void setup() {
  Serial.begin(9600);
  for (int i = 0; i<sizeof(colPins)/sizeof(colPins[0]); i++) {
    pinMode(colPins[i], OUTPUT);
    digitalWrite(colPins[i], HIGH);
  }
  for (int i = 0; i<sizeof(rowPins)/sizeof(rowPins[0]); i++) {
    pinMode(rowPins[i], INPUT);
    digitalWrite(rowPins[i], LOW);
  }
}

void loop() {
  for (int i = 0; i<sizeof(rowPins)/sizeof(rowPins[0]); i++) {
    if (digitalRead(rowPins[i])==HIGH) {
      Serial.println('test');
    }
  }
}

As your topic is not directly related to the use of IDE 2.x it has been moved to the Programming category of the forum.

Please do not post pictures of code. If I wanted to try your code for myself I could not copy it from your picture

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Sorry about that! Just fixed it

Your inputs are floating. They could read HIGH or LOW. By chance, they are reading HIGH.

Have you successfully connected a single button to an Arduino and read if it is pressed or not pressed? If you do not understand about floating inputs, you won't be able to get a single button to work correctly.

I have, I'll mess around with it again and try to figure out what is wrong

You are expecting the row pins to read LOW when no button is pressed. But why should they? They are not connected to either 5V or ground. So they can do what they want. And that's usually going to be whatever you don't want :wink:

You need to prevent the input/row pins from floating. The easy way to do this is use mode INPUT_PULLUP. This will make sure they read HIGH when no button is pressed.

But then, in order to know when a button is pressed, you need them to change from HIGH to LOW. That means you need to set the column output pins to LOW.

Warning: if you do this:

    pinMode(rowPins[i], INPUT_PULLUP);
    digitalWrite(rowPins[i], LOW);

then the digitalWrite(), because of a historical quirk, will turn INPUT_PULLUP back into INPUT, and you are back where you started. So remove that digitalWrite().

Thanks

does this look any better?

int colPins[] = {5, 4, 3, 2};
int rowPins[] = {7, 6};

void setup() {
  Serial.begin(9600);
  for (int i = 0; i<sizeof(colPins)/sizeof(colPins[0]); i++) {
    pinMode(colPins[i], OUTPUT);
    digitalWrite(colPins[i], LOW);
  }
  for (int i = 0; i<sizeof(rowPins)/sizeof(rowPins[0]); i++) {
    pinMode(rowPins[i], INPUT_PULLUP);
  }
}

void loop() {
  for (int i = 0; i<sizeof(rowPins)/sizeof(rowPins[0]); i++) {
    if (digitalRead(rowPins[i])==LOW) {
      Serial.println("test");
    }
  }
}

Yes. Does it work?

I mean, it should detect any button press. But right now the code can't tell you which button. I guess you know that.

I tried it like that and it didn't print anything, didn't give me any errors either

Ah, I see the problem. Do you? It's the circuit, not the code.

Although, you could adjust the code to match the circuit.

do I have to diodes the wrong way? or is the whole circuit?

Yes, you got it. Flip the diodes around. Or change the rows to OUTPUT LOW and the columns to INPUT_PULLUP.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.