Problem with keyboard matrix scan

This is my first project, so bear with me. I am creating a musical keyboard, and am working first on making sure my keyboard matrix scan works. The basic sketch reads the row and displays the key "number" on an LCD display. I'm able to get the display to change based on key press, however it only displays the keys (36-40) associated with the last column turned on. I confirmed this by disabling the enabling of column 8, and the outcome propagated with keys 31-35 being displayed on the LCD screen. It's almost as if the loop is being run once, stopping at the last column in the loop. Any help would be appreciated to help me understand how to make sure each key number is reflected by column.

Code is below:

#include <LiquidCrystal_I2C.h>

#define row_1 30
#define row_2 31
#define row_3 32
#define row_4 33
#define row_5 34
#define col_1 22
#define col_2 23
#define col_3 24
#define col_4 25
#define col_5 26
#define col_6 27
#define col_7 28
#define col_8 29

int temp=0,tempold=1;

LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup() {

  • lcd.init(); // initialize the lcd*
  • // Print a message to the LCD.*
  • lcd.backlight();*
  • lcd.begin(16, 2);*
  • lcd.print("Hello, world!");*
  • pinMode(row_1,OUTPUT);*
  • pinMode(row_2,OUTPUT);*
  • pinMode(row_3,OUTPUT);*
  • pinMode(row_4,OUTPUT);*
  • pinMode(row_5,OUTPUT);*
  • pinMode(col_1,INPUT);*
  • pinMode(col_2,INPUT);*
  • pinMode(col_3,INPUT);*
  • pinMode(col_4,INPUT);*
  • pinMode(col_5,INPUT);*
  • pinMode(col_6,INPUT);*
  • pinMode(col_7,INPUT);*
  • pinMode(col_8,INPUT);*
    }

void loop() {

  • temp=0;*

  • digitalWrite(col_1, 1); digitalWrite(col_2, 0); digitalWrite(col_3, 0); digitalWrite(col_4, 0); digitalWrite(col_5, 0); digitalWrite(col_6, 0); digitalWrite(col_7, 0); digitalWrite(col_8, 0);*

  • if (digitalRead(row_1)==1) temp=1;*

  • if (digitalRead(row_2)==1) temp=2;*

  • if (digitalRead(row_3)==1) temp=3;*

  • if (digitalRead(row_4)==1) temp=4;*

  • if (digitalRead(row_5)==1) temp=5;*

  • digitalWrite(col_1, 0); digitalWrite(col_2, 1); digitalWrite(col_3, 0); digitalWrite(col_4, 0); digitalWrite(col_5, 0); digitalWrite(col_6, 0); digitalWrite(col_7, 0); digitalWrite(col_8, 0);*

  • if (digitalRead(row_1)==1) temp=6;*

  • if (digitalRead(row_2)==1) temp=7;*

  • if (digitalRead(row_3)==1) temp=8;*

  • if (digitalRead(row_4)==1) temp=9;*

  • if (digitalRead(row_5)==1) temp=10;*

  • digitalWrite(col_1, 0); digitalWrite(col_2, 0); digitalWrite(col_3, 1); digitalWrite(col_4, 0); digitalWrite(col_5, 0); digitalWrite(col_6, 0); digitalWrite(col_7, 0); digitalWrite(col_8, 0);*

  • if (digitalRead(row_1)==1) temp=11;*

  • if (digitalRead(row_2)==1) temp=12;*

  • if (digitalRead(row_3)==1) temp=13;*

  • if (digitalRead(row_4)==1) temp=14;*

  • if (digitalRead(row_5)==1) temp=15;*

  • digitalWrite(col_1, 0); digitalWrite(col_2, 0); digitalWrite(col_3, 0); digitalWrite(col_4, 1); digitalWrite(col_5, 0); digitalWrite(col_6, 0); digitalWrite(col_7, 0); digitalWrite(col_8, 0);*

  • if (digitalRead(row_1)==1) temp=16;*

  • if (digitalRead(row_2)==1) temp=17;*

  • if (digitalRead(row_3)==1) temp=18;*

  • if (digitalRead(row_4)==1) temp=19;*

  • if (digitalRead(row_5)==1) temp=20;*

  • digitalWrite(col_1, 0); digitalWrite(col_2, 0); digitalWrite(col_3, 0); digitalWrite(col_4, 0); digitalWrite(col_5, 1); digitalWrite(col_6, 0); digitalWrite(col_7, 0); digitalWrite(col_8, 0);*

  • if (digitalRead(row_1)==1) temp=21;*

  • if (digitalRead(row_2)==1) temp=22;*

  • if (digitalRead(row_3)==1) temp=23;*

  • if (digitalRead(row_4)==1) temp=24;*

  • if (digitalRead(row_5)==1) temp=25;*

  • digitalWrite(col_1, 0); digitalWrite(col_2, 0); digitalWrite(col_3, 0); digitalWrite(col_4, 0); digitalWrite(col_5, 0); digitalWrite(col_6, 1); digitalWrite(col_7, 0); digitalWrite(col_8, 0);*

  • if (digitalRead(row_1)==1) temp=26;*

  • if (digitalRead(row_2)==1) temp=27;*

  • if (digitalRead(row_3)==1) temp=28;*

  • if (digitalRead(row_4)==1) temp=29;*

  • if (digitalRead(row_5)==1) temp=30;*

  • digitalWrite(col_1, 0); digitalWrite(col_2, 0); digitalWrite(col_3, 0); digitalWrite(col_4, 0); digitalWrite(col_5, 0); digitalWrite(col_6, 0); digitalWrite(col_7, 1); digitalWrite(col_8, 0);*

  • if (digitalRead(row_1)==1) temp=31;*

  • if (digitalRead(row_2)==1) temp=32;*

  • if (digitalRead(row_3)==1) temp=33;*

  • if (digitalRead(row_4)==1) temp=34;*

  • if (digitalRead(row_5)==1) temp=35;*

  • digitalWrite(col_1, 0); digitalWrite(col_2, 0); digitalWrite(col_3, 0); digitalWrite(col_4, 0); digitalWrite(col_5, 0); digitalWrite(col_6, 0); digitalWrite(col_7, 0); digitalWrite(col_8, 1);*

  • if (digitalRead(row_1)==1) temp=36;*

  • if (digitalRead(row_2)==1) temp=37;*

  • if (digitalRead(row_3)==1) temp=38;*

  • if (digitalRead(row_4)==1) temp=39;*

  • if (digitalRead(row_5)==1) temp=40;*

  • if (temp!=tempold)*

  • {*

  • lcd.setCursor(0, 1); lcd.print(" ");*

  • lcd.setCursor(0, 1); lcd.print(temp);*

  • tempold=temp;*

  • }*

}

You’ll get to the same finish line way sooner if you take a detour onto the fast lane…

and go off to learn about array variables in C/C++.

Srsly, it is not that hard and would make finding errors in this way easier. Anyone will be way more willing to help you do that than to slog through this code.

That you could write it speaks to your effort level capacity, apply that to learning just a bit more, again, I can practically guarantee you’ll finish sooner, unless someone gets in there with you.

The knowledge will serve you now and for the rest of you life.

a7

What does your matrix and wiring look like?

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

While I agree with @alto777 and also prefer to write my own code, you may want to just google

"arduino keyboard matrix library"

and take the modern approach of getting on with the project rapidly.

You'll still have to deal with arrays, but you can probably bluff your way though that.

No one will think less of you. Where would be without libraries? :wink:

Or google, for that matter.

a7

You have not provided sufficient information for anyone to give a good answer, but my guess would be that you do not have anything pulling the inputs to LOW when no keys are pressed. A floating input pin will randomly read HIGH or LOW.

alto777:
While I agree with @alto777 and also prefer to write my own code, you may want to just google

"arduino keyboard matrix library"

and take the modern approach of getting on with the project rapidly.

You'll still have to deal with arrays, but you can probably bluff your way though that.

No one will think less of you. Where would be without libraries? :wink:

a7

The keypad matrix library would have been my recommendation, but if the OP is interested in learning then its better to do it themselves the first time.

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