//#include <MIDI.h>
//on the janky midi connector I wired:
// yellow is pin 4 > voltage 5V
// red is pin 5 > Data Tx
// blue is pin 2 > Sheild (ground)
// For most boards you can use:
// MIDI_CREATE_DEFAULT_INSTANCE();
// Where MIDI_CREATE_DEFAULT_INSTANCE() is short for MIDI_CREATE_INSTANCE(HardwareSerial, Serial, MIDI);
// but we need to use 'Serial1' for the ATMEGA4809 (Nano)
// The following line compiles in Arduino IDE when using MIDI_Library, but not in Wokwi :-/
//MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
// The above is a workaround to the bug being tracked here:
// https://github.com/FortySevenEffects/arduino_midi_library/issues/300
// Once the bug is sorted, we can go back to using MIDI_CREATE_DEFAULT_INSTANCE(); for the Nano
const int buttonPins[3] = {2, 3, 4};
const int buttonPinCount = 3;
const int ledPins[3] = {A5, A4, A3};
const int ledPinCount = 3;
// MIDI change control from: https://downloads.neuraldsp.com/file/quad-cortex/Quad+Cortex+User+Manual+2_0_0.pdf
// button:Trigger -- A:35, B:36, C:37, D:38, E:39, F:40, G:41, H:42
const int midiControlChange[3] = {35, 36, 37};
const int midiChange = 0;
int buttonState[3] = {HIGH, HIGH, HIGH};
int lastButtonState[3] = {HIGH, HIGH, HIGH}; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // last time output pin was changed
unsigned long debounceDelay = 50; // debounce time, increase if the output flickers
void doStuff(int pinOn) {
//switch off all LEDs
for (int i = 0; i < ledPinCount; i++) {
digitalWrite(ledPins[i], LOW);
}
digitalWrite(ledPins[pinOn], HIGH);
//MIDI.sendControlChange(midiControlChange[pinOn], 0, 1);
}
void setup() {
// set pin modes
for (int i = 0; i < buttonPinCount; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
for (int i = 0; i < ledPinCount; i++) {
pinMode(ledPins[i], OUTPUT);
}
//MIDI.begin();
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < buttonPinCount; i++) {
int reading = digitalRead(buttonPins[i]);
if (reading != lastButtonState[i]) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// if the button state has changed
if (reading != buttonState[i]) {
buttonState[i] = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState[i] == HIGH) {
Serial.println("Do stuff");
doStuff(i);
}
}
}
lastButtonState[i] = reading;
}
}