Skip to content

Instantly share code, notes, and snippets.

@KainokiKaede
Created November 27, 2013 18:28
Show Gist options
  • Save KainokiKaede/7680720 to your computer and use it in GitHub Desktop.
Save KainokiKaede/7680720 to your computer and use it in GitHub Desktop.
Sustain pedal to MIDI: Using Arduino with MIDI shield.
#include <MIDI.h>
// http://playground.arduino.cc/Main/MIDILibrary
// MIDI shield configuration
#define KNOB1 0
#define KNOB2 1
#define BUTTON1 2
#define BUTTON2 3
#define BUTTON3 4
#define STAT1 7 // Status LED
#define STAT2 6
/// I will connect my pedal to analog in 2
#define PEDAL1 2
#define LED 13 // LED pin on Arduino board
int pedal1val;
int pedal1temp;
void setup() {
pinMode(STAT1,OUTPUT);
pinMode(STAT2,OUTPUT);
pinMode(BUTTON1,INPUT);
pinMode(BUTTON2,INPUT);
pinMode(BUTTON3,INPUT);
for(int i = 0;i < 10;i++) // flash MIDI Sheild LED's on startup
{
digitalWrite(STAT1,HIGH);
digitalWrite(STAT2,LOW);
delay(30);
digitalWrite(STAT1,LOW);
digitalWrite(STAT2,HIGH);
delay(30);
}
digitalWrite(STAT1,HIGH);
digitalWrite(STAT2,HIGH);
// Read the initial value of the pedal.
pedal1val = pedal1read();
pinMode(LED, OUTPUT);
MIDI.begin();
}
void loop() {
/***** SUSTAIN PEDAL *****/
if(values_are_different(pedal1val, pedal1read())){
pedal1val = pedal1read();
MIDI.sendControlChange(64, pedal1val, 1); // Sustain, channel 1
}
}
int pedal1read() {
/*return 127 - analogRead(PEDAL1)/8; // convert value to value 127 - 0*/
pedal1temp = 127 - (analogRead(PEDAL1) - 82) / 6; // Fine tuning for my pedal.
if (pedal1temp > 127){pedal1temp = 127;}
else if(pedal1temp < 0) {pedal1temp = 0; }
return pedal1temp;
}
bool values_are_different(int val1, int val2){
// The easiest way:
// if(val1 != val2){return true;}
// else {return false;}
// A bit complicated way (trying not to send too many MIDI messages):
val1 = val1 - val2;
if(abs(val1) > 5){return true;} // abs(val1 - val2) is not supported.
else {return false;} // see http://arduino.cc/en/Reference/Abs
}
@KainokiKaede
Copy link
Author

Note:

  • l.59
    • There is a built-in function map to accomplish the aim of this line.
  • ll.60, 61
    • There is a built-in function constrain to accomplish the aim of these lines.

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