Modifying Control LED program

@shoeb.ahmed @rahul.singh1 Could you possibly assist me with the code to flash an LED several times or in other words say, consecutively turning an LED ON and OFF.

For example, if we click ON button LED should be ON for 1 second, then OFF for next second, then ON, then OFF, and so on until we press the OFF button.

You could try this code and do let me know too if it works.

A slight change in the timer i.e 1000 now, there was a miss calculation. Do not set the timer to 1 second, instead change the timer to 30 seconds because we cannot make more than 20 API calls in 1 minute and hence would not give you the desired output.

1 Like

@swapnil.ravi10 Thank you! Yes, it worked but not as I wanted it to be. For 30 seconds programming, It remained OFF for 1st 30 sec and then blinks (did not remain ON for 30 seconds) and again goes off for next 30 sec. I want it to be ON as soon as I click ON button and should remain ON for 30 sec, then OFF for next 30 sec, then again ON for next 30 sec, and so on…

1 Like

Hi @akshaykumar.kumar198,

Can you share your code here so that I can edit it for you.

While posting your code, you can use ``` to embed it in the code block.

@rahul.singh1

<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>
       <button onclick="setInterval(function(){led_off(), led_on()}, 30000)">ON</button>
       <button onclick="digitalWrite(1, 'LOW');">OFF</button>
       </center>
       <script>
       function led_on()
       {
           digitalWrite('1', 'HIGH');
       }
       function led_off()
       {
           digitalWrite('1', 'LOW');
       }
       </script>
   </body>
</html>

Hi @akshaykumar.kumar198,

Apology the delay in response, I missed out on replying to this query.

Please refer the code below - (as you have asked in the first message)

<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>
         <button onclick="start()"> Start timer
         </button>
         <button onclick="stop()">Switch of time
         </button>
      </center>
      <script>
         var clear_interval;
         function start(){
           var current_state = "low";
         
           clear_interval = setInterval(function(){ 
             if ( current_state == "low"){

               current_state = "high";
               digitalWrite(1, 'HIGH');

               console.log("Now state is high");

             }
             else{
               current_state = "low"
               digitalWrite(1, 'LOW');
               
               console.log("Now state is low")
             }
           }, 3000);
         }
         
         function stop(){
           clearInterval(clear_interval);
         }
      </script>
   </body>

It will stop the timer as soon as you click on the stop button. I have set the time interval for 3 seconds because you might get rate limited for 1 second.

Do let me know in case you need any other information.

2 Likes

@rahul.singh1 Thank you. It worked exactly as I wanted.

@rahul.singh1 what will be the python code for the same logic as above?

@akshaykumar.kumar198 As you had requested for the python logic for your problem , I tried it out and got the solution: Python code

As I work mostly on repl.it , I have shared the link to the same.

Hope you got what you wanted! :smiley:

1 Like

@abishrant.dey Thank you for the response but I had already figured it out by myself. :slightly_smiling_face::innocent: