Arduino Code:
#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);
usbMIDI.sendNoteOn(buttonNumber,127,1);
Serial.println(" pressed");
}
void onRelease(int buttonNumber) {
Serial.print(buttonNumber);
usbMIDI.sendNoteOff(buttonNumber,127,1);
Serial.println(" released");
}
Explain:
Main Program
This program seems to establish each of the buttons individually, and allows them to undergo a particular process, depending on the function “button.process();” which is defined in another part of the program. In this case, it will just print the status of the button in the serial monitor and from what we added, send a particular MIDI note. What is most new to me in this program is the code in the setup section. It seems that another part of the code has established pressed and released states of the buttons (“on press” and “on release” statuses), so that the button will behave in certain ways in these states.
Button.cpp
This programs looks very similar to the button code we used earlier in the semester, that basically codes the button to behave in the same way as a musical keyboard button or key, where it will do something when pressed, and stop only when released. The button of the code, with the “pressHandler” and “releaseHandler” seems to establish the button statuses that can be seen in the main program, but I’m not too sure what they do, and how it’s that different from including just the regular button code in your code.
Button.h
I have no idea what this last part of the code is doing and how it is behaving within this entire code. It seems to be creating the key variables and states for the other code to follow. I’m not sure what public and private means in this context; it seems that all of the public variables can be seen in the main button code, while the private variables can only be seen in the Button.cpp code. Overall, I think this part of the code just creates the variables, and the rest of the code uses them in creative ways to simplify the buttonCode so that is easy to add more buttons, change pin numbers, and add additional functions.