Skip to content

Instantly share code, notes, and snippets.

@jongwook
Created June 9, 2014 19:03
Show Gist options
  • Save jongwook/946b63f1b0b187e41ad7 to your computer and use it in GitHub Desktop.
Save jongwook/946b63f1b0b187e41ad7 to your computer and use it in GitHub Desktop.
Arduino MIDI guitar
#include <SoftwareSerial.h>
SoftwareSerial midi(2, 3);
// there are 6 strings
#define STRINGS 6
// and 5 frets available
#define FRETS 5
// input pins
const int fingers[STRINGS] = {A0, A1, A2, A3, A4, A5};
const int strokes[STRINGS] = {5, 6, 7, 8, 9, 10};
// MIDI note numbers for each string
const int base[STRINGS] = {40, 45, 50, 55, 59, 64};
// threshold values for the analog inputs TODO: adjust
const int thresholds[FRETS] = {1000, 800, 600, 400, 200};
enum {
NOTE = 0x90,
BANK = 0xb0,
INSTRUMENT = 0xc0
};
void setup() {
midi.begin(31250);
for (int i = 0; i < STRINGS; i++) {
pinMode(strokes[i], INPUT);
}
}
void talk(int command, int pitch, int velocity) {
midi.write(command);
midi.write(pitch);
midi.write(velocity);
}
int state[STRINGS] = {0,};
int level[STRINGS] = {0,};
int note[STRINGS] = {-1,};
int frets(int string) {
for (int i = 0; i < FRETS; i++) {
if (level[string] > thresholds[i]) {
return i;
}
}
return FRETS;
}
int mute(int string) {
if (note[string] >= 0) {
talk(NOTE, note[string], 0);
}
}
void loop() {
for (int i = 0; i < STRINGS; i++) {
level[i] = analogRead(fingers[i]);
int value = digitalRead(strokes[i]);
if (value != state[i]) {
mute(i);
}
if (value == 1 && state[i] == 0) {
note[i] = base[i] + frets(i);
talk(NOTE, note[i], 72);
}
state[i] = value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment