issues with Hiduino with Arduino Mega Rev. 3

Hi,

I am trying to turn my arduino mega 2560 rev 3 into a MIDI controller. This is something I’ve done 5 times previously, the most recent time about a year ago, with not too much trouble.

But I’m doing the same steps that I’ve always done and not getting the same results. Has something changed with Arduino Megas or Arduino software?

I put my code below.

I added these lines last time I did this because it helped the code compile:

#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>

MIDI_CREATE_DEFAULT_INSTANCE();

I first uploaded the code to my arduino mega.

Then I followed the steps on this page to flash the USB port to become a midi port using Hiduino:

http://www.ladyada.net/learn/avr/avrdude.html

I am on a Mac and I used terminal to do this. According to avrdude, I did this successfully.
But when I plug my Arduino Mega into my computer via USB it is not showing up as a MIDI controller like it should. I even uploaded my arduino code again to the board to make sure it’s there.

Any suggestions? Or am I missing some new steps or code? I also downloaded the latest arduino midi library. I am using Arduino Software 1.8.5

thanks, Nick

#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>

MIDI_CREATE_DEFAULT_INSTANCE();  //nick added for compatibility with older sketch, 8/13/17

/*
  March 2012

  modified 8/13/17 by N.Demopoulos
*/

const int MIDIChannel = 1;

const int numAnalogIn = 13;
const int firstAnalogIn = 0;  // pin number of first analog in
const int firstAnalogController = 1;  // midi controller number for this analog in
int currentAnalogIndex = 0;
int analogOld[numAnalogIn];

const int numDigitalIn = 17;
const int firstDigitalIn = 22;   //pin number of first digital in
const int firstDigitalController = 14;  // midi controller number for first digital out
int currentDigitalIndex = 0;
int digitalOld[numDigitalIn];


void setup() {
  // wire 3.3V to AREF for 3.3V reference
  analogReference(EXTERNAL);
  
  MIDI.begin(MIDIChannel);
  MIDI.turnThruOff();
  
  int i;
  for (i=0;i<numAnalogIn; i++) {
    analogOld[i] = -1;
  }
  
  for (i=0;i<numDigitalIn; i++) {
    digitalOld[i] = -1;
    // set digital pins to input with pull-up
    pinMode(i+firstDigitalIn, INPUT);
    digitalWrite(i+firstDigitalIn, HIGH);
  }
}


void loop() {
 
  int analog = analogRead(currentAnalogIndex + firstAnalogIn);
  if (analog != analogOld[currentAnalogIndex]) {
    //MIDI.sendControlChange(currentAnalogIndex+firstAnalogController, analog >> 3, 1);
    analogOld[currentAnalogIndex] = analog;
    sendControlChange(currentAnalogIndex+firstAnalogController, analog >> 3, MIDIChannel);
  }
  
  currentAnalogIndex++;
  currentAnalogIndex %= numAnalogIn;
  
  
  int digital = digitalRead(currentDigitalIndex + firstDigitalIn);
  if (digital != digitalOld[currentDigitalIndex]) {
    digitalOld[currentDigitalIndex] = digital;
    sendControlChange(currentDigitalIndex+firstDigitalController, (digital==1 ? 0 :127), MIDIChannel);
  }
  
  currentDigitalIndex++;
  currentDigitalIndex %= numDigitalIn;
  
}

void sendControlChange(int controllerNumber, int controllerValue, int midiChannel) {
  //Serial.write(0xB0 | (midiChannel & 0x0F));
  //Serial.write(controllerNumber & 0x7F);
  //Serial.write(controllerValue & 0x7F);
  MIDI.sendControlChange(controllerNumber, controllerValue, midiChannel);
  delay(4);
}

Then I followed the steps on this page to flash the USB port to become a midi port using Hiduino:

AVR Tutorial - AVRDUDE

I cannot find anything related to "Hiduino" or flashing a USB port (whatever that should mean). The linked page is a how-to for the use of avrdude, nothing more.

To use a Mega as a USB MIDI device you have to upload to programs, one to the main processor (ATmega2560) and one to the USB adapter (ATmega16U2).

The sketch you posted is for the main processor. What kind of code did you load the the ATmega16U2, if you did?

Hi Pylon,

Thanks for your reply. Yes, you are right, the Arduino code is irrelevant at this point.

The page detailing how to use avrdude is what I'm using to upload hiduino to the mega's 16u2 chip. I think I have that part figured out.

I just tried doing it again, and I think there is a problem with my Hiduino hex file.

After uploading my file, which I called output2.hex, I saw this in the terminal window:

To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "output2.hex"
avrdude: error opening output2.hex: No such file or directory
avrdude: input file output2.hex auto detected as invalid format
avrdude: can't open input file output2.hex: No such file or directory
avrdude: read from file 'output2.hex' failed

I went to the Hiduino github and found the correct code for an Arduino Mega with a 16u2 chip. I copied the code and pasted it into a .hex file. Maybe there is a mistake in there somewhere.

Does anyone have a link for a direct download of a hiduino hex file? I'm guessing that could be where my problem is. It seems the github page just has the code, but not a way to download a file.

thanks for your input.

OK,

I just tried this whole process again with a different, older version of hiduino I had stored and it worked. The Arduino now comes up as a midi controller in my audio/midi setup.

And I got this in my terminal when flashing the 16u2 chip:

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e9489
avrdude: safemode: lfuse reads as EF
avrdude: safemode: hfuse reads as D9
avrdude: safemode: efuse reads as F4
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "output.hex"
avrdude: input file output.hex auto detected as Intel Hex
avrdude: writing flash (3646 bytes):

Writing | ################################################## | 100% 0.73s

avrdude: 3646 bytes of flash written
avrdude: verifying flash memory against output.hex:
avrdude: load data flash data from input file output.hex:
avrdude: input file output.hex auto detected as Intel Hex
avrdude: input file output.hex contains 3646 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.73s

avrdude: verifying ...
avrdude: 3646 bytes of flash verified

avrdude: safemode: lfuse reads as EF
avrdude: safemode: hfuse reads as D9
avrdude: safemode: efuse reads as F4
avrdude: safemode: Fuses OK (H:F4, E:D9, L:EF)

avrdude done. Thank you.

If I have any more issues I'll let you know.

Thanks, Nick.

You don't need avrdude to flash the ATmega16U2, you can flash it over USB using dfu-programmer.

Pieter