I created a two button, up and down midi program change controller with some extra switches to change the midi channel. It works perfectly on the breadboard using a sparkfun pro micro however I want to use an atmega328p on the PCB for it hence the uno.
The code doesn't seem to work properly at all with the uno and the serial monitor equally isn't doing what it's should (see photo)
Is there anything I need to change to make this code work properly with the arduino uno and atmega328p? Any help would be greatly appreciated!
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
const byte LEDs[] = {15, 14, 16, 10, 9, 8, 7, 6}; // Pins for LEDs
const byte dipSwitchPins[] = {4, 5}; // Pins for dip switches
int Up = 2;
int Down = 3;
void setup() {
Serial.begin(115200);
MIDI.begin();
pinMode (Up, INPUT_PULLUP); // Momentary switch 1
pinMode (Down, INPUT_PULLUP); // Momentary switch 2
pinMode(dipSwitchPins[0], INPUT_PULLUP); // Dip switch 1
pinMode(dipSwitchPins[1], INPUT_PULLUP); // Dip switch 2
for (byte i = 0; i < 8; i++) pinMode(LEDs[i], OUTPUT);
}
void loop() {
static int progNo = 0;
static bool upNow, downNow = false;
static byte channelNo = 1; // Default MIDI channel
upNow = digitalRead(Up) == LOW;
downNow = digitalRead(Down) == LOW;
// Check if both up and down buttons are pressed
if (upNow && downNow) {
progNo = 127; // Set to preset 127
MIDI.sendProgramChange(byte(progNo), channelNo);
delay(55); // not a Debounce delay
} else {
// Check individual button presses
if (upNow) {
progNo++; // Increment program change
if (progNo > 7) progNo = 0; // Wrap around to 0
MIDI.sendProgramChange(byte(progNo), channelNo);
delay(158);
}
if (downNow) {
progNo--; // Decrement program change
if (progNo < 0) progNo = 7; // Wrap around to 7
if (progNo == 126) progNo = 7;
MIDI.sendProgramChange(byte(progNo), channelNo);
delay(158);
}
}
// Update LEDs and print program and channel number
for (byte i = 0; i < 8; i++) digitalWrite(LEDs[i], progNo != i ? LOW : HIGH);
// Read dip switches to set MIDI channel
byte dipSwitchState = digitalRead(dipSwitchPins[0]) | (digitalRead(dipSwitchPins[1]) << 1);
channelNo = map(dipSwitchState, 0, 3, 1, 4);
Serial.print("Program: ");
Serial.print(progNo);
Serial.print(" | Channel: ");
Serial.println(channelNo);
}