Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ############## INO FILE ##############
- #include "Arduino.h"
- #include <midi.h>
- #include <controller.h>
- // Function declarations:
- void updateButtons();
- MIDI_CREATE_DEFAULT_INSTANCE();
- //************************************************************
- //***SET THE NUMBER OF CONTROLS USED**************************
- //************************************************************
- byte NUMBER_BUTTONS = 2;
- //************************************************************
- //***DEFINE WHAT THE BUTTONS SHOULD DO************************
- //***Button (Pin Number, Command, Note Number, Channel, Debounce Time)
- //*** Command parameter 0=NOTE 1=CC 2=Toggle CC 3=PC **
- //************************************************************
- Button but1 (2, 3, 19, 1, 5); //on pin 2, PC command, pc = 19, MIDI channel 1, 5ms debounce time --> Wes Patch
- Button but2 (3, 3, 18, 1, 5); //on pin 3, PC command, pc = 19, MIDI channel 1, 5ms debounce time --> Foo Fighters Patch
- //************************************************************
- //*** Add all the buttons to a button array
- Button *BUTTONS[] {&but1, &but2};
- void setup()
- {
- MIDI.begin(MIDI_CHANNEL_OFF);
- Serial.begin(9600);
- }
- void loop()
- {
- if (NUMBER_BUTTONS != 0) updateButtons();
- }
- // Functions:
- void updateButtons () {
- // cycle through BUTTONS array
- for (int i = 0 ; i < NUMBER_BUTTONS ; i++) {
- byte message = BUTTONS[i]->getValue();
- // if button is pressed:
- if (message == 0) {
- switch (BUTTONS[i]->Bcommand) {
- case 0: // Note
- MIDI.sendNoteOn(BUTTONS[i]->Bvalue, 127, BUTTONS[i]->Bchannel);
- break;
- case 1: // CC
- MIDI.sendControlChange(BUTTONS[i]->Bvalue, 127, BUTTONS[i]->Bchannel);
- break;
- case 2: // Toggle CC
- if (BUTTONS[i]->Btoggle == 0) {
- MIDI.sendControlChange(BUTTONS[i]->Bvalue, 127, BUTTONS[i]->Bchannel);
- BUTTONS[i]->Btoggle = 1;
- } else if (BUTTONS[i]->Btoggle == 1) {
- MIDI.sendControlChange(BUTTONS[i]->Bvalue, 0, BUTTONS[i]->Bchannel);
- BUTTONS[i]->Btoggle = 0;
- }
- break;
- case 3: // PC
- MIDI.sendProgramChange(BUTTONS[i]->Bvalue, BUTTONS[i]->Bchannel);
- Serial.print("Sending PC Value: ");
- Serial.print(BUTTONS[i]->Bvalue);
- Serial.print(" on MIDI channel: ");
- Serial.println(BUTTONS[i]->Bchannel);
- break;
- }
- }
- // if button is unpressed
- if (message == 1) {
- switch (BUTTONS[i]->Bcommand) {
- case 0: //Note released
- MIDI.sendNoteOff(BUTTONS[i]->Bvalue, 0, BUTTONS[i]->Bchannel);
- break;
- case 1:
- MIDI.sendControlChange(BUTTONS[i]->Bvalue, 0, BUTTONS[i]->Bchannel);
- break;
- }
- }
- }
- }
- ############## HEADER FILE ##############
- #ifndef Controller_h
- #define Controller_h
- #include <Arduino.h>
- //Button (Pin Number, Command, Note Number, Channel, Debounce Time)
- class Button {
- public:
- Button(byte pin, int command, byte value, byte channel, byte debounce);
- byte getValue();
- int Bcommand;
- byte Bvalue;
- byte Bchannel;
- byte Btoggle;
- private:
- byte _pin;
- byte _value;
- int _command;
- byte _debounce;
- unsigned long _time;
- byte _status;
- byte _last;
- };
- #endif
- ############## CPP FILE ##############
- #include "controller.h"
- Button::Button(byte pin, int command, byte value, byte channel, byte debounce) {
- _pin = pin;
- _command = command;
- _value = value;
- _debounce = debounce;
- Bvalue = value;
- Bcommand = command;
- Bchannel = channel;
- Btoggle = 0;
- _status = 0b00000010;
- _last = 1;
- }
- byte Button::getValue() {
- // If BUSY bit is not set - read button
- if (bitRead(_status, 0) == false) { // if BUSY (last bit) is false
- if (digitalRead(_pin) == _last) { // if same as last state - exit
- return 2;
- }
- }
- // If NEW Bit set - Key just pressed
- if (bitRead(_status, 1) == true) { // if NEW (second last bit) is true
- bitSet (_status, 0); // set BUSY (last bit) to true
- bitClear(_status, 1); // set NEW (second last bit) to false
- _time = millis();
- return 200;
- }
- // Check if debounce time has passed. If not, exit
- if (millis() - _time < _debounce ) {
- return 100;
- }
- // Debounce time has passed now. Read pin to see if still set the same
- // If it has changed back - assume false alarm
- if (digitalRead(_pin) == _last) {
- bitClear(_status, 0); // set BUSY to false
- bitSet(_status, 1); // set NEW to true
- return 255;
- }
- // If this point is reached, event is valid. return event type
- else {
- bitClear(_status, 0); // set BUSY to false
- bitSet(_status, 1); // set NEW to true
- _last = ((~_last) & 0b00000001); // invert _last
- return _last;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement