Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: How to create array for four 4051s on teensy2 analog pins

  1. #1
    Member
    Join Date
    Aug 2014
    Location
    Johannesburg South Africa
    Posts
    21

    How to create array for four 4051s on teensy2 analog pins

    Hello..

    I need desperate help with my controllers

    Click image for larger version. 

Name:	image.jpg 
Views:	155 
Size:	108.6 KB 
ID:	2637

    i am using 4 4051s on teensy2 pins A0,A1,A2,A3

    how do i code an array to loop through all analog inputs ?

    currently i can only read 1 4051 at a time with this code :

    Code:
    
    
    //ANALOG IN MUX loop _______________________________________________
    
    int toReadAnalogMux[] = { 
               //IC pin number are written below 
      1,1,1,1, //0-3 
      1,1,1,1,
      1,1,1,1,
      1,1,1,1//4-7 
      }; 
    
     
      
      //DEBUG_______________________________________________ 
      //enable if you want to test your output in the serial monitor
    boolean enableDebug = 0;  // 1 for enable, 0 for disable. **Remember to disable debug when running MIDI**. 
    int analogThreshold = 3; 
    
    //int INH_pin = 11; //analog read pin 
    int analogInsPrev[26]; //array to hold previously read analog values - set all to zero for now
    int tempAnalogInMux = 0; //array to hold previously read analog values 
    int tempAnalogInMap = 0;
     
    int ax = 0; //address pins
    int bx = 1; //address pins
    int cx = 2; //address pins
    
    int analogPin = 19;
    int controlPin[3] = {ax,bx,cx}; //set contol pins in array
    int muxChannel[8][4]={{0,0,0,0},{1,0,0,0},{0,1,0,0},{1,1,0,0},{0,0,1,0},{1,0,1,0},{0,1,1,0},{1,1,1,0}};
    //function to read mux
    int readMux(int channel){  
      //loop through the three control pins
      for(int i = 0; i < 3; i ++){ 
        //turn on/off the appropriate control pins according to what channel we are trying to read 
        digitalWrite(controlPin[i], muxChannel[channel][i]); 
      }
     
      //read the value of the pin
      int val = analogRead(analogPin);
      //return the value
      return val; 
     
    
    void loop(){
      
      
    
    
      for(int i=0;i<8;i++){       //loop through 8 mux channels
       
       
        //for(int multi = 9; multi > -1; multi--)                      // MY ATTEMPT :/
        
        if(toReadAnalogMux[i]==1){                                   //we read this mux channel analog input
          tempAnalogInMux = readMux(i);                              //read values using readMux function
          if(abs(analogInsPrev[i]-tempAnalogInMux)>analogThreshold){ //ensure value changed more than our threshold
            tempAnalogInMap = map(tempAnalogInMux,0,1023,0,127);     //remap value between 0 and 127
            usbMIDI.sendControlChange(i+1,tempAnalogInMap,2);        //send message
            analogInsPrev[i]=tempAnalogInMux;                        //update current value
          }
        }    
      }   
    
    }
    Thank you very much in advance !!

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    25,210
    Quote Originally Posted by garthtaylor View Post
    i am using 4 4051s on teensy2 pins A0,A1,A2,A3

    how do i code an array to loop through all analog inputs ?
    I don't quite understand this question. It looks like you're doing things correctly now.

  3. #3
    Member
    Join Date
    Aug 2014
    Location
    Johannesburg South Africa
    Posts
    21
    Quote Originally Posted by PaulStoffregen View Post
    I don't quite understand this question. It looks like you're doing things correctly now.
    hi thanks for the reply !


    basically the above code only aloows for one 4051 to be read at a time depending on which pin i select

    eg:

    if i have this : int analogPin = 18; (i can read the first 4051 (8 faders)

    if i change the code to : int analogPin = 19; ( i can then read the second 4051 (8 faders)

    I need to read all 4 analog pins at once the muxes are connectec to teensy pins 18,19,20 and 21

  4. #4
    Member
    Join Date
    Aug 2014
    Location
    Johannesburg South Africa
    Posts
    21
    this is so urgent cos i need to use these for shows

  5. #5
    Then modify the code to read 2 analog inputs at once:
    Code:
    //make val1 and val2 global outside of all the functions
    int val1;
    int val2;
    ...
    // inside the readMux function
    val1 = analogRead(analogPin1);
    val2 = analogRead(analogPin2);
    Or rework the function to accept the array itself and have it fill the array on each call. In the loop inside the function have it read the 2 pins at once.

  6. #6
    Member
    Join Date
    Aug 2014
    Location
    Johannesburg South Africa
    Posts
    21
    Quote Originally Posted by Demolishun View Post
    Then modify the code to read 2 analog inputs at once:
    Code:
    //make val1 and val2 global outside of all the functions
    int val1;
    int val2;
    ...
    // inside the readMux function
    val1 = analogRead(analogPin1);
    val2 = analogRead(analogPin2);
    Or rework the function to accept the array itself and have it fill the array on each call. In the loop inside the function have it read the 2 pins at once.
    Thank you for that..

    Ive done the code as you said and it still only reads one mux at a time

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •