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

Thread: Audible clicks from envelope when switching from using delay to nonblocking function

  1. #1
    Member propa's Avatar
    Join Date
    Apr 2018
    Location
    UK
    Posts
    93

    Audible clicks from envelope when switching from using delay to nonblocking function

    Hi,

    Sorry the title's a bit of a mouthful! But that's the exact problem.

    this code works fine, but blocks other tasks during delay:

    Code:
        
        waveformMod1.frequency(freq);
        envelope2.noteOn();
        delay(knob_A4*1000.0+12.0); // Stop using delay 
        envelope2.noteOff();
        delay(knob_A4*1000.0+12.0); // swap delay to non blocking code
    Swapped to generic blink without delay and now I have audible clicks having their way with the sound, the weirder part is once modulated with another sound the clicks go away. Something quite strange is going on.

    This is the function that's causing the clicks:

    Code:
    unsigned long delayVal = knob_A4*1000.0+12.0;
    
    unsigned long currentMillis = millis();
    
      if (currentMillis - previousMillis >= delayVal) 
      {
        // save the last time you blinked the LED
        previousMillis = currentMillis;
    
        // if the LED is off turn it on and vice-versa:
        if (ledState == LOW) 
        {
          ledState = HIGH;
          waveformMod1.frequency(freq);
          envelope2.noteOn();
        } else {
          envelope2.noteOff();
          ledState = LOW;
        }
    
        // set the LED with the ledState of the variable:
        digitalWrite(led2, ledState);
      }
    The only thing I can think of that's different between the two, is the added delay after noteOff() in the first function, reads ON Delay Off Delay, instead of ON Delay Off.

    Has anyone got any thoughts about how the difference in functions is creating these clicks?

  2. #2
    Member propa's Avatar
    Join Date
    Apr 2018
    Location
    UK
    Posts
    93
    If anyone finds this, it's the most ridiculous fix.. (Had nothing to do with the non blocking function, and the problem came from a rouge variable modulating the sound)

    knob_A3 = (float)analogRead(A7) / 1023.0;

    even though you're turning the range of analog read into 0-1 don't use it as .amplitude(knob_A3); instead use .amplitude(1.0 * knob_A3)

    Seems like the same thing to me, but for whatever reason it seems to smooths out the values and get rid of the clicks in this instance.

  3. #3
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,089
    The code you posted isn't a function, it's a snippet and there are some variables such as knob_A3 which aren't declared so there's no way to be sure what's happening. Post complete code.
    Meanwhile, try this instead:
    Code:
    knob_A3 = (float)analogRead(A7) / 1024.0;
    Pete

Posting Permissions

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