#include "Button.h"

Button buttonOne(33, 1);
Button buttonTwo(34, 3);
Button buttonThree(35, 5);
Button buttonFour(36, 6);

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(59 + buttonNumber, 127, 1);
}

void onRelease(int buttonNumber) {
  Serial.print(buttonNumber);
  Serial.println(" released");

  usbMIDI.sendNoteOff(59 + buttonNumber, 0, 1);
  
}

Pretty much in void setup() we’re telling the button object which voids to call when it is pressed and when it is released respectively. Pretty much we’re using a function as a variable in order to allow the class to call whichever function is specified. You could theoretically use whatever function you want as the callback, it just makes sense to use something which is related to the event that triggers it.


0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.