Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: kustom on Feb 06, 2012, 06:19 pm

Title: 4051 and pots
Post by: kustom on Feb 06, 2012, 06:19 pm
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:

Code: [Select]
#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);

}
Title: Re: 4051 and pots
Post by: johnwasser on Feb 06, 2012, 07:01 pm
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.
Title: Re: 4051 and pots
Post by: Grumpy_Mike on Feb 06, 2012, 07:08 pm
Code: [Select]
sendval[i]=analogRead(i);
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.
Title: Re: 4051 and pots
Post by: kustom on Feb 06, 2012, 07:24 pm

Code: [Select]
sendval[i]=analogRead(i);
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)?
Title: Re: 4051 and pots
Post by: Grumpy_Mike on Feb 06, 2012, 07:28 pm
Quote
No I'm having only one 4051 connected with 3 pots connected to it.

So your code is wrong, do you understand where?
Title: Re: 4051 and pots
Post by: kustom on Feb 06, 2012, 07:31 pm

Quote
No I'm having only one 4051 connected with 3 pots connected to it.

So your code is wrong, do you understand where?


No. Please enlighten me.
Title: Re: 4051 and pots
Post by: Grumpy_Mike on Feb 06, 2012, 07:38 pm
Well I did tell you once.
this line:-
Code: [Select]
sendval[i]=analogRead(i);
should be:-
Code: [Select]
sendval[i]=analogRead(0);
Title: Re: 4051 and pots
Post by: kustom on Feb 06, 2012, 07:40 pm
Holy f***in shit. I wonder how I didn't notice that. Thanks!

I'm still getting unstable readings from 2nd and 3rd pot when they're at the very right tho.
Title: Re: 4051 and pots
Post by: Grumpy_Mike on Feb 06, 2012, 07:46 pm
Could be a faulty pot, try swapping over one of the others.
Title: Re: 4051 and pots
Post by: kustom on Feb 06, 2012, 08:07 pm
Well then they're all faulty except for the first, I refuse to believe in that. 5 pots, same behaviour: unstable readings on the very right.
Title: Re: 4051 and pots
Post by: Grumpy_Mike on Feb 07, 2012, 12:40 am
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.
Title: Re: 4051 and pots
Post by: dc42 on Feb 07, 2012, 02:34 am
How unstable are the readings, and what is the resistance of each pot?
Title: Re: 4051 and pots
Post by: EVP on Feb 07, 2012, 10:45 pm
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.

Code: [Select]
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");