This is my code... I'm using Leonardo.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Libraries ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <MIDIUSB.h>
#include <ResponsiveAnalogRead.h>
// ~~~~~~~~~~~~~~~~~~~~~~~~~ Pots Variables
const int N_POTS = 4;
int potPin[N_POTS] = { A0, A1, A2, A3 };
int potState[N_POTS] = { 0 };
int potPState[N_POTS] = { 0 };
// Midi Pots Variables
int potCC[N_POTS] = { 11, 12, 13, 14 };
int POT_CH = 0;
int potMidiState[N_POTS] = { 0 };
int potMidiPState[N_POTS] = { 0 };
// Pot Threshold Variables
const byte potThreshold = 3;
const int POT_TIMEOUT = 100;
unsigned long pPotTime[N_POTS] = { 0 };
unsigned long potTimer[N_POTS] = { 0 };
// ResponsiveAnalogRead library Variables
float snapMultiplier = 0.01;
ResponsiveAnalogRead responsivePot[N_POTS] = {};
int potReading[N_POTS] = { 0 };
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ put your setup code here, to run once: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void setup() {
Serial.begin(9600);
delay(1000); // Add a delay of 1 second (1000 milliseconds)
//
//
//
// Pot threshold setup
for (int i = 0; i < N_POTS; i++) {
responsivePot[i] = ResponsiveAnalogRead(0, true, snapMultiplier);
responsivePot[i].setAnalogResolution(1023); // Sets the resolution
}
}
//
//
//
//
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ put your main code here, to run repeatedly: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void loop() {
// Pot loop
for (int i = 0; i < N_POTS; i++) {
potReading[i] = analogRead(potPin[i]);
responsivePot[i].update(potReading[i]);
potState[i] = responsivePot[i].getValue();
potMidiState[i] = map(potState[i], 0, 1023, 0, 128);
int potVar = abs(potState[i] - potPState[i]);
if (potVar > potThreshold) {
pPotTime[i] = millis();
}
potTimer[i] = millis() - pPotTime[i];
if (potTimer[i] < POT_TIMEOUT) {
if (potMidiState[i] != potMidiPState[i]) {
controlChange(POT_CH, potCC[i], potMidiState[i]);
MidiUSB.flush();
//Serial.print("PotState: ");
//Serial.print(potState[i]);
//Serial.print(" | potMidiState: ");
//Serial.println(potMidiState[i]);
potMidiPState[i] = potMidiState[i];
}
potPState[i] = potState[i];
}
}
}
// FUNCTIONS for MIDI
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) {
midiEventPatcket_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);
}
I want to increase the pots to 12 using this mux.
I was able to do a test of the mux and 12 pots that successfully show changes in my serial.print but I'm unsure how to integrate it into the existing midi code.
// CD74HC4067 Multiplexer control pins
const int S0 = 2;
const int S1 = 3;
const int S2 = 4;
const int S3 = 5;
// Number of channels in the multiplexer
const int NUM_CHANNELS = 12;
void setup() {
Serial.begin(9600);
// Set control pins as outputs
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
// Initialize multiplexer (all selection pins low)
digitalWrite(S0, LOW);
digitalWrite(S1, LOW);
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
}
void loop() {
for (int channel = 0; channel < NUM_CHANNELS; channel++) {
int sensorValue = readChannel(channel);
Serial.print("Channel ");
Serial.print(channel);
Serial.print(": ");
Serial.println(sensorValue);
delay(100); // Adjust delay as needed
}
}
// Function to read analog value from specified channel
int readChannel(int channel) {
// Set selection pins according to channel number
digitalWrite(S0, channel & 0x01);
digitalWrite(S1, (channel >> 1) & 0x01);
digitalWrite(S2, (channel >> 2) & 0x01);
digitalWrite(S3, (channel >> 3) & 0x01);
// Wait for signals to settle
delay(10);
// Read analog value from the selected channel
int sensorValue = analogRead(A0); // Assuming A0 is used to read multiplexer output
return sensorValue;
}