Hello,
I've searched the whole internet soon I think, but no answers out there. I'm trying to make toggle buttons to send data only when released, and it would be a good plan to make it not send anything while pressed to not overflood the midi protocol I'm using.
I think that I should be able to use pretty much the same code as in mode 1 with the temporary reading, since it sends only when released. But it's getting confusing when it's a toggle button as in mode 4.
The temporary reading are interfering with the buttonstate i guess?
Do you see my idea of how to solve it? What am I missing?
Original:
//MULTIPLEXER 5
//DIGITAL IN FOR BUTTONS ________________________________
for (int i = 0; i < 16; i++) { //loop through 16 mux channels
if (toReadDigitalMux5[i] == 1) { //we read the choosen mux channel input
tempDigitalRead5 = readMux5(i);
if (pushbuttonMode[i] == 1 && tempDigitalRead5 != buttonState[i]) { //___NORMAL MODE (1)
delay(20); //just a delay for noise to ensure push button was actually hit
if (readMux5(i) == tempDigitalRead5) { //check if pushbutton is still the same
if (tempDigitalRead5 == LOW) { //button pressed, turn note on
midiSend('p', 1, i); //call note on/off function
} else { //button released
midiSend('p', 0, i);
}
buttonState[i] = tempDigitalRead5; //update the state (on or off)
}
} else { //___ALL OTHER MODES (2,3,4)
if (readMux5(i) == LOW && (millis() - timeHit[i]) > pbBounce) { //check bounce time
if (pushbuttonMode[i] == 2) { //mode 2 - only note on
midiSend('p', 1, i);
} else if (pushbuttonMode[i] == 3) { //mode 3 - only note off
midiSend('p', 0, i);
} else { //mode 4 - toggle
if (buttonState[i] == 1) { //on->off
midiSend('p', 0, i);
buttonState[i] = 0;
} else { //off->on
midiSend('p', 1, i);
buttonState[i] = 1;
}
}
timeHit[i] = millis();
}
}
}
}
Edited:
//MULTIPLEXER 5
//DIGITAL IN FOR BUTTONS ________________________________
for (int i = 0; i < 16; i++) { //loop through 16 mux channels
if (toReadDigitalMux5[i] == 1) { //we read the choosen mux channel input
tempDigitalRead5 = readMux5(i);
if (pushbuttonMode[i] == 1 && tempDigitalRead5 != buttonState[i]) { //___NORMAL MODE (1)
delay(20); //just a delay for noise to ensure push button was actually hit
if (readMux5(i) == tempDigitalRead5) { //check if pushbutton is still the same
if (tempDigitalRead5 == LOW) { //button pressed, turn note on
midiSend('p', 1, i); //call note on/off function
} else { //button released
midiSend('p', 0, i);
}
buttonState[i] = tempDigitalRead5; //update the state (on or off)
}
} else { //___ALL OTHER MODES (2,3,4)
if (readMux5(i) == LOW && (millis() - timeHit[i]) > pbBounce) { //check bounce time
if (pushbuttonMode[i] == 2) { //mode 2 - only note on
midiSend('p', 1, i);
} else if (pushbuttonMode[i] == 3) { //mode 3 - only note off
midiSend('p', 0, i);
} else { //mode 4 - toggle
if (readMux5(i) == tempDigitalRead5) { //check if pushbutton is still the same
if (tempDigitalRead5 == LOW) { // if pressed > do nothing
} else { //if released > send data
if (buttonState[i] == 1) { //on->off
midiSend('p', 0, i);
buttonState[i] = 0;
} else { //off->on
midiSend('p', 1, i);
buttonState[i] = 1;
}
}
}
}
timeHit[i] = millis();
}
}
}
}
The complete working code on GitHub, sending data when button is pressed, instead of released:
https://github.com/Imbecillen/midi-controller/blob/master/Teensy_monster_edit_buttons_simple_working.ino