Hello again!
It was a great feeling, when the arduino sent PC messages to my music program (Gig Performer btw.).
BUT one thing I don't manage and I don't know why: I want to use pin #16 (= internal button #9) as a sort of "momentary shift" command.
By that I mean: when I pressed this button once, the next PC message shall be increased by 10! So I would have an upper register to control another 9 patches in GigPerformer (where the guitar synths will be).
I used the variable "shift" which should be set to 10 when button #9 (pin16) will be pressed. Only the NEXT button pressing shall be increased by 10.
Here's the code:
```cpp
#include <Control_Surface.h>
#include <MIDIUSB.h>
#include <MIDIUSB_Defs.h>
USBMIDI_Interface midi;
/*
BASED ON: Made by Gustavo Silveira, 2023.
- This Sketch reads the Arduino's digital and analog ports and sends (MODIFIED to) ProgramChanges.
*/
/////////////////////////////////////////////
// Choosing your board
// Define your board, choose:
#define ATMEGA32U4 1 // put here the uC you are using, like in the lines above followed by "1", like "ATMEGA328 1", "DEBUG 1", etc.
/////////////////////////////////////////////
// Are you using buttons?
#define USING_BUTTONS 1 // comment if not using buttons
/////////////////////////////////////////////
// BUTTONS
const int N_BUTTONS = 10; // total numbers of buttons
const int BUTTON_ARDUINO_PIN[10] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 16}; // pins of each button connected straight to the Arduino
int buttonCState[N_BUTTONS] = {}; // stores the button current value
int buttonPState[N_BUTTONS] = {}; // stores the button previous value
int button10CState[N_BUTTONS] = {}; // stores the button current value
int button10PState[N_BUTTONS] = {}; // stores the button previous value
// debounce
unsigned long lastDebounceTime[N_BUTTONS] = { 0 }; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
/////////////////////////////////////////////
// MIDI
byte midiCh = 0; // MIDI channel to be used - start with 1 for MIDI.h lib or 0 for MIDIUSB lib
/////////////////////////////////////////////
// SETUP
void setup() {
midi.begin();
Serial.begin(115200); //
// Buttons
// Initialize buttons with pull up resistors
for (int i = 0; i < N_BUTTONS; i++) {
pinMode(BUTTON_ARDUINO_PIN[i], INPUT_PULLUP);
}
}
/////////////////////////////////////////////
// LOOP
void loop() {
buttons();
}
/////////////////////////////////////////////
// BUTTONS
void buttons() {
int shift;
if (digitalRead(BUTTON_ARDUINO_PIN[9]) == LOW) {
int shift = 10;
}
for (int i = 0; i < N_BUTTONS-1; i++) {
buttonCState[i] = digitalRead(BUTTON_ARDUINO_PIN[i]); // read pins from arduino
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (buttonPState[i] != buttonCState[i]) {
lastDebounceTime[i] = millis();
if (buttonCState[i] == LOW) {
// Send a program change message
int p = i + shift + 1;
MIDIAddress program {p, Channel_1};
midi.sendProgramChange(program);
} else {
}
buttonPState[i] = buttonCState[i];
}
}
}
}
/////////////////////////////////////////////
I'm thankful for your insights.
Hans