Arduino, Potenciometros y Midi

Estoy haciendo el proyecto de un dispositivo MIDI el cual cuenta con 6 potenciometros, ya busque un código, pero me sale un error al verificar, no se si sea por usar un arduino Leonardo ya que soy nuevo en esto.

El código que completo es este:

// Simple UNTZtrument MIDI step sequencer.
// Requires an Arduino Leonardo w/TeeOnArdu config (or a PJRC Teensy),
// software on host computer for synth or to route to other devices.

#include <Wire.h>
#include <Adafruit_Trellis.h>
#include <Adafruit_UNTZtrument.h>
#include "MIDIUSB.h"

#define LED 13 // Pin for heartbeat LED (shows code is working)

#ifndef HELLA
// A standard UNTZtrument has four Trellises in a 2x2 arrangement
// (8x8 buttons total).  addr[] is the I2C address of the upper left,
// upper right, lower left and lower right matrices, respectively,
// assuming an upright orientation, i.e. labels on board are in the
// normal reading direction.
Adafruit_Trellis     T[4];
Adafruit_UNTZtrument untztrument(&T[0], &T[1], &T[2], &T[3]);
const uint8_t        addr[] = { 0x70, 0x71, 0x72, 0x73 };
#else
// A HELLA UNTZtrument has eight Trellis boards...
Adafruit_Trellis     T[8];
Adafruit_UNTZtrument untztrument(&T[0], &T[1], &T[2], &T[3],
                                 &T[4], &T[5], &T[6], &T[7]);
const uint8_t        addr[] = { 0x70, 0x71, 0x72, 0x73,
                                0x74, 0x75, 0x76, 0x77 };
#endif // HELLA

#define WIDTH     ((sizeof(T) / sizeof(T[0])) * 2)
#define N_BUTTONS ((sizeof(T) / sizeof(T[0])) * 16)

// Encoder on pins 4,5 sets tempo.  Optional, not present in
// standard UNTZtrument, but won't affect things if unconnected.
enc e(4, 5);

uint8_t       grid[WIDTH];                 // Sequencer state
uint8_t       heart        = 0,            // Heartbeat LED counter
              col          = WIDTH-1;      // Current column
unsigned int  bpm          = 240;          // Tempo
unsigned long beatInterval = 60000L / bpm, // ms/beat
              prevBeatTime = 0L,           // Column step timer
              prevReadTime = 0L;           // Keypad polling timer

// The note[] and channel[] tables are the MIDI note and channel numbers
// for to each row (top to bottom); they're specific to this application.
// bitmask[] is for efficient reading/writing bits to the grid[] array.
static const uint8_t PROGMEM
  note[8]    = {  72, 71, 69, 67, 65, 64, 62,  60 },
  channel[8] = {   1,  1,  1,  1,  1,  1,  1,   1 },
  bitmask[8] = {   1,  2,  4,  8, 16, 32, 64, 128 };

void setup() {
  Serial.begin (31250);//Default speed of MIDI serial port
  pinMode(LED,OUTPUT);//Light LED on L to notify of readynes
  digitalWrite(LED, HIGH);
 
#ifndef HELLA
  untztrument.begin(addr[0], addr[1], addr[2], addr[3]);
#else
  untztrument.begin(addr[0], addr[1], addr[2], addr[3],
                    addr[4], addr[5], addr[6], addr[7]);
#endif // HELLA
  // Default Arduino I2C speed is 100 KHz, but the HT16K33 supports
  // 400 KHz.  We can force this for faster read & refresh, but may
  // break compatibility with other I2C devices...so be prepared to
  // comment this out, or save & restore value as needed.
#ifdef ARDUINO_ARCH_SAMD
  Wire.setClock(400000L);
#endif
#ifdef __AVR__
  TWBR = 12; // 400 KHz I2C on 16 MHz AVR
#endif
  untztrument.clear();
  untztrument.writeDisplay();
  memset(grid, 0, sizeof(grid));
  enc::begin();                     // Initialize all encoder pins
  e.setBounds(60 * 4, 480 * 4 + 3); // Set tempo limits
  e.setValue(bpm * 4);              // *4's for encoder detents
}

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
  MidiUSB.flush();
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
  MidiUSB.flush();
}

// Turn on (or off) one column of the display
void line(uint8_t x, boolean set) {
  for(uint8_t mask=1, y=0; y<8; y++, mask <<= 1) {
    uint8_t i = untztrument.xy2i(x, y);
    if(set || (grid[x] & mask)) untztrument.setLED(i);
    else                        untztrument.clrLED(i);
  }
}

int iAn0Val=0;
int iAn1Val=0;
int iAn2Val=0;
int iAn3Val=0;
int iAn4Val=0;
int iAn5Val=0;

