#include <Bounce.h>
const int channel = 1;
int s0 = 10;
int s1 =11;
int s2 = 14;
int s3 = 13; //Mux control pins
int controlpin = A0;
int previouspot0 = 0;
int previouspot1 = 0;
int previouspot2 = 0;
int previouspot3 = 0;
int previouspot4 = 0;
int previouspot5 = 0;
int previouspot6 = 0;
int previouspot7 = 0;
int previouspot8 = 0;
int previouspot9 = 0;
int previouspot10 = 0;
int previouspot11 = 0;
int previouspot12 = 0;
int previouspot13 = 0;
int previouspot14 = 0;
int previouspot15 = 0; // store previous values
int MUX_read = 0;
elapsedMillis msec = 0;
void setup(){
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
}
void loop(){
if (msec >= 20) {
msec = 0;
int pot0 = readMux(0) / 8;
int pot1 = readMux(1) / 8;
int pot2 = readMux(2) / 8;
int pot3 = readMux(3) / 8;
int pot4 = readMux(4) / 8;
int pot5 = readMux(5) / 8;
int pot6 = readMux(6) / 8;
int pot7 = readMux(7) / 8;
int pot8 = readMux(8) / 8;
int pot9 = readMux(9) / 8;
int pot10 = readMux(10) / 8;
int pot11 = readMux(11) / 8;
int pot12 = readMux(12) / 8;
int pot13 = readMux(13) / 8;
int pot14 = readMux(14) / 8;
int pot15 = readMux(15) / 8; //read values and write them in variables
// transmit only if control change
if (pot0 != previouspot0) {
usbMIDI.sendControlChange(controlpin, pot0, channel);
previouspot0 = pot0;
}
if (pot1 != previouspot1) {
usbMIDI.sendControlChange(controlpin, pot1, channel);
previouspot1 = pot1;
}
if (pot2 != previouspot2) {
usbMIDI.sendControlChange(controlpin, pot2, channel);
previouspot2 = pot2;
}
if (pot3 != previouspot3) {
usbMIDI.sendControlChange(controlpin, pot3, channel);
previouspot3 = pot3;
}
if (pot4 != previouspot4) {
usbMIDI.sendControlChange(controlpin, pot4, channel);
previouspot4 = pot4;
}
if (pot5 != previouspot5) {
usbMIDI.sendControlChange(controlpin, pot5, channel);
previouspot5 = pot5;
}
if (pot6 != previouspot6) {
usbMIDI.sendControlChange(controlpin, pot6, channel);
previouspot6 = pot6;
}
if (pot7 != previouspot7) {
usbMIDI.sendControlChange(controlpin, pot7, channel);
previouspot7 = pot7;
}
if (pot8 != previouspot8) {
usbMIDI.sendControlChange(controlpin, pot8, channel);
previouspot8 = pot8;
}
if (pot9 != previouspot9) {
usbMIDI.sendControlChange(controlpin, pot9, channel);
previouspot9 = pot9;
}
if (pot10 != previouspot10) {
usbMIDI.sendControlChange(controlpin, pot10, channel);
previouspot10 = pot10;
}
if (pot11 != previouspot11) {
usbMIDI.sendControlChange(controlpin, pot11, channel);
previouspot11 = pot11;
}
if (pot12 != previouspot12) {
usbMIDI.sendControlChange(controlpin, pot12, channel);
previouspot12 = pot12;
}
if (pot13 != previouspot13) {
usbMIDI.sendControlChange(controlpin, pot13, channel);
previouspot13 = pot13;
}
if (pot14 != previouspot14) {
usbMIDI.sendControlChange(controlpin, pot14, channel);
previouspot14 = pot14;
}
if (pot15 != previouspot15) {
usbMIDI.sendControlChange(controlpin, pot15, channel);
previouspot15 = pot15;
}
}
// MIDI Controllers should discard incoming MIDI messages.
while (usbMIDI.read()) {
// ignore incoming messages
}
}
void readMux(int channel){
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 i = 0; i < 4; i ++){
digitalWrite(controlPin[i], muxChannel[channel][i]);
}
}