Create a model that make a led blink for 2 diferente cicles

Hello i’m new here and i was trying to star from the https://electricimp.com/docs/gettingstarted/3-agents/
but with a little diference , i wanted to create two diferente cicles as:

if led=0
"on the device the led should stay on for a 0.5 sec"
if led=1
"on the device the led should stay on for a 4 sec"

I’m have difficulty to find an example that shows me how to tell in the function to wait an amount of time.
If someone could help-me ,

thanks

Modify this function in the device code:
// function to turn LED on or off function setLed(ledState) { if (ledState == 0) { server.log("Turn on LED for 0.5 sec."); led.write(1); \\\\Turn the LED on imp.sleep(0.5); \\\\Sleep for 0.5 seconds led.write(0); \\\\Turn the LED off } else if (ledState == 1) { server.log("Turn on LED for 4 sec."); led.write(1); imp.sleep(4); led.write(0); } }
That should work. I typed it into the forum but haven’t tested it. If you copy and paste, make sure that the code transfers correctly.

Just to be clear, if you do that, you are no longer using ledState to toggle the LED on or off, you are now using it to execute different blocks of code.

Also important to note: Using imp.sleep() will cause your imp to go to sleep and stop executing code for the amount of time that you set. You can also use imp.wakeup().
Both commands are documented here:
https://electricimp.com/docs/api/imp/

Thanks a lot i’ll test it later at home , just to be safe :wink: . what i was missing was that “sleep” part :slight_smile:
Actually i’m using this to control the switch button from my home computer and while i didn’t had your comment (thanks again :smiley: ) i made a simple bash script that run the curl http:// adress with led=1 and sleep for 4 seconds …
But now i can program it properly

:smiley: In Portugal we say if you don’t have a dog to hunt go with a cat ehehh.

A slightly more imp-friendly code snippet would be:

// function to turn LED on or off function setLed(ledState) { if (ledState == 0) { server.log("Turn on LED for 0.5 sec."); led.write(1); \\\\Turn the LED on // Turn the LED off after 0.5 seconds imp.wakeup(0.5, function() { led.write(0); }); } else if (ledState == 1) { server.log("Turn on LED for 4 sec."); led.write(1); // Turn the LED off after 4 seconds imp.wakeup(4, function() { led.write(0); }); } }

…the reason this is more friendly is that instead of sleeping (when the imp can’t process network traffic), this just registers a function to be called back after a specified time which turns the LED off. This way, the imp will stay responsive (eg to the “run” button in the IDE) all the time.

Thank you one more time for your help and explanations :wink: