Can we pass a variable as argument for analogWrite() function>

I wanted to know if could pass the intensity value for a pin using a variable given as an argument in the analogWrite() function. So that the user could decide the voltage supply(0-255) and hence get the corresponding intensity from the buzzer. I tried passing a variable but could’nt get an output.

Yes we can pass variable in place of intensity value.

analogWrite(‘pin numer’, ‘intensity value’)

New Syntax would be:
analogWrite(‘pin numer’, var)

Example:

x=255 //x is variable
print(mybolt.analogWrite(‘0’, x))

1 Like

Thank you so much for replying. I tried your suggestion but i am still not able to get the desired result. If it doesn’t bother you, could you please go through the code and just notify me where i went wrong.

< html >
< head >
< title >Buzzer Frequency Controller< /title>
< script type=“text/javascript” src=“https://cloud.boltiot.com/static/js/boltCommands.js”>< /script>
< script>
setKey(’{{ApiKey}}’,’{{Name}}’);
< /script>
< /head>
< body>
< center>
< input type=“text” placeholder=“Type something…” id=“myInput”>
< button type=“button” onclick=“getInputValue();”>Get Value< /button>
< script>
function getInputValue()
{
// Selecting the input element and get its value
var x = document.getElementById(“myInput”).value;
// Displaying the value
alert(x);
}
< /script>
< P>< button onclick=“analogWrite(0,x);”>EXECUTE< /button>< /P>
< P>< button onclick=“analogWrite(0,‘0’);”>EXECUTE 0V< /button>< /P>
< P>< button onclick=“analogWrite(0,‘50’);”>EXECUTE 50V< /button>< /P>
< P>< button onclick=“analogWrite(0,‘100’);”>EXECUTE 100V< /button>< /P>
< P>< button onclick=“analogWrite(0,‘200’);”>EXECUTE 200V< /button>< /P>
< /center>
< /body>
< /html>

What i am trying to execute is accepting an intensity value for the pin from the user and then pass it on to produce corresponding frequency. I am able to get value from the user, but the problem is that it is not activating the analogWrite() function. All the other analogWrite() functions given beneath the required statements are working perfectly and sound is also produced by the buzzer.

1 Like

I was having the same problem, I got the solution. I believe I can help you.
This is my code:

Bolt IoT Platform function change_voltage(){ var voltage = document.getElementById("input_voltage").value; analogWrite(0, voltage); }
    </script>
</head>
<body>
    <center>
    <div>
    <button onclick="digitalWrite(0, 'HIGH');">ON</button>
    <button onclick="digitalWrite(0, 'LOW');">OFF</button>
    </div>
    <input type="number" id="input_voltage">
    <button onclick="change_voltage();">Submit</button>
    </center>
</body>

Here I have used 3 Buttons and an input tag
The input tag takes the value of the voltage that we want to feed.
Two of the three buttons are simple ON and OFF buttons.
The last button is submit button.
I have placed an on-click event-listener on the submit button which calls the “change_voltage” function, this function takes the value of input element and feeds it to the “analogWrite” function.
Hope this helps