Multiple frequency for buzzer

I have done frequency modification of buzzer by using Buttons.

Now, I need to increase and decrease frequency continuously.

or sudo code be like…

analog values 50, 150, 255… again 50, 150, 255… repeatedly…

what will be the code for this…
please write only

Syntax:

analogWrite(‘A0’,‘a’)
The range of the value of a is 0 to 255 and the frequency of buzzer will change with different values of a
For eg:- the sound with value 100 will be different for sound with value 200

analogWrite(‘A0’,‘100’)
analogWrite(‘A0’,‘200’)

1 Like

Yes, I understand the phenomena…

but I want to move further… that, I need to change it repeatedly without control manually with buttons, It need to change or fluctuate automatically…

want that code

var counter=0;
function start_buzz()
{
    setInterval(function(){
        analogWrite("0",counter);
        counter=counter+50;
        },10000)
}

singleButton({name:"On", action:start_buzz});
singleButton({name:"OFF", action:"analogWrite", pin:"0", value:"0"});

This might work. But make sure you keep the looping time high so that your API dosen’t gets blocked. Happened to me 3 times.

1 Like

Is there any limitations to retry this?

cause my code is stop working now.
what will be the reason ?

There is some limitation for using the API.

1 Like

Thank you all…
@2018.sakshee.sawant @himanshugoyel05 @subhamruhela13
It works… I will edit nd upload my code here after sometime.

1 Like

Same mistake I have done. :grimacing:

There is one problem in your code. AnalogWrite has maximum value of 255. But since setInterval loops the code and increases counter value by 50 everytime. It will never reach 255, Infact it will have value 300 after 250. So effectively , what will happen it will mod it by 256, i.e.
300 % 256 = 44,
400 % 256 = 144 and so on…

Modify your code so that it increases in short counter value for example counter = counter + 5 and put a condition that it should stop at 255 or restart again from 0 if reaches 255.

And of course rate limiting is there, so 10 second delay is must,

2 Likes