B4R Tutorial tone() function

Here is a tone function that does Arduino's tone()
The buzzer is connected between pin 6 and ground :
Code: Copy
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Delay( 1000)

    tone( 6 , 500, 500)
    Delay( 100)
    tone( 6 , 750, 500)
    Delay( 100)
    tone( 6 , 1000, 500)
    Delay( 100)
    tone( 6 , 1500, 500)
End Sub

Sub tone(Pin As Byte, freq As Int, duration As ULong)
    Dim sound As Pin
    sound.Initialize(Pin,sound.MODE_OUTPUT)
    Dim d As ULong = 500000/freq
    Dim tnow As ULong = Micros
    Do While Micros - tnow <  duration * 1000
        sound.digitalWrite(True)
        DelayMicroseconds(d)
        sound.digitalWrite(False)
        DelayMicroseconds(d)
    Loop
End Sub

For some reason I get a sound when the program starts, in the parameters of the first tone line. I inserted a 1 sec delay to eliminate it.
 
Last edited:
With B4R, WE don't "Run from IDE" since WE can only compile in release mode.
So, after compiling, the board restarts it self and runs the newly installed program
 
Hi

I needed my program to run even when I was playing a tone.
This is an example of how to do the same without using delays.

Jan
 

Attachments

Hi,
Thanks for this tutorial.
Is the buzzer connected directly to GND or through some buffer circuit, or it has a current limiting resistor?
What's the minimum impedance of the speaker or buzzer? (an output circuit will help).
 
Hi,
Thanks for this tutorial.
Is the buzzer connected directly to GND or through some buffer circuit, or it has a current limiting resistor?
What's the minimum impedance of the speaker or buzzer? (an output circuit will help).

It is a piezo buzzer connected directly to GND and pin 8.
 
works fine with a buzzer connected to pin 12 & GND (next to each other and the only GND pin) on a WeMos Lolin32 Lite aswell.

Thanks derez!
 
Back
Top