Untztrument Adafruit Pot Extension Multiplexer

Hey guys,

hopefully one of you can help me fulfilling this little project.

i got an untztrument by adafruit running on a leonardo in teensy mode.
nothing spectacular so far and everything works fine.

now i´d like to add 8 x 100k pots over a multiplexer into the existing code and i have problems!
i´m not really familiar to arduino, i´m a musician who tries to understand this platform better to use it …
so i´m trying to explain as good as i can.

i get the pots running standalone as i want if i cut the untztrument stuff instead of the libraries.
meanwhile i get both running, but only 2 of the pots, and they act strange …
so there is definitely no hardware issue!
i figured out that the I2C definition causes the pots issue, but i got stuck and i´m not sure if this is the problem.

maybe someone of you got an idea or does have a better solution for it?
after struggling for weeks inside the adafruit forum i´m a bit frustrated …

see both codes which are running but what´s the point?

Untztrument Code:

// Super-basic UNTZtrument MIDI example.  Maps buttons to MIDI note
// on/off events; this is NOT a sequencer or anything fancy.
// 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>

#define LED     13 // Pin for heartbeat LED (shows code is working)
#define CHANNEL 1  // MIDI channel number

#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

// For this example, MIDI note numbers are simply centered based on
// the number of Trellis buttons; each row doesn't necessarily
// correspond to an octave or anything.
#define WIDTH     ((sizeof(T) / sizeof(T[0])) * 2)
#define N_BUTTONS ((sizeof(T) / sizeof(T[0])) * 16)
#define LOWNOTE   ((128 - N_BUTTONS) / 2)

uint8_t       heart        = 0;  // Heartbeat LED counter
unsigned long prevReadTime = 0L; // Keypad polling timer

void setup() {
  pinMode(LED, OUTPUT);
#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
#ifdef __AVR__
  // 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.
  TWBR = 12;
#endif
  untztrument.clear();
  untztrument.writeDisplay();
}

void loop() {
  unsigned long t = millis();
  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...
        // Get column/row for button, convert to MIDI note number
        uint8_t x, y, note;
        untztrument.i2xy(i, &x, &y);
        note = LOWNOTE + y * WIDTH + x;
        if(untztrument.justPressed(i)) {
          usbMIDI.sendNoteOn(note, 127, CHANNEL);
          untztrument.setLED(i);
        } else if(untztrument.justReleased(i)) {
          usbMIDI.sendNoteOff(note, 0, CHANNEL);
          untztrument.clrLED(i);
        }
      }
      untztrument.writeDisplay();
    }
    prevReadTime = t;
    digitalWrite(LED, ++heart & 32); // Blink = alive
  }
  while(usbMIDI.read()); // Discard incoming MIDI messages
}

/* Here's the set of MIDI functions for making your own projects:

  usbMIDI.sendNoteOn(note, velocity, channel)
  usbMIDI.sendNoteOff(note, velocity, channel)
  usbMIDI.sendPolyPressure(note, pressure, channel)
  usbMIDI.sendControlChange(control, value, channel)
  usbMIDI.sendProgramChange(program, channel)
  usbMIDI.sendAfterTouch(pressure, channel)
  usbMIDI.sendPitchBend(value, channel)
  usbMIDI.sendSysEx(length, array)
  usbMIDI.send_now()

  Some info on MIDI note numbers can be found here:
  http://www.phys.unsw.edu.au/jw/notes.html

  Rather than MIDI, one could theoretically try using Serial to
  create a sketch compatible with serialosc or monomeserial, but
  those tools have proven notoriously unreliable thus far. */

My modification without Untztrument what works!:

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

#define CHANNEL 1  // MIDI channel number
int zaehler = 0;

int bit1 = 0;
int bit2 = 0;
int bit3 = 0;

int potiWert[8];
int controllerWert[8];
int controllerWertAlt[8];
int controllerNummer[] = {1, 2, 3, 4, 5, 6, 7, 8};


void setup() {
 pinMode(2, OUTPUT);
 pinMode(3, OUTPUT);
 pinMode(4, OUTPUT);
 
 
}

void loop () {
 for ( zaehler = 0 ; zaehler <= 7; zaehler++ ) {

  bit1 = bitRead(zaehler, 0);
  bit2 = bitRead(zaehler, 1);
  bit3 = bitRead(zaehler, 2);

  digitalWrite(2, bit1);
  digitalWrite(3, bit2);
  digitalWrite(4, bit3);

potisAbfragen(zaehler, A0);

 
 Serial.print(potiWert[zaehler]);
  Serial.print(" ");
 } 
 Serial.println("");
}

void potisAbfragen(int zaehler, int analogPin) {
 potiWert[zaehler] = 0.2 * potiWert[zaehler] + 0.8 * analogRead(analogPin);
 controllerWert[zaehler] = map(potiWert[zaehler],1023,0,0,127);
 if (controllerWert[zaehler] != controllerWertAlt[zaehler]) {
   sendeMIDI(false, controllerNummer[zaehler], controllerWertAlt[zaehler]);
   sendeMIDI(true, controllerNummer[zaehler], controllerWert[zaehler]);
   controllerWertAlt[zaehler] = controllerWert[zaehler];
 }

  
}

void sendeMIDI(bool turnOn, int channel, int controllerWert) {
  if (turnOn)
        usbMIDI.sendControlChange(zaehler, controllerWert, CHANNEL);

  else
usbMIDI.sendControlChange(zaehler, controllerWert, CHANNEL);


 while(usbMIDI.read()); // Discard incoming MIDI messages
}

No that code doesn’t work you have no serial begin but you have a serial print.

You need to include links to what you have in the way of hardware. What you are trying to do is quite simple but post the whole code by copy and paste.

You can’t send MIDI and have a serial print in the same sketch because there is only one route back to the computer and you can’t use it for two things at the same time.

YES I CAN!

problem is the I2C speed of pin 2/3. don´t use them!

it´s working!