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

Status
Not open for further replies.

propa

Well-known member
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?
 
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.
 
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
 
Status
Not open for further replies.
Back
Top