How to create an toggle button for controlling devices

I have written an code to operate led using toggle button as switch, the code worked only for turning on the led and I am not able to turn it off I don’t know what is the correct code to turn it off can anyone help me that when I am on the toggle the led should on and when I off it the led should off .and the led is connected in digital 1 pin .
Waiting for your codes.

Hey, @indus19
which code are you asking about?
Let me clear this.there are two functions for controlling led.
1.digitalWrite
2.analogWrite
you can see the official documentations.See this

1.digitalWrite (For On and Off)takes two values–HIGH to On The Device(Led in your case) and LOW to Off The Device.
2.analogWrite(For Intensity Control) takes any integer value between 0 to 255.that means intensity of any device will vary from 0 to 255.if you’re using Led putting 0 will off your led.for example if you give 255 then your led will glow at it’s highest intensity.remember increasing the value from 0 will increase the intensity.
IMP:
If you’re connecting to digital 1 pin.then, put the digital pin number(1 in your case) in digitalWrite Function.
Note:
Don’t Share your API Key and Device_Id.you can get these things from your cloud account.
1.if you are coding in html,then write this piece of html code for toggling.

<button onclick="digitalWrite(1, 'HIGH');">ON/button>
<button onclick="digitalWrite(1, 'LOW');">OFF/button>

2.In case of Python Library,write this piece of python code for toggling.(After Importing boltiot,watch in trainings).In This case you need to give input as on or off.(by default “off” or anything you input except on will off your led)

a=input()
response = mybolt.digitalWrite(‘1’,‘HIGH’) if(a==“ON” or a==“on”) else mybolt.digitalWrite(‘1’,‘LOW’)
print (response)

hope this helps :slight_smile:

button onclick=“digitalWrite(1, ‘HIGH’);”>ON/button>
button onclick=“digitalWrite(1, ‘LOW’);”>OFF/button>
I used this code but it shows an error that there is two onclick, that mean the button onclick is for turning on and the same onclick for turning off it shows an error what to do

could you send the screenshot of error?
you must enclose two buttons with this less than < symbol.
that is
<button onclick=“digitalWrite(1, ‘HIGH’);”>ON/button> <button onclick=“digitalWrite(1, ‘LOW’);”>OFF/button>


this is my code pls check the error. and help me.


the error is in 103 line

@indus19
You can’t use two onclick event in one input tag.Below is the link to learn more about html input type.
See This Page

you should use two input tags for executing two functions.but you’re using one input tag with two onclick event that is not allowed in html and against syntax of html input tag.
try this code.(Use two input tags.)

<input type="button" onclick="digitalWrite(0, 'HIGH')" > <input type="button" onclick="digitalWrite(0, 'LOW')" >

Can we use 2 input tags in one toggle
For a single toggle will it work.

you can toggle with two input tags.
if you define one input tag for “ON” then you can’t OFF the Led.
so,Use two input tags for toggling.

Can you send me the corrected input tag alone pls

This is the code.digitWrite takes two argument 1st is pin number and second is HIGH or LOW.
carefully enter the pin.incase you connected your led to 1 then write 1 instead of 0

<html>
 <head>
   <title>Bolt IoT Platform</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="button" onclick="digitalWrite(0, 'HIGH')" name="ON" value="ON">
   <input type="button" onclick="digitalWrite(0, 'LOW')" name="OFF" value="OFF">
   </center>
</body>
</html>

Hi there , you can try to apply this code

hope this helps you