Problem with multiplexer

Hello, everyone!
I'm trying to build a midi controller with 16 rotary potontiometers and 1 slide potentiometer. I'm using an arduino uno with 1 multiplexer 4067.

I testing the multiplexer using this code:

#include "Multiplexer4067.h"

//* Define s0, s1, s2, s3, and x pins
#define s0 4
#define s1 5
#define s2 6
#define s3 7
#define x1 A0


Multiplexer4067 mplex = Multiplexer4067(s0, s1, s2, s3, x1);

const int N_MUX_PINS = 1; // total number of components connected to the mux
int muxPin[N_MUX_PINS] = {0}; // pins used int the mux


void setup() {

  Serial.begin(115200);
  mplex.begin();

  pinMode(x1, INPUT_PULLUP);

}

void loop() {

  readMux();
  //Serial.println("??");
  delay(50);
}

// Reads the mux pins and prints its values in the serial monitor
void readMux() {

    for (int i = 0; i < N_MUX_PINS; i++) {
      Serial.print(i);
      Serial.print(": ");
      //Serial.print(map(mplex.readChannel(i), 0, 1022, 0, 127));
      Serial.print(mplex.readChannel(muxPin[i]));
      Serial.print("   ");
    }

//  Serial.print(mplex.readChannel(0));

  Serial.println();
  delay(10);

}


When i move the pots, it's work. Serial monitor can read the value.

The problem is when i using this code:

// LIBRARY
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();

#include <Multiplexer4067.h>
#define N_Mux 1
#define s0 4
#define s1 5
#define s2 6
#define s3 7
#define x1 A0
#define x2 A1
Multiplexer4067 mux[N_Mux] = {
  Multiplexer4067(s0, s1, s2, s3, x1)
};


//POTENTIOMETERS
const int N_Pots = 17;
const int NPots_Arduino = 1;
const int NPots_ArduinoPin[N_Pots] = {A1};

int potCState[N_Pots] = {0};
int potPState[N_Pots] = {0};
int midiCState[N_Pots] = {0};
int midiPState[N_Pots] = {0};
int potThreshold = 25;
int potVar = 0;
int lastCcValue[N_Pots] = {0};

unsigned long timer[N_Pots] = {0};
unsigned long lastPotTime[N_Pots] = {0};
const int timeout = 300;
boolean potMoving = true;

//POTENTIOMETERS MUX
const int NPots_PerMux[N_Mux] = {16};
const int NPots_MuxPin[N_Mux][16] = {
  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
};

//MIDI
byte midiChannel = 1;
byte note = 36;
byte cc = 1;

// ---------------------------------------------------------------------

void setup() {
  Serial.begin(115200);
  for (int i = 0; i < N_Mux; i++) {
    mux[i].begin();
    pinMode(x1, INPUT_PULLUP);
  }
}

void loop() {
  potentiometers();
}

void potentiometers() {
  for (int i = 0; i < NPots_Arduino; i++) {
    potCState[i] = analogRead(NPots_ArduinoPin[i]);

    int nPotsPerMuxSum = NPots_Arduino;
    for (int j = 0; j < N_Mux; j++) {
      for (int i = 0; i < NPots_PerMux[j]; i++) {
        potCState[i + nPotsPerMuxSum] = mux[j].readChannel(NPots_MuxPin[j][i]);
      }
      nPotsPerMuxSum += NPots_PerMux[j];
    }

    midiCState[i] = map(potCState[i], 0, 1023, 0, 127);


    int potVar = abs(potCState[i] - potPState[i]);

    if (potVar > potThreshold) {
      timer[i] = millis();
    }
    timer[i] = millis() - timer[i];
    if (timer[i] < timeout) {
      potMoving = true;
    } else {
      potMoving = false;
    }

    if (potMoving == true) {
      if (midiCState[i] != midiPState[i]) {
        MIDI.sendControlChange(cc + i, midiCState[i], midiChannel);

        midiPState[i] = midiCState[i];
        potPState[i] = midiCState[i];
      }
    }
  }
}

Hairless MIDI can't be read the pots from multiplexer. They only read the potentiometer from pin A1. Can someone give me a clue what's wrong with my code?

It's my wiring

Please fix your indentation (use Ctrl+T in the IDE), it will show that the values you're storing to potCState here are never used afterwards, they are never sent over MIDI.

In the meantime, you might want to look at Control Surface: Getting Started and Control Surface: 1.First-Output.ino. You can use the HairlessMIDI_Interface class instead of USBMIDI_Interface.

Note: Don't use pinMode() on pins you are only using for analogRead().

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.