You are reading a single comment by @Drumline18 and its replies. Click here to read the full conversation.
  • English is not my native language, sorry if my explanations are not clear enough.

    I don't want to bother Gordon with the title-thingie but I assure you I'm supporting the project on Patreon. Thank you for your answer.

    There is no complete code. I was playing with it in the same way shown in the video at the top of Making Music, using the WebIDE. But here is the complete code from the example, in one block:

    var BUZZER=A9;
    
    function freq(f) { 
      if (f===0) digitalWrite(BUZZER,0);
      else analogWrite(BUZZER, 0.5, { freq: f } );
    }
    
    var pitches = {
      'a':220.00,
      'b':246.94,
      'c':261.63,
      'd':293.66,
      'e':329.63,
      'f':349.23,
      'g':392.00,
      'A':440.00,
      'B':493.88,
      'C':523.25,
      'D':587.33,
      'E':659.26,
      'F':698.46,
      'G':783.99
    };
    
    function step() {
      var ch = tune[pos];
      if (ch !== undefined) pos++;
      if (ch in pitches) freq(pitches[ch]);
      else freq(0); // off
    }
    
    var tune = "c    c    c   d    e   e  d   e    f   g   C  C C   g  g g   e  e e   c  c c  g    f  e   d c";
    var pos = 0;
    
    setInterval(step, 100);
    

    You can see in that code where the BUZZER terminology comes from. But I have both a passive buzzer and a speaker salvaged from a screen (no markings). Both exhibit the same behavior, and the LED too, so that's why I'm targetting the MCU/code now. No capacitors, no resistors. The buzzer has polarity. It's connected GND to GND and + to a pin, directly, just like in the tutorial.

    The confusion over L9 stating I can change frequencies instantly stems from the fact the problem depends on the state of the pin I'm trying to change.

    If a pin is already using PWM (i.e. by a previous analogWrite 0.x), the changes are instant.
    But, if the current state of the pin is either exactly 0 or 1, there is a delay before changes occur.
    But also, transitioning from 0 or 1 to an intermediary value on another pin will hang other pins.

    1 -> 0.x = delay
    0 -> 0.x = delay
    0.x -> 0.y = instant
    0.x -> 0 = instant
    0.x -> 1 = instant
    0.x -> freq change => instant

    Its like... sticking to the 1 and 0. I don't know how to better explain it. It may require a video (tomorrow once everyone is awake).

    Here is the code I am using to get that 1700ms figure :

    analogWrite(PIN, 0.5, {freq:1000});
    setTimeout(()=>{analogWrite(PIN,0)}, 1700)
    

    By playing with the timeout delay I get it to where I can hear it start, resulting in a extremely short beep. At 1700 I can still hear it but at 1690 I can't.

    I get the exact same behavior, down to the 1700ms figure, with 2 different boards of different manufacturers. I can't find the proper datasheets (I don't know which ones are the proper ones) but I highly doubt it makes a difference. One is ESP-12F and the other is ESP8266MOD. I tried all pins with the buzzer and used onboard LEDs for the visual tests.

    Following your link to source code I am reading the implementation for ESP8266.
    The code executes differently if Hardware PWM is defined, notably defaulting the freq at 50 in software mode and 1000 in hardware mode.

    https://github.com/espruino/Espruino/blo­b/624d6f2c700bfbf08e4a7eb7d282bd57648470­56/targets/esp8266/jshardware.c#L609

    Whenever I test without adding a frequency I get the same sound as if I try with freq : 50, leading me to believe it is defaulting to software right away. I don't understand much further in the source though.

About

Avatar for Drumline18 @Drumline18 started