I’m using 8-bit decoder 74HC238. Its outputs are connected to 8 ULN2803 inputs. Using 32 LEDs and 48 buttons. LED and button matrix are sharing the same columns. Button inputs are connected to 4021 shift register. I’m trying to light up all the LEDs and read buttons simultaneously but I can’t really get it to work like it should.
void loop() {
for (int i=0; i<8; i++) {
nextColumn(i);
readButtons(i);
}
void nextColumn(int column) {
switch (column) {
case 0:
digitalWrite(SHARED_PIN_1, LOW);
digitalWrite(SHARED_PIN_1, LOW);
digitalWrite(SHARED_PIN_1, LOW);
break;
case 1:
digitalWrite(SHARED_PIN_1, LOW);
digitalWrite(SHARED_PIN_1, LOW);
digitalWrite(SHARED_PIN_1, HIGH);
break;
case 2:
digitalWrite(SHARED_PIN_1, LOW);
digitalWrite(SHARED_PIN_1, HIGH);
digitalWrite(SHARED_PIN_1, LOW);
break;
case 3:
digitalWrite(SHARED_PIN_1, LOW);
digitalWrite(SHARED_PIN_1, HIGH);
digitalWrite(SHARED_PIN_1, HIGH);
break;
case 4:
digitalWrite(SHARED_PIN_1, HIGH);
digitalWrite(SHARED_PIN_1, LOW);
digitalWrite(SHARED_PIN_1, LOW);
break;
case 5:
digitalWrite(SHARED_PIN_1, HIGH);
digitalWrite(SHARED_PIN_1, LOW);
digitalWrite(SHARED_PIN_1, HIGH);
break;
case 6:
digitalWrite(SHARED_PIN_1, HIGH);
digitalWrite(SHARED_PIN_1, HIGH);
digitalWrite(SHARED_PIN_1, LOW);
break;
case 7:
digitalWrite(SHARED_PIN_1, HIGH);
digitalWrite(SHARED_PIN_1, HIGH);
digitalWrite(SHARED_PIN_1, HIGH);
break;
}
}
void readButtons(int column) {
latchPin();
for (int i=7; i>=0; i--) {
int buttonNumber = i*NUMBER_OF_COLUMNS+column;
digitalWrite(SR_CLOCK_PIN, 0);
delayMicroseconds(0.2);
boolean temp = digitalRead(SR_DATA_PIN);
//button isn't pressed (pull up resistors cause reading to be 1)
if (temp) {
//send note off for regular press if the note on has been sent
if (midiNoteOnSent[buttonNumber] == true) {
//generateNote(i, column, MIDI_NOTE_OFF_CHANNEL);
#ifdef DEBUG
sendMIDImessage(MIDI_NOTE_OFF_CHANNEL, buttonNumber, MIDI_NOTE_VELOCITY);
#else
buttonDebugInfo(buttonNumber, MIDI_NOTE_OFF_CHANNEL);
#endif
midiNoteOnSent[buttonNumber] = false;
//if the midiLongNoteOn has also been sent send note off for that event as well
if (midiLongNoteOnSent[buttonNumber] == true) {
//send MIDI note off
//generateNote(j, i, MIDI_NOTE_OFF_LONG_PRESS_CHANNEL);
//make sure the notes are sent only once while button is released
midiLongNoteOnSent[buttonNumber] = false;
}
}
debounceTimer[buttonNumber] = millis();
}
else {
//button press detected, check if is it a valid reading
if ((millis() - debounceTimer[buttonNumber]) > BUTTON_DEBOUNCE_DELAY) {
//button is really pressed
//if the note on hasn't been sent already, send it
if (midiNoteOnSent[buttonNumber] == false) {
//send MIDI note on
//generateNote(i, column, MIDI_NOTE_ON_CHANNEL);
#ifdef DEBUG
sendMIDImessage(MIDI_NOTE_ON_CHANNEL, buttonNumber, MIDI_NOTE_VELOCITY);
#else
buttonDebugInfo(buttonNumber, MIDI_NOTE_ON_CHANNEL);
#endif
//make sure the note is sent only once while button is pressed
midiNoteOnSent[buttonNumber] = true;
//start measuring time needed to send long-press midi note
longPressButtonTimer[buttonNumber] = millis();
}
//if it's been more than LONG_PRESS_BUTTON_TIMER, send midiLongNoteOn
else if (((millis() - longPressButtonTimer[buttonNumber]) > LONG_PRESS_BUTTON_TIMER) && midiLongNoteOnSent[buttonNumber] == false) {
//send long-press MIDI note on
//generateNote(j, i, MIDI_NOTE_ON_LONG_PRESS_CHANNEL);
//make sure the note is sent only once while button is pressed
midiLongNoteOnSent[buttonNumber] = true;
}
}
}
digitalWrite(SR_CLOCK_PIN, 1);
}
}
void latchPin() {
//pulse the latch pin; set it to 1 to collect parallel data
digitalWrite(SR_LATCH_PIN, 1);
//wait
delayMicroseconds(20);
//set it to 0 to transmit data serially
digitalWrite(SR_LATCH_PIN, 0);
}
void buttonDebugInfo(int buttonNumber, int channel) {
Serial.print("Button: ");
Serial.print(buttonNumber);
Serial.print("\n");
}
So what happens when I press a button is this (using debug mode):
Button: 27
Button: 29
Button: 31
Button: 25
Button: 27
Button: 29
Button: 31
Button: 25
Button: 27
Button: 29
Button: 31
Button: 25
Button: 27
Button: 25
Button: 27
Button: 31
Button: 27
Button: 29