Go Down

Topic: HELP WITH WIRING A SUSTAIN PEDAL TO BE USED AS A HI HAT PEDAL AND CODES FOR IT (Read 75 times) previous topic - next topic

staccatoboi

I am new to arduino, wirings and codes, but luckily I was able to make my own USB MIDIdrums using piezos connected to my laptop running superior drummer on my MAGIX: Music Maker MX via HAIRLESS MIDI + MIDI YOKE. I was able to make it work just by following the guides I've found at http://www.instructables.com/id/Arduino-Xylophone/ I made a slight changes on its codes since my setup was a little different. It is working fine, but I'm planning to buy a sustain pedal momentary/on/off type https://www.amazon.com/Stage-KSP100-Universal-Sustain-Pedal/dp/B001ELP5WM to be able to control the hi hat by stepping the pedal to close the hi hat and vice versa. If someone could give me some advice with the wirings on how to connect a 1/4 mono to be able to connect the pedal to arduino and if I need additional stuff like resistors etc, and to add a code for it I would really appriciate it.

By the way, this is the code I'm using with Hairless MIDI:

Code: [Select]
int pinRead;
char pinAssignments[16] ={
 'A0','A1','A2','A3','A4','A5','A6','A7','A8','A9','A10','A11'};
byte PadNote[16] = {
 57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72};         // MIDI notes from 0 to 127 (Mid C = 60)
int PadCutOff[16] =
{
 400,400,200,800,400,400,400,400,400,400,400,400,400,400,400,400};           // Minimum Analog value to cause a drum hit
int MaxPlayTime[16] = {
 90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90};               // Cycles before a 2nd hit is allowed
#define  midichannel 1;                              // MIDI channel from 0 to 15 (+1 in "real world")
boolean VelocityFlag  = true;                           // Velocity ON (true) or OFF (false)

//*******************************************************************************************************************
// Internal Use Variables
//*******************************************************************************************************************
boolean activePad[16] = {
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};                   // Array of flags of pad currently playing
int PinPlayTime[16] = {
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};                     // Counter since pad started to play
byte status1;

int pin = 0;    
int hitavg = 0;
//*******************************************************************************************************************
// Setup
//*******************************************************************************************************************
void setup()
{
 Serial.begin(57600);                                  // SET HAIRLESS TO THE SAME BAUD RATE IN THE SETTINGS

}
//*******************************************************************************************************************
// Main Program
//*******************************************************************************************************************
void loop()
{
 for(int pin=0; pin < 16; pin++)                          //
 {
   //int pin = 3;
   //   for (pinRead=0; pinRead < 16, pin++){
   hitavg = analogRead(pinAssignments[pin]);  
   //Serial.println(hitavg);  
   // read the input pin

   if((hitavg > PadCutOff[pin]))
   {
     if((activePad[pin] == false))
     {
       if(VelocityFlag == true)
       {
         //          hitavg = 127 / ((1023 - PadCutOff[pin]) / (hitavg - PadCutOff[pin]));    // With full range (Too sensitive ?)
         hitavg = (hitavg / 8) -1 ;                                                 // Upper range
       }
       else
       {
         hitavg = 127;
       }
       MIDI_TX(144,PadNote[pin],hitavg); //note on

       PinPlayTime[pin] = 0;
       activePad[pin] = true;
     }
     else
     {
       PinPlayTime[pin] = PinPlayTime[pin] + 1;
     }
   }
   else if((activePad[pin] == true))
   {
     PinPlayTime[pin] = PinPlayTime[pin] + 1;
     if(PinPlayTime[pin] > MaxPlayTime[pin])
     {
       activePad[pin] = false;
       MIDI_TX(144,PadNote[pin],0);
     }
   }
 }
}

//*******************************************************************************************************************
// Transmit MIDI Message
//*******************************************************************************************************************
void MIDI_TX(byte MESSAGE, byte PITCH, byte VELOCITY)
{
 status1 = MESSAGE + midichannel;
 Serial.write(status1);
 Serial.write(PITCH);
 Serial.write(VELOCITY);

}

Grumpy_Mike

You connect the peddle with the tip to an Arduino input pin and the ring to ground. Then you set up the pin as an input with the internal pull up resistors enabled in the setUp function.

Then a digitalRead of that pin will give you a HIGH if the peddle is not depressed and a LOW when it is.

Use this information to send a CC 66 message with the data as 00 for off or 0x7f for on.

Note to prevent you flooding the system with messages only send the CC 66 message when the state of the switch changes. That is save the state of the switch last time round the loop and only send the CC 66 when the current reading of the switch is not the same as the last reading.

Please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

staccatoboi

Thank you for your reply, I'm sorry if I've posted the codes improperly, I've corrected it already. Thank you for letting me know how to wire the jack for connecting the pedal to Arduino, but sorry for my stupidity, I just don't know how I would go about writing and adding the codes to my existing sketch and to where or what line I'm supposed to write or add it. I tried to read more about the examples found in Arduino software about INPUT_PULLUP, I kinda understand how it works but my problem is how do I go about writing the exact code, and the If and Then coding to program the switch to send a CC, I mean I'm a complete novice with codes, and I'm thinking of searching the internet for lessons, tutorials, how-tos. I bought the Arduino board from someone without a box that I think includes some paper works like a manual or something. Sorry for giving you so much trouble, I'm willing to learn more about Arduino to be good at it just like you guys.

Grumpy_Mike

Quote
I'm sorry if I've posted the codes improperly, I've corrected it already.
Thanks a lot for that.

Quote
to where or what line I'm supposed to write or add it
OK - line that sets the pin you want to use as an input and sets the pull up option is the pinMode call. It goes in the setUp function, anywhere will do.

The code that looks at this pin and acts on the value it sees goes in the loop function at the very beginning before the for loop that scans the sensors.

Quote
to send a CC
To send a CC message you call the void MIDI_TX() function. You pass it three parameters, the CC message byte of 0xB0, the CC channel you want to send, your case 66, and finally the data you want to send it. In your case the data will be either 0 or 0x7F depending if you want it on or off.

So you look at the switch with a digitalRead call and write a CC 66 message of 0 or 0x7F depending on what you find by using the "if" structure.

So do that, post your code and you will be ready for the final touch to complete what you want.

Note numbers that have the prefix 0x are numbers written in the base 16 or in hex. We use that to make things easy although beginners often prefer to tie them selves in knots rather than apply a little effort.

I hope you appreciate I am trying to teach you something here, and not just give you the six lines of code that this represents.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy