8 years 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.

        <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) {
				else {
        after js load </br>
		<button onclick="button()">Click</button>

1 Answer

7 years, 4 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;