Hi
Couldn't find a similar case in this forum so I post my question:
I'm working on a midi project, and try to digital read 4 buttons and analog read 11 potentiometers which 3 of them connected directly to the Arduino and other 8 via 4051 mux to A0 input the board.
The digital Read works fine, and so as analog read which is not from the mux, but something wrong with the mux readings. Only one potentiometer (the one that goes to input 000 of the mux) is being read, and with wrong values, while the other potentiometers aren't being read at all (or, at least, I can't tell if they are because the midi massage isn't being sent).
Some checks I've already done:
*I used a voltmeter and they all work fine electronically (the produce variable output voltage).
*The calcAvg function goes over all 11 values of the array
Thank you very much
*My Code:
// MIDI Library - Version: Latest
#include <MIDI.h>
// MIDI enable pin, avoids conflict of Serial between USB for programming and DIN MIDI I/O
#define MIDI_ENABLE 12
#define NUM_OF_ANALOG_INPUTS 4
#define NUM_OF_DIGITAL_INPUTS 4
#define NUM_OF_MUX_READ 8;
#define NUM_OF_READ 5;
// Variables
// define variables and arrays for the controller data
int AnalogValue[11] = {0,0,0,0,0,0,0,0,0,0,0};
int TotalValue[11] = {0,0,0,0,0,0,0,0,0,0,0};
int AvgArray[11] = {0,0,0,0,0,0,0,0,0,0,0};
int lastAnalogValue[11][5] ={{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}
,{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}};
// select the midi Controller Number for each input
int midiCCselect[6] = {1,2,3,4,5,6};
// select threshold for each analog input
int thresh[11] = {0,0,0,0,0,0,0,0,0,0,0};
//define array for digital input status
int DigitalValue[4] = {LOW,LOW,LOW,LOW};
//define array for digital input last status
int LastDigitalValue[4] = {LOW,LOW,LOW,LOW};
MIDI_CREATE_DEFAULT_INSTANCE();
static const unsigned ledPin = 13; // LED pin on Arduino Uno
const int channel = 1;
/*
*/
void setup() {
pinMode(ledPin, OUTPUT);
//digital pins
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
//mux selectors
pinMode(8, OUTPUT); // s0
//digitalWrite(8, LOW);
pinMode(9, OUTPUT); // s1
pinMode(10, OUTPUT); // s2
MIDI.begin();
// MIDI.begin(4); // Launch MIDI and listen to channel 4
}
void AnalogUpdate(int idx, int readnum) { //this func store analog read value in the analog values array;
lastAnalogValue[idx][readnum] =AnalogValue[idx];
return;
//End AnalogUpdate
}
void AvgCalc(int idx) { //this func calc an avarage of 5 read form the pin and send midi accordingly
int tmpTotal = 0;
int tmpAvg = 0;
for(int i=1;i<5;i++) {
tmpTotal = tmpTotal + lastAnalogValue[idx][i];
}
tmpAvg=tmpTotal/NUM_OF_READ;
if((tmpAvg < AvgArray[idx] -1) || (tmpAvg > AvgArray[idx] +1)){
//send control change on cc#i
MIDI.sendControlChange(16 +idx ,AnalogValue[idx],channel);
pinMode(ledPin,HIGH);
}
AvgArray[idx]=tmpAvg;
return;
}
void loop() {
int j=0;
int readnum=0;
int readn=0;
//Analog loop
for(int i=0;i<NUM_OF_ANALOG_INPUTS;i++){ //this pard is the mux reading
if(i == 0){
// MIDI.sendNoteOn(10, 100, 12);
for(readnum=0; readnum < 5; readnum++){
digitalWrite(8,LOW); //000
digitalWrite(9,LOW);
digitalWrite(10,LOW);
lastAnalogValue[i+j][readnum] = analogRead(i)/8;
j++;
digitalWrite(8,LOW); //001
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
lastAnalogValue[i+j][readnum] = analogRead(i)/8;
j++;
digitalWrite(8,LOW); //010
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
lastAnalogValue[i+j][readnum] = analogRead(i)/8;
j++;
digitalWrite(8,LOW); //011
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
lastAnalogValue[i+j][readnum] = analogRead(i)/8;
j++;
digitalWrite(8,HIGH); //100
digitalWrite(9,LOW);
digitalWrite(10,LOW);
lastAnalogValue[i+j][readnum] = analogRead(i)/8;
j++;
digitalWrite(8,HIGH); //101
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
lastAnalogValue[i+j][readnum] = analogRead(i)/8;
j++;
digitalWrite(8,HIGH); //110
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
lastAnalogValue[i+j][readnum] = analogRead(i)/8;
j++;
digitalWrite(8,HIGH); //111
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
lastAnalogValue[i+j][readnum] = analogRead(i)/8;
delay(20);
j=0;
}
for(int k=0;k<8;k++){ //calac avarage for each mux read
AvgCalc(i+k);
delay(100);
}
}
else{ //this pard read the other analog inputs a1 to a4
j=7;
for(readn=0; readn<5; readn++){
AnalogValue[i+j] = (analogRead(i))/8; // read the value from the analog input and divide by 8 for range 0-127
AnalogUpdate(i+j,readn);
}
AvgCalc(i+j);
}
delay(20);
pinMode(ledPin,LOW);
}
//Digital read loop
if(digitalRead(2) == LOW){
MIDI.sendNoteOn(16 +2 ,100,channel);
delay(20);
}
if(digitalRead(3) == LOW){
MIDI.sendNoteOn(16 +3 ,100,channel);
delay(20);
}
if(digitalRead(4) == LOW){
MIDI.sendNoteOn(16 +4 ,100,channel);
delay(20);
}
if(digitalRead(6) == LOW){
MIDI.sendNoteOn(16 +6 ,100,channel);
delay(20);
}
}//End Loop