This will output 6 MIDI note on and off events depending on 6 switches.
the instrument i am building only needs 6 velocity switches which correspond to guitar strings (maybe more on that later, its basically a souped up sustainer guitar with hexaphonic control)
anyways i have breadboarded 2 switches just for testing, Ill add the rest of them tomorrow.
It outputs the number of milliseconds between break and make quite nicely, even if i press them both at once.
what i did notice though is that once i added the code for reading the additional pins, the measured time went up by a couple of milliseconds
(i could get 1 MS by "playing" really fast with only 1 switch but with code added to read all 12 pins it seems i can only get a minimum of about 3 milliseconds)
maybe it doesnt really matter, i will have to look up how fast real MIDI keyboards respond to velocity.
you must bear with me on this, It is my very first arduino project, normally i just code maxscript at work ![]()
anyways here is what i have so far.
/*
MIDI velocity switch detector
6 2 way switches are connected with the common pin to ground. the normally on position is the "a" button
and the normally open one is the "b"
after the switch is pressed the time is measured betwen the breaking of the a contact to the making of the b contact
the result will be output to a MIDI note on event.
when the b contact is broken a MIDI note off event will be sent.
Switches are Omron D3C-2220 with common grounded and arduinos internal pullup resistors used.
If it can be fast enough the analog inputs will be used to send MIDI CC changes.
*/
// this constant won't change:
const int buttonPin1b = 2;
const int buttonPin1a = 3; // the pin that the switches are attached
const int buttonPin2b = 4;
const int buttonPin2a = 5;
const int buttonPin3b = 6;
const int buttonPin3a = 7;
const int buttonPin4b = 8;
const int buttonPin4a = 9;
const int buttonPin5b = 10;
const int buttonPin5a = 11;
const int buttonPin6b = 12;
const int buttonPin6a = 13;
// Variables will change:
//int buttonPushCounter1a = 0; // counter for the number of button presses
//int buttonPushCounter1b = 0; // counter for the number of button presses
int buttonState1a = 0, buttonState2a = 0,buttonState3a = 0,buttonState4a = 0,buttonState5a = 0,buttonState6a = 0; // current state of the button
int lastButtonState1a = 0, lastButtonState2a = 0,lastButtonState3a = 0,lastButtonState4a = 0,lastButtonState5a = 0,lastButtonState6a = 0; // previous state of the button
int buttonState1b = 0, buttonState2b = 0,buttonState3b = 0,buttonState4b = 0,buttonState5b = 0,buttonState6b = 0; // current state of the button
int lastButtonState1b = 0, lastButtonState2b = 0, lastButtonState3b = 0, lastButtonState4b = 0, lastButtonState5b = 0, lastButtonState6b = 0; // previous state of the button
int time1 = 0,time2 = 0,time3 = 0,time4 = 0,time5 = 0,time6 = 0;
int ready1 = 0, ready2 = 0, ready3 = 0, ready4 = 0, ready5 = 0, ready6 = 0;
void setup() {
for (int pin=2; pin <= 13; ++pin) {
pinMode (pin, INPUT);
digitalWrite(pin, HIGH); // initialize switch pins and enable pull up resistor
}
Serial.begin(9600);
}
void loop() {
// read the switches input pins:
buttonState1a = digitalRead(buttonPin1a);
buttonState1b = digitalRead(buttonPin1b);
buttonState2a = digitalRead(buttonPin2a);
buttonState2b = digitalRead(buttonPin2b);
buttonState3a = digitalRead(buttonPin3a);
buttonState3b = digitalRead(buttonPin3b);
buttonState4a = digitalRead(buttonPin4a);
buttonState4b = digitalRead(buttonPin4b);
buttonState5a = digitalRead(buttonPin5a);
buttonState5b = digitalRead(buttonPin5b);
buttonState6a = digitalRead(buttonPin6a);
buttonState6b = digitalRead(buttonPin6b);
//----------------------------------------SWITCH1------------------------------- 1
// compare the buttonState to its previous state
if (buttonState1a != lastButtonState1a) {
if (buttonState1a == HIGH) {
time1 = millis();
ready1 = 1; //prevent double hits on second switch
// if the current state is HIGH then the switch
// went from on to off: (normally closed to ground, pullup resister)
}
// save the current state as the last state,
//for next time through the loop
lastButtonState1a = buttonState1a;
}
if (buttonState1b != lastButtonState1b) {
if (buttonState1b == LOW) {
if (ready1 == 1){
time1 = (millis() - time1);
Serial.println(time1); //this will be mapped to velocity 0-127 and send the MIDI output
ready1 = 0;
}
}
else {
Serial.println("noteoff SWITCH 1"); //MIDI NOTE OFF GOS HERE
}
lastButtonState1b = buttonState1b;
}
//----------------------------------------SWITCH2------------------------------- 2
if (buttonState2a != lastButtonState2a) {
if (buttonState2a == HIGH) {
time2 = millis();
ready2 = 1;
}
lastButtonState2a = buttonState2a;
}
if (buttonState2b != lastButtonState2b) {
if (buttonState2b == LOW) {
if (ready2 == 1){
time2 = (millis() - time2);
Serial.println(time2); //this is to be mapped to velocity 0-127 and send the MIDI output
ready2 = 0;
}
}
else {
Serial.println("noteoff SWITCH 2"); //MIDI NOTE OFF GOS HERE
}
lastButtonState2b = buttonState2b;
}
//----------------------------------------SWITCH3------------------------------- 3
//----------------------------------------SWITCH4------------------------------- 4
//----------------------------------------SWITCH5------------------------------- 5
//----------------------------------------SWITCH6------------------------------- 6
}