arduino and midi

Hey guys, i need some help. I’m currently working on my project “MIDI drum pads”. I’ve flip the arduino with MIDI.hex to make it as a MIDI device, connect it to a MIDI interface (MIDI in) then to my laptop (the laptop only serves as speaker). I’ve connected the MIDI interface into the tx pin of the arduino but it seems the arduino itself is not transmitting the signals. It only receives signals when the buttons/pads are pressed(RX led is on).

But by only using MidiOX, the arduino produces drum sounds even when the MIDI interface is disconnected or not being used. I can’t seem to play the drum pads without the MidiOX and by only using the MIDI interface as a bridge for arduino and the laptop. Any help is very much appreciated. Thanks! :slight_smile:

Below is the code.

#include <avr/io.h>
#include <stdint.h>
#include <avr/interrupt.h>

void USART_Transmit( unsigned char data );
int ON = 120;//velocity of MIDI notes
int OFF = 0; 
int noteON = 153;//midi note on channel 10 percussion 1001 1001
int note;
volatile uint8_t portbhistory = 0xFF;
volatile uint8_t state = 0;

void setup(){
 //Set MIDI baud rate:
 Serial.begin(31250);
 DDRD |= 0xFF;
 PORTD &= 0x00;
 DDRB &= ~((1<<DDB0)|(1<<DDB1)|(1<<DDB2)|(1<<DDB3)|(1<<DDB4));
 PORTB |= ((1<<PORTB0)|(1<<PORTB1)|(1<<PORTB2)|(1<<PORTB3)|(1<<PORTB4));
 
 PCICR |= (1<<PCIE0);
 PCMSK0 |= ((1<<PCINT0)|(1<<PCINT1)|(1<<PCINT2)|(1<<PCINT3)|(1<<PCINT4));
 
 sei();
}

void loop()
{
   switch(state)
   {
     case 0:
       PORTD = 0x00;
       MIDImessage(noteON, note, OFF);
     break;
     
     case 1:
       PORTD |= (1<<PORTD2);//LED1
       MIDImessage(noteON, note, ON);
       _delay_ms(300);
       state = 0;
      break;
     
      case 2:
       PORTD |= (1<<PORTD3);//LED2
       MIDImessage(noteON, note, ON);        
       _delay_ms(300); 
       state = 0; 
     break;
     
     case 3:
       PORTD |= (1<<PORTD4);//LED3
       MIDImessage(noteON, note, ON);          
       _delay_ms(300);
       state = 0;
     break;

    case 4:
       PORTD |= (1<<PORTD5);//LED4
       MIDImessage(noteON, note, ON);  
       _delay_ms(300);
       state = 0;
     break;
     
     case 5:
       PORTD |= (1<<PORTD6);//LED5
       MIDImessage(noteON, note, ON);  
       _delay_ms(300);
       state = 0;
     break;
   }
}

ISR(PCINT0_vect)
{
 uint8_t changedbits;
 changedbits = PINB ^ portbhistory;
 portbhistory = PINB;

   if(changedbits & (1<<PINB0))
   {
     state = 1;
     note = 36;//Bass drum
   }
   if(changedbits & (1<<PINB1))
   {
     state = 2;
     note = 38;//Snare drum
   }
   if(changedbits & (1<<PINB2))
   {
     state = 3;
     note = 42;//High Hat
   }
  if(changedbits & (1<<PINB3))
   {
     state = 4;
     note = 49;//Crash cymbal
   }
   if(changedbits & (1<<PINB4))
   {
     state = 5;
     note = 45;//right tom
   }
}

//send MIDI message
void MIDImessage(int command, int MIDInote, int MIDIvelocity) 
{
 USART_Transmit(command);//send note on or note off command
 USART_Transmit(MIDInote);//send note number (determines what drum to play)
 USART_Transmit(MIDIvelocity);//send velocity data
}

void USART_Transmit( unsigned char data )
{
 //Wait for empty transmit buffer 
 while ( !( UCSR0A & (1<<UDRE0)) )
   ;
 //Put data into buffer, sends the data 
 UDR0 = data;
}