Hi everyone.
So I have been at it making a serious midi controller for a few months. There will be a lot of button switches on this controller and for my prototyping I am using some simple button switches to figure this out.
I am using a Teensy 3.6. I recently was informed about the keypad method of reading buttons switches using the keypad library , which would save me a lot of connections, and I'd like to implement this into my code, although I've never used it before.
I altered the HelloKeypad example for my design of 4X4 rows and columns of buttons. Using the serial monitor I can see that my buttons are working and when they are pressed I can see them listed in the serial monitor.
Here's my code for that
What I need is that when a button is pressed I get a 127 value for a particular midi controller, and when it is not pressed that value is a 0.
Below is the code I tried out.
Right now i am getting a 0 for midi controller number 20, and a 127 for every other midi controller( 21-35) no matter what button I press. And I see a lot of midi information streaming in.
Any suggestions on how to fix this?
I also looked at the EventKeypad example, where there is a switch and each case is accounted for in the code. I was thinking of trying that, but I wanted to ask if anyone has any suggestions first.
Also, I was previously incorporating a 5 millisecond bounce for my button switches. I want to implement that too eventually.
Thank you in advance for any help or suggestions.
Here is what I came up with.
So I have been at it making a serious midi controller for a few months. There will be a lot of button switches on this controller and for my prototyping I am using some simple button switches to figure this out.
I am using a Teensy 3.6. I recently was informed about the keypad method of reading buttons switches using the keypad library , which would save me a lot of connections, and I'd like to implement this into my code, although I've never used it before.
I altered the HelloKeypad example for my design of 4X4 rows and columns of buttons. Using the serial monitor I can see that my buttons are working and when they are pressed I can see them listed in the serial monitor.
Here's my code for that
Code:
//4/26/20
#include <Bounce.h>
#include <Adafruit_NeoPixel.h>
#include <Keypad.h>
#include <Wire.h> // wire library for LED matrix
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3', '4'},
{'5', '6', '7', '8'},
{'9', 'a', 'b', 'c'},
{'d', 'e', 'f', 'g'}
};
byte rowPins[ROWS] = {32, 31, 30, 29}; //teensy pins to connect to the row pinouts of the keypad
byte colPins[COLS] = {28, 27, 12, 11,}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.println(key);
}
}
What I need is that when a button is pressed I get a 127 value for a particular midi controller, and when it is not pressed that value is a 0.
Below is the code I tried out.
Right now i am getting a 0 for midi controller number 20, and a 127 for every other midi controller( 21-35) no matter what button I press. And I see a lot of midi information streaming in.
Any suggestions on how to fix this?
I also looked at the EventKeypad example, where there is a switch and each case is accounted for in the code. I was thinking of trying that, but I wanted to ask if anyone has any suggestions first.
Also, I was previously incorporating a 5 millisecond bounce for my button switches. I want to implement that too eventually.
Thank you in advance for any help or suggestions.
Here is what I came up with.
Code:
//4/26/20
#include <Bounce.h>
#include <Adafruit_NeoPixel.h>
#include <Keypad.h>
#include <Wire.h> // wire library for LED matrix
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3', '4'},
{'5', '6', '7', '8'},
{'9', 'a', 'b', 'c'},
{'d', 'e', 'f', 'g'}
};
byte rowPins[ROWS] = {32, 31, 30, 29}; //teensy pins to connect to the row pinouts of the keypad
byte colPins[COLS] = {28, 27, 12, 11,}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
const int channel = 1;
const int MIDIchannel = 1; // this will only apply to midi channel 1
const int controllerD20 = 20; // toggle, control change 14
const int controllerD21 = 21; // Button, control change 15
const int controllerD22 = 22; //
const int controllerD23 = 23;
const int controllerD24 = 24; //
const int controllerD25 = 25;
const int controllerD26 = 26;
const int controllerD27 = 27;
const int controllerD28 = 28;
const int controllerD29 = 29;
const int controllerD30 = 30;
const int controllerD31 = 31;
const int controllerD32 = 32;
const int controllerD33 = 33;
const int controllerD34 = 34;
const int controllerD35 = 35;
Bounce button0 = Bounce(0, 5);
Bounce button1 = Bounce(1, 5); // 5 = 5 ms debounce time
Bounce button2 = Bounce(2, 5); // which is appropriate for good
Bounce button3 = Bounce(3, 5); // quality mechanical pushbuttons
Bounce button4 = Bounce(4, 5);
Bounce button5 = Bounce(5, 5); // if a button is too "sensitive"
Bounce button6 = Bounce(6, 5); // to rapid touch, you can
Bounce button7 = Bounce(7, 5); // increase this time.
Bounce button8 = Bounce(8, 5);
Bounce button9 = Bounce(9, 5);
Bounce button10 = Bounce(10, 5);
Bounce button11 = Bounce(11, 5);
Bounce button12 = Bounce(12, 5); // goes out of order because other pins double as analog inputs
Bounce button24 = Bounce(24, 5);
Bounce button25 = Bounce(25, 5);
Bounce button26 = Bounce(26, 5);
void setup() {
// pinMode(latch, OUTPUT);
// digitalWrite(latch, LOW);
// pinMode(clock, OUTPUT);
// digitalWrite(clock, LOW);
// pinMode(data, OUTPUT);
// digitalWrite(data, LOW);
// Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key == 1) {
usbMIDI.sendControlChange(controllerD20, 127, channel);
}
if (key != 1) {
usbMIDI.sendControlChange(controllerD20, 0, channel);
}
if (key = 2) {
usbMIDI.sendControlChange(controllerD21, 127, channel);
}
if (key != 2) {
usbMIDI.sendControlChange(controllerD21, 0, channel);
}
if (key = 3) {
usbMIDI.sendControlChange(controllerD22, 127, channel);
}
if (key != 3) {
usbMIDI.sendControlChange(controllerD22, 0, channel);
}
if (key = 4) {
usbMIDI.sendControlChange(controllerD23, 127, channel);
}
if (key != 4) {
usbMIDI.sendControlChange(controllerD24, 0, channel);
}
if (key = 5) {
usbMIDI.sendControlChange(controllerD24, 127, channel);
}
if (key != 5) {
usbMIDI.sendControlChange(controllerD24, 0, channel);
}
if (key = 6) {
usbMIDI.sendControlChange(controllerD25, 127, channel);
}
if (key != 6) {
usbMIDI.sendControlChange(controllerD25, 0, channel);
}
if (key = 7) {
usbMIDI.sendControlChange(controllerD26, 127, channel);
}
if (key != 7) {
usbMIDI.sendControlChange(controllerD26, 0, channel);
}
if (key = 8) {
usbMIDI.sendControlChange(controllerD27, 127, channel);
}
if (key != 8) {
usbMIDI.sendControlChange(controllerD27, 0, channel);
}
if (key = 9) {
usbMIDI.sendControlChange(controllerD28, 127, channel);
}
if (key != 9) {
usbMIDI.sendControlChange(controllerD28, 0, channel);
}
if (key = 'a') {
usbMIDI.sendControlChange(controllerD29, 127, channel);
}
if (key != 'a') {
usbMIDI.sendControlChange(controllerD29, 0, channel);
}
if (key = 'b') {
usbMIDI.sendControlChange(controllerD30, 127, channel);
}
if (key != 'b') {
usbMIDI.sendControlChange(controllerD30, 0, channel);
}
if (key = 'c') {
usbMIDI.sendControlChange(controllerD31, 127, channel);
}
if (key != 'c') {
usbMIDI.sendControlChange(controllerD31, 0, channel);
}
if (key = 'd') {
usbMIDI.sendControlChange(controllerD32, 127, channel);
}
if (key != 'd') {
usbMIDI.sendControlChange(controllerD32, 0, channel);
}
if (key = 'e') {
usbMIDI.sendControlChange(controllerD33, 127, channel);
}
if (key != 'e') {
usbMIDI.sendControlChange(controllerD33, 0, channel);
}
if (key = 'f') {
usbMIDI.sendControlChange(controllerD34, 127, channel);
}
if (key != 'f') {
usbMIDI.sendControlChange(controllerD34, 0, channel);
}
if (key = 'g') {
usbMIDI.sendControlChange(controllerD35, 127, channel);
}
if (key != 'g') {
usbMIDI.sendControlChange(controllerD35, 0, channel);
}
// MIDI Controllers should discard incoming MIDI messages.
// http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash
while (usbMIDI.read()) {
// ignore incoming messages
}
}