Go Down

Topic: MIDI over USB with Arduino UNO (Solved) (Read 2988 times) previous topic - next topic

leenux5030

Apr 06, 2011, 09:49 pm Last Edit: Apr 09, 2011, 09:42 am by leenux5030 Reason: 1
Hello I followed the instructions mentioned here :
http://itp.nyu.edu/physcomp/Labs/MIDIOutput

and then I did this : http://www.youtube.com/watch?v=CJe9uq-9uFA

The video explains the problems I am encountering.
the code that I used is :
Code: [Select]
const int switchPin = 10;  // The switch is on Arduino pin 10
const int middleC = 60;    // Middle C (MIDI note value 60) is the lowest note we'll play
const int LEDpin = 13;     //  Indicator LED

// Variables:
byte note = 60;              // The MIDI note value to be played
//int AnalogValue = 0;        // value from the analog input
int lastNotePlayed = 0;     // note turned on when you press the switch
int lastSwitchState = 0;    // state of the switch during previous time through the main loop
int currentSwitchState = 0;

void setup() {
  //  set the states of the I/O pins:
  pinMode(switchPin, INPUT);
  pinMode(LEDpin, OUTPUT);
  //  Set MIDI baud rate:
  Serial.begin(31250);
  blink(3);
}

void loop() {
  //  My potentiometer gave a range from 0 to 1023:
  //AnalogValue = analogRead(0);
  //  convert to a range from 0 to 127:
  //note = AnalogValue/8;
  currentSwitchState = digitalRead(switchPin);
  // Check to see that the switch is pressed:
  if (currentSwitchState == 1) {
    //  check to see that the switch wasn't pressed last time
    //  through the main loop:
    if (lastSwitchState == 0) {
      // set the note value based on the analog value, plus a couple octaves:
      // note = note + 60;
      // start a note playing:
      noteOn(0x90, note, 0x40);
      // save the note we played, so we can turn it off:
      lastNotePlayed = note;
      digitalWrite(LEDpin, HIGH);
      note ++;
      if (note == 72)
      note = 60;
    }
  }
  else {   // if the switch is not pressed:
    //  but the switch was pressed last time through the main loop:
    if (lastSwitchState == 1) {
      //  stop the last note played:
      noteOn(0x90, lastNotePlayed, 0x00);
      digitalWrite(LEDpin, LOW);
    }
  }

  //  save the state of the switch for next time
  //  through the main loop:
  lastSwitchState = currentSwitchState;
}

//  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are  less than 127:
void noteOn(byte cmd, byte data1, byte  data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}

// Blinks an LED 3 times
void blink(int howManyTimes) {
  int i;
  for (i=0; i< howManyTimes; i++) {
    digitalWrite(LEDpin, HIGH);
    delay(500);
    digitalWrite(LEDpin, LOW);
    delay(500);
  }
}
 

dhunt

#1
Apr 07, 2011, 08:07 am Last Edit: Apr 07, 2011, 12:30 pm by dhunt Reason: 1
Since you are using an UNO, why not make it into a USB MIDI device instead of using serial?  You just need to program the ATMega8u2 with USB device firmware and then you can send MIDI commands using Serial.write() commands.  Then the UNO will appear to be a USB MIDI device and you won't need the MIDI to serial software.

Details here on how to do that here: Arduino hacking

leenux5030

Yes, I am aware of that option, but I am a total noob, I got this Arduino like 3 days back, and I just have very rudimentary knowledge about embedded systems, so I was afraid that I might end up doing something bad which is irreversible.
I searched for tutorials which are like beginners guide. but didn't find any good guides(I agree that due to time constraints my search wasn't exhaustive ), and over that I am not a rich dude, so didn't wanted to take any risk. 
BTW  the link that you have posted isn't working, and thanks for your response.

dhunt

I fixed the link (typo missing s).  Replacing the atmega8u2 using DFU mode is quite safe, you can always reprogram it with Arduino-usbserial.hex to recover the normal Arduino operation.  I'm suggesting trying the MIDI firmware because I think its simpler to use than the serial solution.  If you run into problems just drop me an email on darran [at] hunt [dot] net [dot] nz.

Cheers,
Darran.

leenux5030

Thanks for the link man , it was quite informative. I will definitely try it today when I reach home.
and will report back my progress.

dhunt

For windows, check this thread: http://arduino.cc/forum/index.php/topic,56488.0.html.  You'll need to use flip to program the atmega8u2.

leenux5030

Hey Thanks a lot dude, IT WORKED !, MY ARDUINO IS A USB MIDI , YAY ! 



lawrence_doss

hi Darren,
if you want to print the MIDI message to an LCD what should the command be - lcd.print(midiMsg); ?

This is my sketch to do program change via usb-
void SendMIDI(byte number)
{
t_midiMsg midiMsg;
midiMsg.msg.command = MIDI_COMMAND_PROGRAM_CHANGE;
midiMsg.msg.channel = number; // Data 1
midiMsg.msg.data2 = 0; // Data 2
midiMsg.msg.data3 = 0;
lcd.clear();
lcd.print(midiMsg);
Serial.write(midiMsg.raw, sizeof(midiMsg)); /* Send MIDI Message */
}

Go Up