   
   void sendNoteOn(byte channel, byte key, byte velocity)
   {
-    sendCommand(0x80, channel, key, velocity);
+    sendCommand(0x90, channel, key, velocity);
   }
 
   void sendNoteOff(byte channel, byte key, byte velocity)
   {
-    sendCommand(0x90, channel, key, velocity);
+    sendCommand(0x80, channel, key, velocity);
   }
   
 private:


static const int kMidiBaud = 9600;
static const int kMidiChannel = 0;
static const int kDotCount = 13;
static byte sPitchMap[kDotCount];

static const int kMidiBegin = 30;
static const int kMidiEnd = 85;
static const int kVelocity = 64;

static int sDot = 0;

void setup()
{
  // Seed pitch map table with something...
  for (int dot = 0; dot < kDotCount; dot++)
  {
    sPitchMap[dot] = map(dot, 0, kDotCount - 1, kMidiBegin, kMidiEnd);
  }
  
  Serial.begin(kMidiBaud);

  Stream& mStream;
};

class MusicBoxPip
{
public:
  static const int kMaxTime = 1000;
  static const int kMinVelocity = 1;
  static const int kMaxVelocity = 128;
  
  enum Transition
  {
    eTransitioned_None,
    eTransitioned_On,
    eTransitioned_Off
  };

public:
  MusicBoxPip() :
    mPin(0),
    mVelocity(0),
    mWasOn(false),
    mStartTime(0)
  {
  }
  
  void begin(int pin)
  {
    mPin = pin;
    pinMode(mPin, INPUT);
    mWasOn = isOnNow();
    mStartTime = millis();
  }
  
  Transition checkTransition()
  {
    bool isOn = isOnNow();
    Transition transition = eTransitioned_None;
    
    if (!mWasOn && isOn)
    {
      calculateTransitionVelocity();
      transition = eTransitioned_On;
    }
    else if(mWasOn && !isOn)
    {
      calculateTransitionVelocity();
      transition = eTransitioned_Off;
    }
    
    mWasOn = isOn;
    return transition;
  }
  
  int transitionVelocity()
  {
    return mVelocity;
  }
  
private:

  bool isOnNow()
  {
    return digitalRead(mPin) <= 0;
  }

  void calculateTransitionVelocity()
  {
    unsigned long now = millis();
    unsigned long elapsed = now - mStartTime;
    mStartTime = now;
    
    if (elapsed > kMaxTime)
    {
      elapsed = kMaxTime;
    }
    
    mVelocity = map(kMaxTime - elapsed, 0, kMaxTime, kMinVelocity, kMaxVelocity);
  }

  int mPin;
  bool mWasOn;
  unsigned long mStartTime;
  int mVelocity;
};

//
// Serial MIDI device:
//  Baud: 9600
