Skip to content
Permalink
Browse files

implement momentary press behaviour

* move sending of MIDI CC and logging to seperate function sendCCandLog()
* state of buttons (pressed, released) is saved in two arrays:
  * buttonsPressedNow -> button state in current cycle
  * buttonsPressedLast -> button state before that
* each cycle we compare if a button is pressed or released by comparing its current state
  with the one backed up in buttonsPressedLast
* expected behaviour:
  * send "on" CC _once_ when button is pressed (value 65)
  * do nothing if it stays pressed
  * send "off" CC _once_ when it's released (value 0)
  • Loading branch information...
J0J0 T
J0J0 T committed Nov 16, 2018
1 parent 029837b commit 1ee5c8eafd26eca593e37ddc5b05adf1b8383ceb
Showing with 51 additions and 11 deletions.
  1. +51 −11 matrixmidi.ino
@@ -57,26 +57,66 @@ void setup()
}
}

// saves the buttons current state. 1=pressed, 0=released
uint8_t buttonsPressedNow[4][3] = {
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0}
};

// button state is backuped up to this array after each cycle where a button press was detected,
// so we can compare if the state of the button has changed on the next cycle
uint8_t buttonsPressedLast[4][3] = {
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0}
};

void sendCCandLog(uint8_t cc_num, uint8_t cc_value, uint8_t _midi_ch, uint8_t _mode)
{
digitalWrite(ledPin, HIGH);
aSerial.vvv().p("button CC: ").pln(cc_num);
aSerial.vvv().pln();
if (mode != 2)
{
delay(5);
MIDI.sendControlChange(cc_num, cc_value, _midi_ch);
delay(250);
}
}

void loop()
{
for (uint8_t col = 0; col < sizeof(outPins); col++)
{
digitalWrite(outPins[col], HIGH);
for (uint8_t row = 0; row < sizeof(inPins); row++)
{
if (digitalRead(inPins[row]) == HIGH) // blink led if inPin is HIGH)
// MOMENTARY PRESS BEHAVIOUR:
// first check what state the button is in
if (digitalRead(inPins[row]) == HIGH)
{
buttonsPressedNow[row][col] = 1;
}
else
{
buttonsPressedNow[row][col] = 0;
}
// then check if it was just pressed or just released
// no else: nothing to do when state didn't change
if (buttonsPressedNow[row][col] > buttonsPressedLast[row][col])
{
sendCCandLog(buttons[row][col], 65, midi_ch, mode); // just pressed
// -> send sth higher than 64
}
else if (buttonsPressedNow[row][col] < buttonsPressedLast[row][col])
{
digitalWrite(ledPin, HIGH);
aSerial.vvv().p("yes, it's high: ").p(col).p(" ").pln(row);
aSerial.vvv().p("button CC: ").pln(buttons[row][col]);
aSerial.vvv().pln();
if (mode != 2)
{
delay(5);
MIDI.sendControlChange(buttons[row][col], 65, midi_ch);
delay(250);
}
sendCCandLog(buttons[row][col], 0, midi_ch, mode); // just released -> send 0
}
// save current button state in ...Last array so we can compare against it on next run
buttonsPressedLast[row][col] = buttonsPressedNow[row][col];
}
digitalWrite(outPins[col], LOW);
}

0 comments on commit 1ee5c8e

Please sign in to comment.
You can’t perform that action at this time.