Go Down

Topic: 4051 and pots (Read 3231 times) previous topic - next topic

kustom

Feb 06, 2012, 06:19 pm Last Edit: Feb 06, 2012, 06:21 pm by kustom Reason: 1
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);

}

johnwasser

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.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Grumpy_Mike

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.

kustom

#3
Feb 06, 2012, 07:24 pm Last Edit: Feb 06, 2012, 07:30 pm by kustom Reason: 1

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)?

Grumpy_Mike

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

So your code is wrong, do you understand where?

kustom


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.

Grumpy_Mike

Well I did tell you once.
this line:-
Code: [Select]
sendval[i]=analogRead(i);
should be:-
Code: [Select]
sendval[i]=analogRead(0);

kustom

#7
Feb 06, 2012, 07:40 pm Last Edit: Feb 06, 2012, 07:46 pm by kustom Reason: 1
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.

Grumpy_Mike

Could be a faulty pot, try swapping over one of the others.

kustom

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.

Grumpy_Mike

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.

dc42

How unstable are the readings, and what is the resistance of each pot?
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

EVP

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");


Go Up