Arduino to MIDI: Difference between revisions
From I/M/D Wiki
More actions
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
<nowiki>##</nowiki> Setup | <nowiki>##</nowiki> Setup | ||
| Line 13: | Line 11: | ||
5. Select your board: Tools > Board > Arduino Leonardo (or Pro Micro) | 5. Select your board: Tools > Board > Arduino Leonardo (or Pro Micro) | ||
🎛️🎚️ Potentiometers / Sliders | |||
<nowiki>###</nowiki> What you need: | <nowiki>###</nowiki> What you need: | ||
| Line 32: | Line 31: | ||
└─ Right pin / VCC / Power → 5V | └─ Right pin / VCC / Power → 5V | ||
#include <MIDIUSB.h> | |||
int lastValue = 0; | |||
void setup() { | |||
pinMode(A0, INPUT); // ← Change A0 to your pin | |||
} | |||
void loop() { | |||
int value = analogRead(A0) / 8; // ← Change A0 to match setup | |||
if (value != lastValue) { | |||
midiEventPacket_t event = {0x0B, 0xB0, 1, value}; | |||
// ↑ ← CC number | |||
MidiUSB.sendMIDI(event); | |||
MidiUSB.flush(); | |||
lastValue = value; | |||
} | |||
delay(10); | |||
} | |||
<nowiki>##</nowiki> 🔘 Note button | <nowiki>##</nowiki> 🔘 Note button | ||
| Line 99: | Line 75: | ||
``` | ``` | ||
#include <MIDIUSB.h> | |||
int lastState = HIGH; | |||
void setup() { | |||
pinMode(2, INPUT_PULLUP); // ← Change 2 to your pin | |||
int lastState = HIGH; | } | ||
void setup() { | void loop() { | ||
int state = digitalRead(2); // ← Change 2 to match setup | |||
if (state != lastState) { | |||
} | if (state == LOW) { | ||
// Button pressed | |||
void loop() { | midiEventPacket_t noteOn = {0x09, 0x90, 60, 127}; | ||
// ↑ ↑ | |||
// note velocity | |||
MidiUSB.sendMIDI(noteOn); | |||
} else { | |||
// Button released | |||
midiEventPacket_t noteOff = {0x08, 0x80, 60, 0}; | |||
// ↑ | |||
// note | |||
MidiUSB.sendMIDI(noteOff); | |||
} | |||
MidiUSB.flush(); | |||
lastState = state; | |||
} | |||
delay(10); | |||
} | |||
} | |||
Revision as of 12:35, 19 January 2026
## Setup
1. Install MIDIUSB library https://docs.arduino.cc/libraries/midiusb/
2. Add the MIDIUSB.h Library: Go to Tools < Manage Libraries...
3. Search for "MIDIUSB" by Gary Grewal
4. Install the latest version
5. Select your board: Tools > Board > Arduino Leonardo (or Pro Micro)
🎛️🎚️ Potentiometers / Sliders
### What you need:
- 1 potentiometer or slider
- 3 wires
### How to connect:
```
Potentiometer:
├─ Left pin / GND → GND
├─ Middle pin / Signal → A0 (or A1, A2, A3...)
└─ Right pin / VCC / Power → 5V
#include <MIDIUSB.h>
int lastValue = 0;
void setup() {
pinMode(A0, INPUT); // ← Change A0 to your pin
}
void loop() {
int value = analogRead(A0) / 8; // ← Change A0 to match setup
if (value != lastValue) {
midiEventPacket_t event = {0x0B, 0xB0, 1, value};
// ↑ ← CC number
MidiUSB.sendMIDI(event);
MidiUSB.flush();
lastValue = value;
}
delay(10);
}
## 🔘 Note button
### What you need
- 1 push button
- 2 wires
### How to connect
```
Button:
├─ One side → Pin 2 (or 3, 4, 5...)
└─ Other side → GND
```
#include <MIDIUSB.h>
int lastState = HIGH;
void setup() {
pinMode(2, INPUT_PULLUP); // ← Change 2 to your pin
}
void loop() {
int state = digitalRead(2); // ← Change 2 to match setup
if (state != lastState) {
if (state == LOW) {
// Button pressed
midiEventPacket_t noteOn = {0x09, 0x90, 60, 127};
// ↑ ↑
// note velocity
MidiUSB.sendMIDI(noteOn);
} else {
// Button released
midiEventPacket_t noteOff = {0x08, 0x80, 60, 0};
// ↑
// note
MidiUSB.sendMIDI(noteOff);
}
MidiUSB.flush();
lastState = state;
}
delay(10);
}