I'm using a pro micro board to run this sketch.
I am currently utilizing the MIDIUSB library.
Here is a description of my schematic.
all the switches for the main switches are connected to pin's 2,3,5,6,7.
with the ground connected to the centre pin and the main pin connected to one side of the switch,
the bank switch is connected in the same manner to pin 4,
the pots are connected right to left from the back. "I'm not great at remembering which pin numbers which." #
so right to left for the back
left=ground centre=analog pin, right=vcc
the analog pins are A0,A1,A2,A3,A10,
I'll quickly explain its functions.
It has five mechanical switches to send cc messages as momentary commands. The cc messages. need to work on rising and falling edges to trigger a single momentary cc message. which should be at the value of 127 value else 0.
The five switches should work with a bank switch of which, at the moment id like 2 of A and B. Operated with a separate 6ith, banks switch. This is also a mechanical switch, which id like to trigger on edge detection. I would like to have a midi cc code sent on that switch also but Gpt seems to not quite get what I'm after. this is so it could do double duty if needed.
Then there are five potentiometers which are to bank the cc messages also sending different cc codes depending on banks.
I've tried to add an LED on pin 8 to be set to high on bank A, I also set the onboard led to flash on bank A. This doesn't appear to work properly for some reason not sure what to do to fix it.
On to my problems with my code
I have the board flashed with my code but have major issues with the midi device. The device shows up but I can't seem to get anything out of the switches re cc messages.
I tried adding in a midi cc message length to the code, to try to make sure messages would get through.
I have also tried to play around with pot thresholds and debounce delays to fix my issues with this.
The pots only register every so often and the switches don't appear to work at all. Can someone help me fix this code?
I've used chat gpt to write this code thus far.
I understand a small amount about setting up these devices as I have done one before with a lot of help, but couldn't write the code myself.
I know just enough to be dangerous as they say.
Please help here's the code can someone look at it? To see if they can see any problems with the setup of the switches,
and generally help me get the whole thing working.
I know don't know if the code is not set up right to send cc messages in the way I've described on the switches or if it's something with the debounce and other things. Please help me to understand if it's something wrong with the code, and or fix it for me. I don't have a clue at the moment.
#include <MIDIUSB.h>
// MIDIUSB functions
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, static_cast<uint8_t>(0x90 | channel), pitch, velocity};
MidiUSB.sendMIDI(noteOn);
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, static_cast<uint8_t>(0x80 | channel), pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}
void controlChange(byte channel, byte control, byte value, byte signalLength) {
midiEventPacket_t event = {0x0B, static_cast<uint8_t>(0xB0 | channel), control, value};
MidiUSB.sendMIDI(event);
}
// Potentiometer variables
const int numPotentiometers = 5;
const int potentiometerPins[numPotentiometers] = {A0, A1, A2, A3, A10};
const byte potentiometerCCNumbersA[numPotentiometers] = {25, 26, 27, 28, 29};
const byte potentiometerCCNumbersB[numPotentiometers] = {35, 36, 37, 38, 39};
const int potentiometerDebounceDelay = 0;
const float potentiometerMovementThreshold = 6; // Minimum movement threshold as a percentage
const int potentiometerDelayTime = 30; // Delay time in milliseconds for sending CC message
// Switch variables
const int numSwitches = 5;
const int switchPins[numSwitches] = {2, 3, 5, 6, 7};
const byte switchCCNumbersA[numSwitches] = {19, 20, 21, 22, 23};
const byte switchCCNumbersB[numSwitches] = {30, 31, 32, 33, 34};
const int switchDebounceDelay = 0; // Adjust debounce delay as needed
// Bank switch variables
const int bankSwitchPin = 4; // Pin for the bank switch
const byte switchCCSignalLength = 500; // CC signal length (max value for a momentary CC message)
const byte bankLED = 8; // Pin for the bank LED
byte switchStates[numSwitches] = {0};
byte previousSwitchStates[numSwitches] = {0};
int potentiometerStates[numPotentiometers] = {0};
int previousPotentiometerStates[numPotentiometers] = {0};
byte bankState = LOW;
byte previousBankState = LOW;
void readPotentiometerValues() {
for (int i = 0; i < numPotentiometers; i++) {
int value = analogRead(potentiometerPins[i]);
float movementPercentage = abs(value - previousPotentiometerStates[i]) / 1023.0;
if (movementPercentage >= potentiometerMovementThreshold) {
delay(potentiometerDebounceDelay); // Debounce the potentiometer reading
value = analogRead(potentiometerPins[i]); // Read the value again after debounce
movementPercentage = abs(value - previousPotentiometerStates[i]) / 1023.0;
if (movementPercentage >= potentiometerMovementThreshold) {
previousPotentiometerStates[i] = value;
if (digitalRead(bankSwitchPin) == HIGH) {
controlChange(0, potentiometerCCNumbersA[i], map(value, 0, 1023, 0, 127), switchCCSignalLength);
} else {
controlChange(0, potentiometerCCNumbersB[i], map(value, 0, 1023, 0, 127), switchCCSignalLength);
}
delay(potentiometerDelayTime);
}
}
}
}
void readSwitchStates() {
for (int i = 0; i < numSwitches; i++) {
byte state = digitalRead(switchPins[i]);
if (state != previousSwitchStates[i]) {
delay(switchDebounceDelay); // Debounce the switch reading
state = digitalRead(switchPins[i]); // Read the state again after debounce
if (state != previousSwitchStates[i]) {
// High on rising edge
if (state == HIGH) {
if (digitalRead(bankSwitchPin) == HIGH) {
controlChange(0, switchCCNumbersA[i], 127, switchCCSignalLength);
} else {
controlChange(0, switchCCNumbersB[i], 127, switchCCSignalLength);
}
}
// Low on falling edge
else {
if (digitalRead(bankSwitchPin) == HIGH) {
controlChange(0, switchCCNumbersA[i], 0, switchCCSignalLength);
} else {
controlChange(0, switchCCNumbersB[i], 0, switchCCSignalLength);
}
}
switchStates[i] = state;
}
}
previousSwitchStates[i] = state;
}
}
void readBankSwitchState() {
byte state = digitalRead(bankSwitchPin);
if (state != previousBankState) {
delay(switchDebounceDelay); // Debounce the switch reading
state = digitalRead(bankSwitchPin); // Read the state again after debounce
if (state != previousBankState) {
if (state == HIGH) {
controlChange(0, 35, 127, switchCCSignalLength); // Send CC message on bank switch rising edge
} else {
controlChange(0, 35, 0, switchCCSignalLength); // Send CC message on bank switch falling edge
}
previousBankState = state;
}
}
}
void setup() {
for (int i = 0; i < numPotentiometers; i++) {
pinMode(potentiometerPins[i], INPUT);
}
for (int i = 0; i < numSwitches; i++) {
pinMode(switchPins[i], INPUT_PULLUP);
}
pinMode(bankSwitchPin, INPUT_PULLUP);
pinMode(bankLED, OUTPUT);
}
void loop() {
readPotentiometerValues();
readSwitchStates();
readBankSwitchState();
// Update bank LED
if (digitalRead(bankSwitchPin) == HIGH) {
digitalWrite(bankLED, HIGH);
} else {
digitalWrite(bankLED, LOW);
}
}