void loop() {
  //Canal1
  int iAn0ValPrev = iAn0Val;
  iAn0Val = analogRead(A0)/8;
  analogPinMidiTX(1,iAn0Val,iAn0ValPrev);

  //Canal2
  int iAn1ValPrev = iAn1Val;
  iAn1Val = analogRead(A1)/8;
  analogPinMidiTX(2,iAn1Val,iAn1ValPrev);

  //Canal3
  int iAn2ValPrev = iAn2Val;
  iAn2Val = analogRead(A2)/8;
  analogPinMidiTX(3,iAn2Val,iAn2ValPrev);

  //Canal4
  int iAn3ValPrev = iAn3Val;
  iAn3Val = analogRead(A3)/8;
  analogPinMidiTX(4,iAn3Val,iAn3ValPrev);

  //Canal5
  int iAn4ValPrev = iAn4Val;
  iAn4Val = analogRead(A4)/8;
  analogPinMidiTX(5,iAn4Val,iAn4ValPrev);

  //Canal6
  int iAn5ValPrev = iAn5Val;
  iAn5Val = analogRead(A5)/8;
  analogPinMidiTX(6,iAn5Val,iAn5ValPrev);

  
  uint8_t       mask;
  boolean       refresh = false;
  unsigned long t       = millis();

  enc::poll(); // Read encoder(s)

  if((t - prevReadTime) >= 20L) { // 20ms = min Trellis poll time
    if(untztrument.readSwitches()) { // Button state change?
      for(uint8_t i=0; i<N_BUTTONS; i++) { // For each button...
        uint8_t x, y;
        untztrument.i2xy(i, &x, &y);
        mask = pgm_read_byte(&bitmask[y]);
        if(untztrument.justPressed(i)) {
          if(grid[x] & mask) { // Already set?  Turn off...
            grid[x] &= ~mask;
            untztrument.clrLED(i);
            noteOff(pgm_read_byte(&channel[y]), pgm_read_byte(&note[y]), 127);
          } else { // Turn on
            grid[x] |= mask;
            untztrument.setLED(i);
          }
          refresh = true;
        }
      }
    }
    prevReadTime = t;
    digitalWrite(LED, ++heart & 32); // Blink = alive
  }

  if((t - prevBeatTime) >= beatInterval) { // Next beat?
    // Turn off old column
    line(col, false);
    for(uint8_t row=0, mask=1; row<8; row++, mask <<= 1) {
      if(grid[col] & mask) {
        noteOff(pgm_read_byte(&channel[row]), pgm_read_byte(&note[row]), 127);
      }
    }
    // Advance column counter, wrap around
    if(++col >= WIDTH) col = 0;
    // Turn on new column
    line(col, true);
    for(uint8_t row=0, mask=1; row<8; row++, mask <<= 1) {
      if(grid[col] & mask) {
          noteOn(pgm_read_byte(&channel[row]), pgm_read_byte(&note[row]), 127);
      }
    }
    prevBeatTime = t;
    refresh      = true;
    bpm          = e.getValue() / 4; // Div for encoder detents
    beatInterval = 60000L / bpm;
  }

  if(refresh) untztrument.writeDisplay();

}

  }
}

void analogPinMidiTX(int iChan, int iVal, int iValPrev)
{
  if (iValPrev !=iVal)
  {
    MidiTX(0, iChan, iValPrev);
    MidiTX(0x64, iChan,iVal);
  }
}

void MidiTX(unsigned char VELOCITY, unsigned char CANAL, unsigned char VALOR)//pass values out through standard Midi Command
{
  CANAL += 0X90 - 1;
  Serial.write(CANAL);
  Serial.write(VALOR);
  Serial.write(VELOCITY);
}

Y el código que cargue de referencia es este:

void setup()
{
Serial.begin(31250); // Default speed of MIDI serial port
pinMode(13, OUTPUT); // Light LED on pin 13 to notify of readynes
digitalWrite(13, HIGH);
}

int iAn0Val=0;
int iAn1Val=0;
int iAn2Val=0;
int iAn3Val=0;
int iAn4Val=0;

void loop()
{

//1
int iAn0ValPrev = iAn0Val;
iAn0Val = analogRead(0)/8;
analogPinMidiTX(1,iAn0Val,iAn0ValPrev);

//2
int iAn1ValPrev = iAn1Val;
iAn1Val = analogRead(1)/8;
analogPinMidiTX(2,iAn1Val,iAn1ValPrev);

//3
int iAn2ValPrev = iAn2Val;
iAn2Val = analogRead(2)/8;
analogPinMidiTX(3,iAn2Val,iAn2ValPrev);

//4
int iAn3ValPrev = iAn3Val;
iAn3Val = analogRead(3)/8;
analogPinMidiTX(4,iAn3Val,iAn3ValPrev);

//5
int iAn4ValPrev = iAn4Val;
iAn4Val = analogRead(4)/8;
analogPinMidiTX(5,iAn4Val,iAn4ValPrev);

}

void analogPinMidiTX(int iChan, int iVal, int iValPrev)
{
//only TX the value over midi if it is changed, as to prevent spamming the midi port and thus confusing the receiving application in learning mode
if(iValPrev != iVal)
{
MidiTX(176,iChan,iVal);
}
}

void MidiTX(unsigned char MESSAGE, unsigned char CONTROL, unsigned char VALUE) //pass values out through standard Midi Command
{
Serial.print(MESSAGE);
Serial.print(CONTROL);
Serial.print(VALUE);
}

Muchas gracias, para el que me pueda ayudar con este problema.

Hola. Posiblemente sea por ser una placa Leonardo. Trabajando dentro del propio Arduino todas las placas funcionan "prácticamente igual" pero cuando se trata de comunicar con el exterior, ya hay que empezar a diferenciar. Como sabrás, la Uno tiene un microcontrolador de USB a serie y comunica con el exterior de cierta manera. A la Leonardo no le hace falta ese pequeño microcontrolador, porque el 32U4 ya tiene puerto USB nativo, y el tema de las comunicaciones serie no se programa igual. Prueba con una Uno si tienes posibilidad. :wink:

Estoy usando la Leonardo por lo que es compatible con un elastomero adafruit trellis, el cual estoy usando con la librería MIDI y UNTZtrument, para que sea leído de una vez como MIDI en ciertos programas de audio. Pero solo me falta cuadrar los potenciometros, los cuales conecte en las entradas analógicas del arduino, al ser nuevo en esto la verdad no se como