Skip to content

Instantly share code, notes, and snippets.

@rhaamo
Forked from Simon-L/gist:4972421
Last active December 13, 2015 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhaamo/4972564 to your computer and use it in GitHub Desktop.
Save rhaamo/4972564 to your computer and use it in GitHub Desktop.
Midi Drum Controller using one Paper Sensor
#include <stdarg.h>
#include <MIDI.h>
// http://www.phys.unsw.edu.au/jw/notes.html <- for MIDI_NOTE
// Range : 40 - 1015
#define RLOW 40
#define RHIGH 1015
#define MIDI_CHANNEL 1
#define MIDI_NOTE 48 // C3
int range = RHIGH - RLOW;
boolean state = false;
// http://playground.arduino.cc/Main/Printf
void p(char *fmt, ... ){
char tmp[128]; // resulting string limited to 128 chars
va_list args;
va_start (args, fmt );
vsnprintf(tmp, 128, fmt, args);
va_end (args);
Serial.print(tmp);
}
void setup() {
// Initialize MIDI serial
// Serial will be 31250 and MIDI channel will be 1
MIDI.begin(MIDI_CHANNEL);
}
void loop() {
int capVal = analogRead(A0);
if ( (state == false) && (capVal <= 1015) ) {
if (capVal <= 40) {
int velocity = 127;
//p("Hit with velicity: %i (%i)\n", velocity, capVal);
MIDI.sendNoteOn(MIDI_NOTE, velocity, MIDI_CHANNEL);
state = true;
} else {
int velocity = ((float)(1015 - capVal)/range)*127;
//p("Hit with velicity: %i (%i)\n", velocity, capVal);
MIDI.sendNoteOn(MIDI_NOTE, velocity, MIDI_CHANNEL);
state = true;
}
} else if ((capVal > 1015) && (state == true)) {
//p("Button released (%i)\n\n", capVal);
MIDI.sendNoteOff(MIDI_NOTE, 0, MIDI_CHANNEL);
state = false;
} else {
// nothing
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment