//**************************************************************** // button matrix scanning code with 74hc595 // see: http://www.openmusiclabs.com/learning/digital/input-scanning-matrix/shift-out-mux/index.html //**************************************************************** // define serial speed const uint32_t serialSpeed = 115200; ////Pin connected to DS of 74HC595 const uint8_t dataPin = 2; //Pin connected to ST_CP of 74HC595 const uint8_t latchPin = 3; //Pin connected to SH_CP of 74HC595 const uint8_t clockPin = 4; // input pins for reading button matrix rows const uint8_t rowPin[] = {5, 6, 7}; // defining the size of the button matrix const uint8_t numRows = sizeof(rowPin); const uint8_t numCols = 4; // bit mask for shift register ("activates" columns) uint8_t columnBitMask[numCols]; // button state array uint8_t buttonState[numCols][numRows]; // elpapsed micros for timing elapsedMicros myElapsedMicros = 0; // debouncing methods for rising/falling edge detection, see: // see: http://forum.arduino.cc/index.php?PHPSESSID=9a55td0ms6e7b8okqr9g2e03q2&topic=199630.msg1475393#msg1475393 // see also: https://forum.pjrc.com/threads/40492-Midi-8X8-diode-matrix-vs-16-1-MUX?p=125947&viewfull=1#post125947 #define DEBOUNCE 4 #define DMASK ((1< 250) { myElapsedMicros = 0; // iterate columns for (uint8_t columnCtr = 0; columnCtr < numCols; columnCtr++) { // pull latchPin LOW to keep current state digitalWriteFast(latchPin, LOW); // shift out the bits for the current column; replace by SPI transmit shiftOut(dataPin, clockPin, MSBFIRST, columnBitMask[columnCtr]); // pull latch pin high so that the bitmask is represented on the 74HC595 output pins digitalWriteFast(latchPin, HIGH); // iterate and read the rows for (uint8_t rowCtr = 0; rowCtr < numRows; rowCtr++) { //do something on rising edge if (DRE(digitalReadFast(rowPin[rowCtr]), buttonState[columnCtr][rowCtr])) { Serial.print("RISE "); Serial.print(columnCtr); Serial.print(" "); Serial.println(rowCtr); } //do soemthing on falling edge(same read) if (buttonState[columnCtr][rowCtr] == DF) { Serial.print("FALL "); Serial.print(columnCtr); Serial.print(" "); Serial.println(rowCtr); } } } } }