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

Thread: Effects routing

  1. #1

    Effects routing

    Hello, Trying to get an effect to work (flanger) based on sample code. I generate a tone out on Pin 33. It is turned on/off through a button. The square wave then gets fed back in to the i2s input. I had it simply route to the i2s output, and it worked just fine. Now I added a flange effect
    into the signal chain ( code below ), and no output. Any thoughts on what I am doing wrong?

    Thanks!
    Leor

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    
    AudioInputI2S            i2s1;           //xy=200,69
    AudioOutputI2S           i2s2;           //xy=365,94
    AudioControlSGTL5000     sgtl5000_1;     //xy=302,184
    AudioEffectFlange   l_myEffect;
    AudioEffectFlange   r_myEffect;
    
    AudioConnection c1(i2s1, 0, l_myEffect, 0);
    AudioConnection c2(i2s1, 1, r_myEffect, 0);
    AudioConnection c3(l_myEffect, 0, i2s2, 0);
    AudioConnection c4(r_myEffect, 0, i2s2, 1);
    
    #define FLANGE_DELAY_LENGTH (6*AUDIO_BLOCK_SAMPLES)
    // Allocate the delay lines for left and right channels
    short l_delayline[FLANGE_DELAY_LENGTH];
    short r_delayline[FLANGE_DELAY_LENGTH];
    const int myInput = AUDIO_INPUT_LINEIN;
    int s_idx = FLANGE_DELAY_LENGTH/4;
    int s_depth = FLANGE_DELAY_LENGTH/4;
    double s_freq = .5;
    
    void setup() {
      AudioMemory(12);
    
      sgtl5000_1.enable();
      sgtl5000_1.inputSelect(myInput);
      sgtl5000_1.volume(0.3);
      pinMode(34, INPUT_PULLDOWN); 
    
      l_myEffect.begin(l_delayline,FLANGE_DELAY_LENGTH,s_idx,s_depth,s_freq);
      r_myEffect.begin(r_delayline,FLANGE_DELAY_LENGTH,s_idx,s_depth,s_freq);
    }
    
    void loop() {
        
           int pressed = digitalRead(34);
           if (pressed == HIGH ) {
               tone(33,300,10);
           } 
           
        l_myEffect.voices(s_idx,s_depth,s_freq);
        r_myEffect.voices(s_idx,s_depth,s_freq);
      }

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,069
    The tone() function requires a PWM capable pin but pin 33 is not a PWM pin. I've changed your code to use pin 35.
    I've also changed pin 34 to INPUT_PULLUP.
    Apart from the PWM pin problem, I think that continually turning on the flanger effect in loop was preventing it doing anything useful. As soon as it got started, the loop would reset it again so I've started the flanger effects in the setup() function.
    If you want to turn the flange effect on and off with a button push, you'll probably find it easier to use the Button library and then change the flanger state only when the button changes state.

    Pete

    Code:
    //See https://forum.pjrc.com/threads/54755
    //Generate square wave and feed into flanger.
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    
    AudioInputI2S            i2s1;           //xy=200,69
    AudioOutputI2S           i2s2;           //xy=365,94
    AudioControlSGTL5000     sgtl5000_1;     //xy=302,184
    AudioEffectFlange   l_myEffect;
    AudioEffectFlange   r_myEffect;
    
    AudioConnection c1(i2s1, 0, l_myEffect, 0);
    AudioConnection c2(i2s1, 1, r_myEffect, 0);
    AudioConnection c3(l_myEffect, 0, i2s2, 0);
    AudioConnection c4(r_myEffect, 0, i2s2, 1);
    
    #define FLANGE_DELAY_LENGTH (6*AUDIO_BLOCK_SAMPLES)
    // Allocate the delay lines for left and right channels
    short l_delayline[FLANGE_DELAY_LENGTH];
    short r_delayline[FLANGE_DELAY_LENGTH];
    const int myInput = AUDIO_INPUT_LINEIN;
    int s_idx = FLANGE_DELAY_LENGTH/4;
    int s_depth = FLANGE_DELAY_LENGTH/4;
    double s_freq = .5;
    
    void setup()
    {
      AudioMemory(12);
    
      sgtl5000_1.enable();
      sgtl5000_1.inputSelect(myInput);
      sgtl5000_1.volume(0.3);
      pinMode(34, INPUT_PULLUP);
    
      l_myEffect.begin(l_delayline,FLANGE_DELAY_LENGTH,s_idx,s_depth,s_freq);
      r_myEffect.begin(r_delayline,FLANGE_DELAY_LENGTH,s_idx,s_depth,s_freq);
      l_myEffect.voices(s_idx,s_depth,s_freq);
      r_myEffect.voices(s_idx,s_depth,s_freq);
    
    }
    
    void loop()
    {
      int pressed = digitalRead(34);
      if (pressed == LOW ) {
        // Turn the tone on
        tone(35,300);
      } else {
        // Turn the tone off
        noTone(35);
      }
    }

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,941
    tone() uses IntervalTimer and GPIO, so it works on any digital pin. But it's also sensitive to interrupt latency from any higher priority interrupts. You can get a more reliable & stable waveform with analogWriteFrequency & analogWrite, but only on the PWM pins.
    Last edited by PaulStoffregen; 01-02-2019 at 12:46 AM.

  4. #4
    Thanks. Got it working now. Pin 33 was also OK.

Posting Permissions

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