Skip to content

Instantly share code, notes, and snippets.

@tomaustin700
Last active April 30, 2023 16:17
Show Gist options
  • Save tomaustin700/9a7ee759f7fc7f3be0c82e07d99dd125 to your computer and use it in GitHub Desktop.
Save tomaustin700/9a7ee759f7fc7f3be0c82e07d99dd125 to your computer and use it in GitHub Desktop.
#include <MIDI.h>
// Define the MIDI input pin, MIDI out channel, and the two relay output pins
#define RELAY_PIN_1 3
#define RELAY_PIN_2 2
// Create MIDI, relay, and MIDI out objects
MIDI_CREATE_DEFAULT_INSTANCE();
const int relay_pins[] = { RELAY_PIN_1, RELAY_PIN_2 };
void controlChange(byte channel, byte number, byte value) {
if (number == 13) {
// Check if the relay pins are already on
bool allOn = true;
for (int i = 0; i < 2; i++) {
if (digitalRead(relay_pins[i]) != HIGH) {
allOn = false;
break;
}
}
// If the relay pins are not already on, turn them on and start the timer to reset them
if (!allOn) {
for (int i = 0; i < 2; i++) {
digitalWrite(relay_pins[i], HIGH);
}
}
}
if (number == 92 && value == 0) {
for (int i = 0; i < 2; i++) {
digitalWrite(relay_pins[i], LOW);
}
}
}
void setup() {
// Initialize the MIDI, relay, and MIDI out objects
MIDI.begin();
MIDI.setHandleControlChange(controlChange);
for (int i = 0; i < 2; i++) {
pinMode(relay_pins[i], OUTPUT);
}
}
void loop() {
// Check for MIDI messages
MIDI.read();
}
@tomaustin700
Copy link
Author

tomaustin700 commented Apr 28, 2023

MIDI_IN_PIN: This pin is used to connect the MIDI input cable to the Arduino board. In the code, it is defined as pin 2, but you can change this to any available digital input pin on your board.

RELAY_PIN_1 and RELAY_PIN_2: These pins are used to connect the relays to the Arduino board. In the code, they are defined as pins 3 and 4, respectively, but you can change them to any available digital output pins on your board.

Serial1: This is the hardware serial port used for the MIDI output (pin 1 (TX)).

Make sure to connect the pins on your components to the correct pins on your Arduino board according to the circuit diagram of your setup. For example, you will need to connect the MIDI input pin to the pin you define as MIDI_IN_PIN in the code, and connect the relay input pins to the pins you define as RELAY_PIN_1 and RELAY_PIN_2 in the code.

@tomaustin700
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment