midi multichannel

hi, me again. i found an awesome code from Greg Kennedy on the fourm. it is a midi to arduino sketch, but it only does one channel. im trying to get it to do 3.

what i changed:
added

// MIDI channel to answer to, 0x00 - 0x0F
#define myChannel 0x00
#define myChannel1 0x01
#define myChannel2 0x02

#define tonePin2 11
#define tonePin3 12

//setup: declaring iputs and outputs and begin serial
pinMode(tonePin2,OUTPUT);
pinMode(tonePin3,OUTPUT);

what im trying to figure out

void loop () {
  static byte note;
  static byte lastCommand = MIDI_IGNORE;
  static byte state;
  static byte lastByte;

  while (Serial.available()) {

    // read the incoming byte:
    byte incomingByte = Serial.read();

    // Command byte?
    if (incomingByte & 0b10000000) {
      if (respondAllChannels ||
             (incomingByte & 0x0F) == myChannel)  
             (incomingByte & 0x0F) == myChannel2)
             (incomingByte & 0x0F) == myChannel3)
          {
        lastCommand = incomingByte & 0xF0;
      } else { // Not our channel.  Ignore command.
        lastCommand = MIDI_IGNORE;
      }
      state = MIDI_STATE_BYTE1; // Reset our state to byte1.
    } else if (state == MIDI_STATE_BYTE1) { // process first data byte
      if ( lastCommand==MIDI_CMD_NOTE_OFF )
      { // if we received a "note off", make sure that is what is currently playing
        if (note == incomingByte) noTone(tonePin);
        state = MIDI_STATE_BYTE2; // expect to receive a velocity byte
      } else if ( lastCommand == MIDI_CMD_NOTE_ON ){ // if we received a "note on", we wait for the note (databyte)
        lastByte=incomingByte;    // save the current note
        state = MIDI_STATE_BYTE2; // expect to receive a velocity byte
      }
      // implement whatever further commands you want here
    } else { // process second data byte
      if (lastCommand == MIDI_CMD_NOTE_ON) {
        if (incomingByte != 0) {
          note = lastByte;
          tone(tonePin,(unsigned int)pgm_read_word(&frequency[note]));
        } else if (note == lastByte) {
          noTone(tonePin);
        }
      }
      state = MIDI_STATE_BYTE1; // message data complete
                                 // This should be changed for SysEx
    }
  }
}

the goal: making my arduino play barenaked ladies

You will need some logical ORs:

     if (respondAllChannels ||
             (incomingByte & 0x0F) == myChannel)  ||
             (incomingByte & 0x0F) == myChannel2) ||
             (incomingByte & 0x0F) == myChannel3)
          {

The built-in Tone() function can only play one note at a time. There is a third-party Tone library that can play more than one note at a time:
http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation

Note that to get 3 tone pins on a 168/328 you loose the ability to keep time (Timer 0).

lose one thing, gain another. ok i guess i will stick to 1 channel

noob question:

arent there other/more timers you can use/utilize?

thanks

xl97:
Aren't there other/more timers you can use/utilize?

The ATmega328P has three hardware timer/counters. The Tone library uses one timer per Tone.

The processor on the Arduino Mega has 6 timers.