Playing a tune using the buzzer

I wanted to use the buzzer like a piano. So I tried to place 8 buttons on the website each set to play a particular frequency(adjusted using 12 tone temperament) using the buzzer. I also put a button called play that should theoretically play Twinkle Twinkle Little Star. I can’t get it to work however and don’t know where it goes wrong.
Buzzer’s positive is connect to pin 0 and the negative terminal is connected to ground.
Here’s my code:

<!DOCTYPE html>
<html>
    <head>
        <title>Frequency Controls</title>
        <script type="text/javascript" src="https://cloud.boltiot.com/static/js/boltCommands.js"></script>
        <script>
        setKey('{{ApiKey}}','{{Name}}');
        </script>
    </head>
    <body>
        <center>
        <button onclick = 'tune(0);'> C </button>
        <button onclick = 'tune(2);'> D </button>
        <button onclick = 'tune(4);'> E </button>
        <button onclick = 'tune(5);'> F</button>
        <button onclick = 'tune(7);'> G </button>
        <button onclick = 'tune(9);'> A </button>
        <button onclick = 'tune(11);'> B </button>
        <button onclick = 'tune(12);'> C </button> <br>
        <button onclick = 'play();'> Play</button>
        
        <script type = "text/javascript">
            function tune(val){
                val = 120 * (2 ** (val/12));
                analogWrite(0, val);
                setTimeout(digitalWrite(0,'LOW'),5000);
            }
            function play(){
                tune(0);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(0);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(7);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(7);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(9);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(9);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(7);
                setTimeout(digitalWrite(0,'LOW'),1000);
                tune(5);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(5);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(4);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(4);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(3);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(3);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(2);
                setTimeout(digitalWrite(0,'LOW'),1000);
                tune(7);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(7);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(5);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(5);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(4);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(4);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(2);
                setTimeout(digitalWrite(0,'LOW'),1000);
                tune(7);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(7);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(5);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(5);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(4);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(4);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(2);
                setTimeout(digitalWrite(0,'LOW'),1000);
                tune(0);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(0);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(7);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(7);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(9);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(9);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(7);
                setTimeout(digitalWrite(0,'LOW'),1000);
                tune(5);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(5);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(4);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(4);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(3);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(3);
                setTimeout(digitalWrite(0,'LOW'),500);
                tune(2);
                setTimeout(digitalWrite(0,'LOW'),1000);
            }
        </script>
        </center>
    </body>
</html>

@aniruddhpramod

There are way too many requests at a time. Your code looks good to go. But if you play the song, you’ll be rate limited.
For reference - https://docs.boltiot.com/docs/api-access-rules

Check your email for any such mails.

With some changes in the code, like removing unnecessary digital LOW functions, picking a tiny part of a song/poem it should be good to go.

Other wise, you can use an Arduino for the longer songs/poems.

Let me know if you have any other queries.

Oh so the API access rate limits both ways? I wasn’t aware of that. Thanks this solves the problem

1 Like