Sending MIDI, formatting to binary?

Hey guys, working on my first project on the Arduino. The device is a MIDI velocity sensitive drum trigger using Piezo elements to pick up vibrations. You know, standard stuff. :wink:

Here’s what I’ve got thus far:

int threshold = 55; // 0-127 range, minimum threshold for note
int mx = 2; // number of input pins. Counting numbers (starting at 1)
int midiChan[] = {1, 1}; // MIDI Channel. Not used ATM
int midiNote[] = {15, 16}; // MIDI note
int midiVel[] = {0, 0}; // MIDI velocity initialization
boolean midiNoteIsOn[] = {false, false}; // Is note playing?
int anaVal[] = {0, 0}; // Store analog values here

void midiNoteOn(int note, int vel)
{
  Serial.println(10000000); // MIDI note on message
  Serial.println(note); 
  Serial.println(vel);
  Serial.println("Note on!"); //for debugging
}
  void midiNoteOff(int note)
{
  Serial.println(10010000); // MIDI note off message
  Serial.println(note);
  Serial.println(00000000);
  Serial.println("Note off!");
}


void setup()
{
  Serial.begin(9600); //Also for debugging. Arduino IDE serial monitor can't do the needed 31250 baud
}

void loop()
{
  for (int i=0; i<mx; i++) // Less than works with counting numbers.
  {
    anaVal[i] = analogRead(i); // Get 0-1023 value from pin
    midiVel[i] = anaVal[i]/8; // Convert to MIDI-usuable 0-127 range
    
    if (midiNoteIsOn[i] == false) 
    {
      if (midiVel[i] >= threshold) // If no note is playing and the piezo is sending voltage
      {
       midiNoteOn(midiNote[i],midiVel[i]);
       midiNoteIsOn[i] = true;
      }
    }
      else
      {
        if (midiVel[i] < threshold) // If a note is playing and the voltage is below threshold, note off
        {
          midiNoteOff(midiNote[i]);
          midiNoteIsOn[i] = false;
        }
      }
    }
}

Unfortunately, in the serial monitor, I get this:

10000000
15
73
Note on!
10010000
15
0
Note off!

Is there any way to get those last two lines to display as 8-bit words? Help a newbie out, guys! Thanks.

In the Serial.println statements, you can add the BIN qualifier, after the value, to see the number in binary, instead of decimal.

Serial.println(note, BIN);

Almost...Now I've got this:

10000000
1111
1010000
Note on!
10010000
1111
0
Note off!

Apparently, leading 0's are nor being printed. If you need them, you'll need to look into using sprintf to format the data as a string.

Can't understand what you mean by sprintf.

http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

This function can be used on the Arduino, and allows great control over how a number is converted to a string.

Underlying the Serial.print function is the printf function. The sprintf function is identical to printf, except that it puts the output in a character array instead of a stream.

This may not work for you, though, as I see no way to use sprintf to output the number in binary.

Converting the int to an 8 place binary string is not difficult, though.

Yup...not seeing a binary option in the specifiers.

Hmm, I've not read about any complaints from people using the MIDI tutorials included with the IDE, and they don't output like I'm trying to do, so maybe I should just go with that?