Aidan Singh
Main code for 4 MIDI-controlling buttons:
#include "Button.h"
Button buttonOne(33, 60);
Button buttonTwo(34, 62);
Button buttonThree(35, 64);
Button buttonFour(36, 65);
void setup() {
Serial.begin(9600);
buttonOne.pressHandler(onPress);
buttonOne.releaseHandler(onRelease);
buttonTwo.pressHandler(onPress);
buttonTwo.releaseHandler(onRelease);
buttonThree.pressHandler(onPress);
buttonThree.releaseHandler(onRelease);
buttonFour.pressHandler(onPress);
buttonFour.releaseHandler(onRelease);
}
void loop() {
buttonOne.process();
buttonTwo.process();
buttonThree.process();
buttonFour.process();
}
void onPress(int buttonNumber) {
Serial.print(buttonNumber);
Serial.println(" pressed");
usbMIDI.sendNoteOn(buttonNumber, 90, 1);
}
void onRelease(int buttonNumber) {
Serial.print(buttonNumber);
Serial.println(" released");
usbMIDI.sendNoteOff(buttonNumber, 0, 1);
}
Speculative explanation of pressCallback and releaseCallback:
In the Button.cpp program, the process() function calls pressCallback with the argument buttonNum at the moment the button is pressed, and calls releaseCallback with the argument buttonNum at the moment the button is released.
Therefore: When a button is pressed, a function (pressCallBack) will run. When a button is released, a function (releaseCallBack) will run.
In the Button.cpp program, the pressHandler function takes a function argument and sets pressCallback to it. The releaseHandler function takes a function argument and sets it to pressCallback to it.
Therefore: pressHandler and releaseHandler set what pressCallback and releaseCallback do respectively.
In practice: In the part_1_button program, buttonOne.pressHandler(onPress) means that the object buttonOne will run the function pressHandler, taking the argument onPress. This function(in the setup) sets the function that happens when button one is pressed(in the loop).
I haven’t done much stuff in C++, as I’ve mainly used Java. Although the syntax is a little foreign, this was a great demo to get me thinking about other things to do with Arduino.