Ok guys so i'm very pleased to say that (even if a couple days late) my multiplexers has finally arrived!!!
But well i know that coding a multiplexer should be a feasible thing but, and i repeat but, i'm seriously stuck even with the stuff i found on the net >:( .. and well the fact is i cant read the value of the pot i mounted on the board (attached photo) neither send a midi signal.
thanks in advance for the advices!
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
////////////////////////////////////
//MUX
//MUX Pins
int s0 = 5;
int s1 = 4;
int s2 = 3;
int s3 = 2;
//Mux in "SIG" pin
int SIG_pin = {A5};
const int nMux=1;
/////////////////////////////////////////////
// potentiometers
const int NPots = 16; //* Number of Pots per MUX
int potCState[nMux][NPots] = {0}; // Current state of the pot
int potPState[nMux][NPots] = {0}; // Previous state of the pot
int potVar = 0; // Difference between the current and previous state of the pot
int midiCState[nMux][NPots] = {0}; // Current state of the midi value
int midiPState[nMux][NPots] = {0}; // Previous state of the midi value
int TIMEOUT = 300; //* Amount of time the potentiometer will be read after it exceeds the varThreshold
int varThreshold = 20; //* Threshold for the potentiometer signal variation
boolean potMoving = true; // If the potentiometer is moving
unsigned long PTime[nMux][NPots] = {0}; // Previously stored time
unsigned long timer[nMux][NPots] = {0}; // Stores the time that has elapsed since the timer was reset
/////////////////////////////////////////////
byte midiCh1 = 1; //MIDI channel to be used bank1
byte midiCh2 = 2;//* MIDI channel to be used bank2
byte note = 36; //* Lowest note to be used
byte cc = 1; //* Lowest MIDI CC to be used
void setup(){
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(SIG_pin, INPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
Serial.begin(115200);
}
void loop(){
//Loop through and read all 16 values
//Reports back Value at channel 6 is: 346
MuxPots1();
}
void MuxPots1(){
for(int i=0;i<16;i++){
int controlPin[] = {s0, s1, s2, s3};
int muxChannel[16][4]={
{0,0,0,0}, //channel 0
{1,0,0,0}, //channel 1
{0,1,0,0}, //channel 2
{1,1,0,0}, //channel 3
{0,0,1,0}, //channel 4
{1,0,1,0}, //channel 5
{0,1,1,0}, //channel 6
{1,1,1,0}, //channel 7
{0,0,0,1}, //channel 8
{1,0,0,1}, //channel 9
{0,1,0,1}, //channel 10
{1,1,0,1}, //channel 11
{0,0,1,1}, //channel 12
{1,0,1,1}, //channel 13
{0,1,1,1}, //channel 14
{1,1,1,1} //channel 15
};
//loop through the 4 sig
for(int j = 0; j < 4; j ++){
digitalWrite(controlPin[i], muxChannel[i][j]);
}
//read the value at the SIG pin
potCState[1][i]=analogRead(SIG_pin);
midiCState[1][i] = map(potCState[1][i], 0, 1023, 127, 0); // Maps the reading of the potCState to a value usable in midi
potVar = abs(potCState[1][i] - potPState[1][i]); // Calculates the absolute value between the difference between the current and previous state of the pot
if (potVar > varThreshold) { // Opens the gate if the potentiometer variation is greater than the threshold
PTime[1][i] = millis(); // Stores the previous time
}
timer[1][i] = millis() - PTime[1][i]; // Resets the timer 11000 - 11000 = 0ms
if (timer[1][i] < TIMEOUT) { // If the timer is less than the maximum allowed time it means that the potentiometer is still moving
potMoving = true;
}
else {
potMoving = false;
}
if (potMoving == true) { // If the potentiometer is still moving, send the change control
if (midiPState[1][i] != midiCState[1][i]) {
MIDI.sendControlChange(cc + i, midiCState[1][i], midiCh1); // cc number, cc value, midi channel
//Serial.println(midiCState);
potPState[1][i] = potCState[1][i]; // Stores the current reading of the potentiometer to compare with the next
midiPState[1][i] = midiCState[1][i];
}
}
}
}
HOOGE_RolandX.ino (3.65 KB)