Part 1
Basically it checks for when the button has changed state and if it has been clicked, it sends a note on message, and if it has been released, it sends a note off message. This is done multiple times in this case.
Part 2
Code:
int potValue = 0; int button1Pin = 33; int button2Pin = 34; int button3Pin = 35; int button4Pin = 36; bool button1State = LOW; bool lastButton1State = LOW; bool button2State = LOW; bool lastButton2State = LOW; bool button3State = LOW; bool lastButton3State = LOW; bool button4State = LOW; bool lastButton4State = LOW; int controlState = 0; int lastControlState = 0; void setup() { Serial.begin(9600); pinMode(button1Pin, INPUT); pinMode(button2Pin, INPUT); pinMode(button3Pin, INPUT); pinMode(button4Pin, INPUT); } void loop() { usbMIDI.read(); lastButton1State = button1State; button1State = digitalRead(button1Pin); lastButton2State = button2State; button2State = digitalRead(button2Pin); lastButton3State = button3State; button3State = digitalRead(button3Pin); lastButton4State = button4State; button4State = digitalRead(button4Pin); if(lastButton1State == LOW && button1State == HIGH) { usbMIDI.sendNoteOn(60,127,1); delay(5); } else if (lastButton1State == HIGH && button1State == LOW) { usbMIDI.sendNoteOff(60,0,1); delay(5); } if(lastButton2State == LOW && button2State == HIGH) { usbMIDI.sendNoteOn(62,127,1); delay(5); } else if (lastButton2State == HIGH && button2State == LOW) { usbMIDI.sendNoteOff(62,0,1); delay(5); } if(lastButton3State == LOW && button3State == HIGH) { usbMIDI.sendNoteOn(63,127,1); delay(5); } else if (lastButton3State == HIGH && button3State == LOW) { usbMIDI.sendNoteOff(63,0,1); delay(5); } if(lastButton4State == LOW && button4State == HIGH) { usbMIDI.sendNoteOn(65,127,1); delay(5); } else if (lastButton4State == HIGH && button4State == LOW) { usbMIDI.sendNoteOff(65,0,1); delay(5); } lastControlState = controlState; controlState = map(analogRead(A13), 0, 1023, 0, 127); controlState = constrain(controlState, 0, 127); if (lastControlState != controlState) { usbMIDI.sendControlChange(1, controlState, 1); Serial.println(controlState); } }
0 Comments