Hello, I'm having issues with multiplexing 3 pots (so far only 3 yes). Arduino reads every of them correctly IF I read only one of them, that is, if for loop exectutes only once (i<1). When I add 2 or 3 in the loop, I'm getting weird readings, for instance, I move pot 1 all the way right and it reads as if I moved 2nd pot (127 reading). Pretty random. Here is the code:
#define DEBUG
//current reading from pot
int sendval[3] = { 0, 0, 0};
//previous reading from pot
int last[3] = { 0, 0, 0};
//difference between current and previous reading
int diff[3] = { 0, 0, 0 };
//for loop variable
int i = 0;
int r0;
int r1;
int r2;
void setup() {
pinMode(2, OUTPUT); // s0
pinMode(3, OUTPUT); // s1
pinMode(4, OUTPUT); // s2
Serial.begin(9600);
}
void loop() {
//analog readings from pots
for (i=0; i<2; i++) {
r0 = bitRead(i,0);
r1 = bitRead(i,1);
r2 = bitRead(i,2);
digitalWrite(2, r0);
digitalWrite(3, r1);
digitalWrite(4, r2);
sendval[i]=analogRead(i);
diff[i]=abs(sendval[i]-last[i]);
if ( (sendval[i]/8 != last[i]/8 ) && (diff[i]>7) )
{
#ifdef DEBUG
Serial.print ("Pot number is: ");
Serial.println (i+1, DEC);
Serial.print("Value to send is: ");
Serial.print(sendval[i]/8);
Serial.print("\n \n");
#else
midiCC(0xB1, 21+i, sendval[i]/8);
#endif
// update last variable
last[i] = sendval[i];
}
}
}
// this function sends a Midi CC.
void midiCC(char CC_data, char c_num, char c_val){
Serial.print(CC_data, BYTE);
Serial.print(c_num, BYTE);
Serial.print(c_val, BYTE);
}
The ATmega has a sample-and-hold capacitor after the built-in multiplexer. If you change the multiplexer it adds some time to the reading process to let the new value settle. If you use an external multiplexer and read the same input again it doesn't know that the input may have jumped A LOT since the last reading so it does a short cycle. Try adding a small delay between changing the external multiplexer and reading the input pin.
Another method often used is to read the same input twice and throw away the first reading.
Reads a different analogue input each time. If you are using a 4051 you want to read the same analogue input because the 4051 has only one output.
As written the code assumes that you have three 4051 chips attached each to a different analogue input. Then you are reading the value from input 0 from the first chip, then input 1 from the second chip and input 2 from the third chip. Unless you have post attached to these inputs you will read floating values and get the sort of results you are seeing.
I don't think that is your setup.
Also why so much white space (blank lines) it makes the code hard to read.
Reads a different analogue input each time. If you are using a 4051 you want to read the same analogue input because the 4051 has only one output.
As written the code assumes that you have three 4051 chips attached each to a different analogue input. Then you are reading the value from input 0 from the first chip, then input 1 from the second chip and input 2 from the third chip. Unless you have post attached to these inputs you will read floating values and get the sort of results you are seeing.
I don't think that is your setup.
Also why so much white space (blank lines) it makes the code hard to read.
No I'm having only one 4051 connected with 3 pots connected to it.
Edit: I forgot to mention that I get funny readings when one of the pots is at the very right (reading 127). It gets stable after a while, is it because of multiple pots + 4051 connected to same source (arduino +5)?
Is it at the high (+5V) end or the low (ground end).
Swap the ends round, the knob will work backwards. Does the instability still happen at the same physical part of the pot even though now it is at the other end?
If so then you have a bad batch of pots, otherwise you have bad wiring.
What is the value of the pot.
I have no idea if your code works or not but i was working on something similar a bit back. I don't know if it will solve your problem, i wont compile as it is but it worked for me. I've put your variables names in it but it might need a check.
int readings[8];
int average[8];
#define Input 0
for (i=0; i<8; i++) {
r0 = bitRead(i,0);
r1 = bitRead(i,1);
r2 = bitRead(i,2);
digitalWrite(2, r0);
digitalWrite(3, r1);
digitalWrite(4, r2);
readings[i] = analogRead(Input);
average[i] = average[i] + ((readings [i] - average[i])>> 2);
Serial.print ("Pot number is: ");
Serial.println (i+1, DEC);
Serial.print("Value to send is: ");
Serial.print(average[i]<<4);
Serial.print("\n \n");
}