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

Thread: Need Help with 4051 Mux and Audio Library

  1. #1

    Need Help with 4051 Mux and Audio Library

    Hi there I'm having trouble integrating a CD4051 mux with 6 pot inputs into my teensy 3.2 with audio library.

    In the gui I have six sine oscillators multiplied by 6 sine LFOs acting as amplitude modulators.

    The pots should control the frequency of each of the LFOs

    All that is happening is that the 6th pot is controlling all the frequencies and the pin outputs on the Teensy stay stuck on the that particular high/low config.

    So I'm not sure how to fix that, Im having trouble understanding how to use the mux with the library.

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    // GUItool: begin automatically generated code
    AudioSynthWaveformSine   sine11;         //xy=78,576
    AudioSynthWaveformSine   sine10;         //xy=81,477
    AudioSynthWaveformSine   sine6;          //xy=82,619
    AudioSynthWaveformSine   sine5;          //xy=89,531
    AudioSynthWaveformSine   sine12;         //xy=93,674
    AudioSynthWaveformSine   sine4;          //xy=101,425
    AudioSynthWaveformSine   sine8;          //xy=107,236
    AudioSynthWaveformSine   sine1;          //xy=110,20
    AudioSynthWaveformSine   sine3;          //xy=109,308
    AudioSynthWaveformSine   sine2;          //xy=115,166
    AudioSynthWaveformSine   sine9;          //xy=128,356
    AudioSynthWaveformSine   sine7;          //xy=130,76
    AudioEffectMultiply      multiply6;      //xy=248,625
    AudioEffectMultiply      multiply2;      //xy=266,182
    AudioEffectMultiply      multiply5;      //xy=266,526
    AudioEffectMultiply      multiply4;      //xy=273,428
    AudioEffectMultiply      multiply1;      //xy=291,47
    AudioEffectMultiply      multiply3;      //xy=292,290
    AudioMixer4              mixer1;         //xy=477,74
    AudioMixer4              mixer2;         //xy=497,412
    AudioSynthWaveform       waveform13;     //xy=508,734
    AudioMixer4              mixer3;         //xy=673,266
    AudioOutputI2S           i2s1;           //xy=892,291
    AudioConnection          patchCord1(sine11, 0, multiply5, 1);
    AudioConnection          patchCord2(sine10, 0, multiply4, 1);
    AudioConnection          patchCord3(sine6, 0, multiply6, 0);
    AudioConnection          patchCord4(sine5, 0, multiply5, 0);
    AudioConnection          patchCord5(sine12, 0, multiply6, 1);
    AudioConnection          patchCord6(sine4, 0, multiply4, 0);
    AudioConnection          patchCord7(sine8, 0, multiply2, 1);
    AudioConnection          patchCord8(sine1, 0, multiply1, 0);
    AudioConnection          patchCord9(sine3, 0, multiply3, 0);
    AudioConnection          patchCord10(sine2, 0, multiply2, 0);
    AudioConnection          patchCord11(sine9, 0, multiply3, 1);
    AudioConnection          patchCord12(sine7, 0, multiply1, 1);
    AudioConnection          patchCord13(multiply6, 0, mixer2, 2);
    AudioConnection          patchCord14(multiply2, 0, mixer1, 1);
    AudioConnection          patchCord15(multiply5, 0, mixer2, 1);
    AudioConnection          patchCord16(multiply4, 0, mixer2, 0);
    AudioConnection          patchCord17(multiply1, 0, mixer1, 0);
    AudioConnection          patchCord18(multiply3, 0, mixer1, 2);
    AudioConnection          patchCord19(mixer1, 0, mixer3, 0);
    AudioConnection          patchCord20(mixer2, 0, mixer3, 1);
    AudioConnection          patchCord21(mixer3, 0, i2s1, 0);
    AudioControlSGTL5000     sgtl5000_1;     //xy=856,37
    // GUItool: end automatically generated code
    
    float ampPot0Smooth = 0.0;
    float ampPot0Diff = 0.0;
    
    int muxA = 3;
    int muxB = 4;
    int muxC = 5;
    
    
    void setup() {
      // put your setup code here, to run once:
      AudioMemory(20);
      sgtl5000_1.enable();
    
      pinMode(muxA, OUTPUT);
      pinMode(muxB, OUTPUT);
      pinMode(muxC, OUTPUT);
    
      sine1.frequency(160);
      sine2.frequency(180);
      sine3.frequency(192);
      sine4.frequency(240);
      sine5.frequency(256);
      sine6.frequency(320);
    
      sine1.amplitude(0.4);
      sine2.amplitude(0.4);
      sine3.amplitude(0.3);
      sine4.amplitude(0.2);
      sine5.amplitude(0.2);
      sine6.amplitude(0.2);
    
      sine7.frequency(0.1);
      sine8.frequency(0.1);
      sine9.frequency(0.1);
      sine10.frequency(0.1);
      sine11.frequency(0.1);
      sine12.frequency(0.1);
    
      sine7.amplitude(0.9);
      sine8.amplitude(0.9);
      sine9.amplitude(0.9);
      sine10.amplitude(0.9);
      sine11.amplitude(0.9);
      sine12.amplitude(0.9);
    
      mixer1.gain(0, 1.0);
      mixer1.gain(1, 1.0);
      mixer1.gain(2, 1.0);
      mixer1.gain(3, 0.0);
    
      mixer2.gain(0, 1.0);
      mixer2.gain(1, 1.0);
      mixer2.gain(2, 1.0);
      mixer2.gain(3, 0.0);
    
      mixer3.gain(0, 1.0);
      mixer3.gain(1, 1.0);
      mixer3.gain(2, 0.0);
      mixer3.gain(3, 0.0);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
     
      float ampPot0 = (float)analogRead(A19) / 1023.0;  
      
      ampPot0Diff = ampPot0 - ampPot0Smooth;
      ampPot0Smooth = ampPot0Smooth + (ampPot0Diff * 0.01f);
    
          digitalWrite(muxA, LOW); // Channel 0
        digitalWrite(muxB, LOW);
        digitalWrite(muxC, LOW);
        delayMicroseconds(50);
        sine7.frequency(ampPot0Smooth * 10);
    
        digitalWrite(muxA, HIGH); // Channel 1
        digitalWrite(muxB, LOW);
        digitalWrite(muxC, LOW);
        delayMicroseconds(50);
        sine8.frequency(ampPot0Smooth * 10);
    
        digitalWrite(muxA, LOW); // Channel 2
        digitalWrite(muxB, HIGH);
        digitalWrite(muxC, LOW);
        delayMicroseconds(50);
        sine9.frequency(ampPot0Smooth * 10);
    
        digitalWrite(muxA, HIGH); // Channel 3
        digitalWrite(muxB, HIGH);
        digitalWrite(muxC, LOW);
        delayMicroseconds(50);
        sine10.frequency(ampPot0Smooth * 10);
    
        digitalWrite(muxA, LOW); // Channel 4
        digitalWrite(muxB, LOW);
        digitalWrite(muxC, HIGH);
        delayMicroseconds(50);
        sine11.frequency(ampPot0Smooth * 10);
    
        digitalWrite(muxA, HIGH); // Channel 5
        digitalWrite(muxB, LOW);
        digitalWrite(muxC, HIGH);
        delayMicroseconds(50);
        sine12.frequency(ampPot0Smooth * 10); 
        
      AudioNoInterrupts();
      AudioInterrupts();  
    }
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	sine_mux_example.JPG 
Views:	8 
Size:	94.0 KB 
ID:	15226  

  2. #2
    Junior Member
    Join Date
    Jun 2018
    Location
    Oxford, UK
    Posts
    17
    Looks like you're only reading the value from the 4051 once per loop. I think you need to do analogRead(19) (and the smoothing code) each time you change the channel of the 4051 (six times per loop in total).

  3. #3
    Ok cool i will try that.

    Thank you!

Posting Permissions

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