Skip to content

Instantly share code, notes, and snippets.

@companje
Created June 10, 2014 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save companje/f769248e4a22ad156f62 to your computer and use it in GitHub Desktop.
Save companje/f769248e4a22ad156f62 to your computer and use it in GitHub Desktop.
ButtonMatrix for Arduino without resistors or multiplexer chip
const int ROWS=3;
const int COLS=5;
bool buttons[ROWS][COLS];
int rowPins[] = {2,3,4};
int colPins[] = {5,6,7,8,9};
void setup() {
Serial.begin(9600);
for (int i=0; i<ROWS; i++) {
pinMode(rowPins[i],OUTPUT); //row pins are OUTPUT
}
for (int i=0; i<COLS; i++) {
pinMode(colPins[i],INPUT); //col pins are INPUT
digitalWrite(colPins[i],HIGH); //use internal pull-up
}
}
void loop() {
for (int row=0; row<ROWS; row++) {
for (int i=0; i<ROWS; i++) {
digitalWrite(rowPins[i],i!=row); //active row LOW, others HIGH
}
for (int col=0; col<COLS; col++) {
Serial.print(digitalRead(colPins[col]));
Serial.print(" ");
}
Serial.print("- ");
}
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment