11 years, 4 months ago.

LED on/off via javascript

this code is probably incorrect, i get the logic, but i don't know the proper syntax.

i wanted to switch on/off LED1 by clicking a javascript button.

below is the code used to "turn on" LED 1, but im having a hard time creating the syntax to turn it "off" when button is clicked again.

    <head>
        <script src="mbedRPC.js" language="javascript"></script>
        <script type="text/javascript">
            mbed = new HTTPRPC();
            led1 = new DigitalOut(mbed, LED1);
			
			var i = 1;
			var o = 0;
			
			function button() {
				if (led1 == 0) {
				led1.write(i);
				}
				else {
				led1.write(o);
				}
								
        </script>
    </head>
    <body>
        after js load </br>
		<button onclick="button()">Click</button>
    </body>


1 Answer

10 years, 9 months ago.

I know this is an old post but I thought I would respond anyway.

How about making it a toggle function:

function button() {
led1 = ~led1;
}