Hi, I followed below image to made a Midi Controller, use Arduino micro and CD4067, uploaded the code then use the loopMIDI and hairless-midi, but it no any response when press any button or potentiometer. What is the problem?
#include "MIDIUSB.h"
#include <Multiplexer4067.h>
#include <Thread.h>
#include <ThreadController.h>
/////////////////////////////////////////////
// buttons
const byte muxNButtons = 5;
const byte NButtons = 11;
const byte totalButtons = muxNButtons + NButtons;
const byte muxButtonPin[muxNButtons] = {12, 13, 14, 15, 11};
const byte buttonPin[NButtons] = {18, 19, 20, 10, 16, 14, 15, 6, 7, 8, 9};
int buttonCState[totalButtons] = {0};
int buttonPState[totalButtons] = {0};
/////////////////////////////////////////////
// debounce
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 5;
/////////////////////////////////////////////
// potentiometers
const byte NPots = 4;
const byte muxPotPin[NPots] = {0, 1, 2, 3};
int potCState[NPots] = {0};
int potPState[NPots] = {0};
int potVar = 0;
int lastCcValue[NPots] = {0};
/////////////////////////////////////////////
// pot reading
int TIMEOUT = 50;
byte varThreshold = 4;
boolean potMoving = true;
unsigned long pTime[NPots] = {0};
unsigned long timer[NPots] = {0};
/////////////////////////////////////////////
// midi
byte midiCh = 0; // * midi channel to be used
byte note = 36; // * The lowest note that will be used
byte cc = 1; // *Lowest CC that will be used
/////////////////////////////////////////////
// Multiplexer
Multiplexer4067 mplex = Multiplexer4067(2, 3, 4, 5, A3);
/////////////////////////////////////////////
// threads - program each Arduino activity to happen at a certain time
ThreadController cpu; // master thread, where others will be added
Thread threadReadPots; // thread to control pots
/////////////////////////////////////////////
void setup() {
mplex.begin();
for (int i = 0; i < NButtons; i++) {
pinMode(buttonPin[i], INPUT_PULLUP);
}
pinMode(A3, INPUT_PULLUP);
/////////////////////////////////////////////
// threads
threadReadPots.setInterval(10);
threadReadPots.onRun(readPots);
cpu.add(&threadReadPots);
/////////////////////////////////////////////
}
void loop() {
cpu.run();
readButtons();
}
/////////////////////////////////////////////
// read buttons
void readButtons() {
for (int i = 0; i < muxNButtons; i++) {
int buttonReading = mplex.readChannel(muxButtonPin[i]);
if (buttonReading > 1000) {
buttonCState[i] = HIGH;
}
else {
buttonCState[i] = LOW;
}
}
for (int i = 0; i < NButtons; i++) {
buttonCState[i + muxNButtons] = digitalRead(buttonPin[i]);
}
for (int i = 0; i < totalButtons; i++) {
if ((millis() - lastDebounceTime) > debounceDelay) {
if (buttonCState[i] != buttonPState[i]) {
lastDebounceTime = millis();
if (buttonCState[i] == LOW) {
noteOn(potMidiCh(), note + i, 127);
MidiUSB.flush();
buttonPState[i] = buttonCState[i];
}
else {
noteOn(potMidiCh(), note + i, 0);
MidiUSB.flush();
buttonPState[i] = buttonCState[i];
}
}
}
}
}
/////////////////////////////////////////////
//read potentiometers
void readPots() {
for (int i = 0; i < NPots - 1; i++) {
potCState[i] = mplex.readChannel(muxPotPin[i]);
}
for (int i = 0; i < NPots; i++) {
potVar = abs(potCState[i] - potPState[i]);
if (potVar >= varThreshold) {
pTime[i] = millis();
}
timer[i] = millis() - pTime[i];
if (timer[i] < TIMEOUT) {
potMoving = true;
}
else {
potMoving = false;
}
if (potMoving == true) {
int ccValue = map(potCState[i], 22, 1022, 0, 127);
if (lastCcValue[i] != ccValue) {
controlChange(11, cc + i, ccValue);
MidiUSB.flush();
potPState[i] = potCState[i];
lastCcValue[i] = ccValue;
}
}
}
}
/////////////////////////////////////////////
// calculates midi channel based on pot position
int potMidiCh () {
int potCh = map(mplex.readChannel(muxPotPin[9]), 22, 1023, 0, 4);
if (potCh == 4) {
potCh = 3;
}
return potCh + midiCh;
}
/////////////////////////////////////////////
// Arduino (pro)micro midi functions MIDIUSB Library
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}
void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
}