Pedal Box Code
/* 
 * MIDI Pedal box by Drew Wagner.  Public Domain. Have fun!
 *  Sorry it's a bit unpolished; I just wanted to get it up online before moving
 *  on to other projects.  
 */
 
#define PEDALS_SEND_NOTES 1
#define SOSTENUTO_IS_DAMPER 0
 
#define DAMPER_MIDI 64 // 0x40
#define SOSTENUTO_MIDI 66 // 0x42
#define UNACORDA_MIDI 67 // 0x43
 
int sostenutoLedPin = 6;                // LED connected to digital pin 13
int unaCordaLedPin = 5;
int value = LOW;                // previous value of the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 1000;           // interval at which to blink (milliseconds)
 
int unaCordaPin = 2; // uc pin 4
int sostenutoPin = 3; // uc pin 5
 
int unaCordaDepressed = LOW; // Flag that the una corda pedal is depressed
int sostenutoDepressed = LOW; // ... same for sostenuto
 
#define MIDI_SEND_CHANNEL 0x1
 
int unaCordaDepressedValue = LOW;
int sostenutoDepressedValue = LOW;
 
void setup()
{
  Serial.begin(31250);   //  Set MIDI baud rate:
 
  pinMode(unaCordaLedPin, OUTPUT);      // sets the digital pin as output
  pinMode(sostenutoLedPin, OUTPUT);      // sets the digital pin as output
  pinMode(unaCordaPin, INPUT);
  pinMode(sostenutoPin, INPUT);
 
  // Automatically detect footswitch polarity.
  // If there is a power fluctuation while pedal is down,
  // or if the pedal is down, or not plugged in when turned on,
  // it may be necessary to toggle the power to get the right polarity.
  unaCordaDepressedValue = !digitalRead(unaCordaPin);
  sostenutoDepressedValue = !digitalRead(sostenutoPin);
 
  digitalWrite(unaCordaLedPin, LOW);
  digitalWrite(sostenutoLedPin, LOW);
 
}
 
void loop()
{
 
    if ((digitalRead(unaCordaPin) == unaCordaDepressedValue) && !unaCordaDepressed) unaCordaDown(); 
    if ((digitalRead(unaCordaPin) == !unaCordaDepressedValue) && unaCordaDepressed) unaCordaUp(); 
    if ((digitalRead(sostenutoPin) == sostenutoDepressedValue) && !sostenutoDepressed) sostenutoDown(); 
    if ((digitalRead(sostenutoPin) == !sostenutoDepressedValue) && sostenutoDepressed) sostenutoUp();     
 
}
 
void damperDown(void)
{
 
  Serial.print(0xB0, BYTE);
  Serial.print(0x40, BYTE);
  Serial.print(0x7F, BYTE);
 
}
 
void damperUp(void)
{  
  Serial.print(0xB0, BYTE);
  Serial.print(0x40, BYTE);
  Serial.print(0x00, BYTE);
}
 
void unaCordaDown(void)
{
 
  Serial.print(0xB0, BYTE);
  Serial.print(UNACORDA_MIDI, BYTE);
  Serial.print(0x75, BYTE);
 
  //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
  #if PEDALS_SEND_NOTES
  noteOn(0x90, 60, 0x45);
  delay(40); // Hold note long enough for staccato.
  noteOn(0x90, 60, 0x00);
  #endif
 
  digitalWrite(unaCordaLedPin, HIGH);
 
  unaCordaDepressed = HIGH;
 
}
 
void unaCordaUp(void)
{
 
  //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
  #if DEBUG
  //noteOn(0x90, 60, 0x00);
  //damperUp();
  #endif
 
  Serial.print(0xB0, BYTE);
//  Serial.print(0x43, BYTE);
  Serial.print(UNACORDA_MIDI, BYTE);
  Serial.print(0x00, BYTE);
 
  digitalWrite(unaCordaLedPin, LOW);
 
  unaCordaDepressed = LOW;
 
}
 
void sostenutoDown(void)
{
 
  #if SOSTENUTO_IS_DAMPER
  damperDown();
  #else
  Serial.print(0xB0, BYTE);
  Serial.print(SOSTENUTO_MIDI, BYTE);
  Serial.print(0x7F, BYTE);
  #endif
 
  #if PEDALS_SEND_NOTES
  noteOn(0x90, 62, 0x45);
  delay(40); // Hold note long enough for staccato.
  noteOn(0x90, 62, 0x00);
  #endif
 
  digitalWrite(sostenutoLedPin, HIGH);
  sostenutoDepressed = HIGH;
}
 
void sostenutoUp(void)
{
  #if SOSTENUTO_IS_DAMPER
  damperUp();
  #else
  Serial.print(0xB0, BYTE);
  Serial.print(SOSTENUTO_MIDI, BYTE);
  Serial.print(0x00, BYTE);
  #endif
 
  digitalWrite(sostenutoLedPin, LOW);
  sostenutoDepressed = LOW;
}
 
//  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are  less than 127:
void noteOn(char cmd, char data1, char data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 3.0 License