# Chicken Coop Predator Lighting - Blink - JavaScript Question

**URL:** <https://discourse.nodered.org/t/chicken-coop-predator-lighting-blink-javascript-question/76972>\
**Category:** General\
**Created:** [26 March 2023 22:23 UTC](https://discourse.nodered.org/t/chicken-coop-predator-lighting-blink-javascript-question/76972 "2023-03-26T22:23:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![richiep](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@richiep](https://discourse.nodered.org/u/richiep)\
**Post date:** [26 March 2023 22:23 UTC](https://discourse.nodered.org/t/chicken-coop-predator-lighting-blink-javascript-question/76972/1 "2023-03-26T22:23:59Z")

</div>

Hey All,  
I'm putting together an esp8266 board with motion sensors and lights to help protect my chickens from nighttime predators. When the motion sensor is tripped I want to blink lights for a short period and then hold them on for a few minutes.

I've found some JavaScript that will blink the lights but I don't know how to make it stop, the code below just blinks for infinity. How can I tweak this so that the lights blink, say for 10 seconds and then stay on? I'll control the rest with NodeRed.

Thanks

````auto
var BLINKDELAY = 250;
var light = true;
var blinker = setInterval(blink, BLINKDELAY);
global.set("blinker", blinker);
function blink () {    
    if (light) {
        msg.payload = 1;
        light = false;
    }
    else {
        msg.payload = 0;
        light = true;
    }
    node.send(msg);
}
return;```
````

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [26 March 2023 22:43 UTC](https://discourse.nodered.org/t/chicken-coop-predator-lighting-blink-javascript-question/76972/2 "2023-03-26T22:43:49Z")

</div>

> [@richiep](#):
>
> I'll control the rest with NodeRed.

I think this should work  
Edit - this is a bit more elegant

 ![Untitled 3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/2/52aaa7e320071fcf59bb6b47e8ffd3937cc5358a.jpeg)

```auto
[{"id":"03129cf49825b270","type":"inject","z":"72e3ac42d134d33c","name":"Start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":240,"wires":[["f96a76b2292e28c8"]]},{"id":"6b1863c5746ab6bd","type":"debug","z":"72e3ac42d134d33c","name":"debug 295","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":360,"wires":[]},{"id":"f96a76b2292e28c8","type":"trigger","z":"72e3ac42d134d33c","name":"Blink for 10s","op1":"go","op2":"reset","op1type":"str","op2type":"str","duration":"10","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":370,"y":240,"wires":[["ccb505782331f0fa","8e79cb75e3504cdc"]]},{"id":"8e79cb75e3504cdc","type":"change","z":"72e3ac42d134d33c","name":"Then ON","rules":[{"t":"set","p":"payload","pt":"msg","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":360,"wires":[["6b1863c5746ab6bd"]]},{"id":"83ca5006a975aa22","type":"group","z":"72e3ac42d134d33c","name":"Blink","style":{"label":true},"nodes":["ccb505782331f0fa","7e8d6345bf57d4ef"],"x":514,"y":199,"w":252,"h":142},{"id":"ccb505782331f0fa","type":"trigger","z":"72e3ac42d134d33c","g":"83ca5006a975aa22","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"-500","extend":false,"overrideDelay":false,"units":"ms","reset":"reset","bytopic":"all","topic":"topic","outputs":1,"x":640,"y":240,"wires":[["7e8d6345bf57d4ef"]]},{"id":"7e8d6345bf57d4ef","type":"trigger","z":"72e3ac42d134d33c","g":"83ca5006a975aa22","name":"250ms blink","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"250","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":630,"y":300,"wires":[["6b1863c5746ab6bd"]]}]

```

Will foxes be deterred by blinking lights?

---

<div class="post-metadata">

**Author:** ![richiep](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@richiep](https://discourse.nodered.org/u/richiep)\
**Post date:** [26 March 2023 23:00 UTC](https://discourse.nodered.org/t/chicken-coop-predator-lighting-blink-javascript-question/76972/3 "2023-03-26T23:00:49Z")

</div>

Beautiful! Thank You!

> Will foxes be deterred by blinking lights?

I hope so. I'm gonna position the lights lower, at fox, coyote and racoon eye level. We'll see how it goes.

Thanks again!  
Rich

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [26 March 2023 23:01 UTC](https://discourse.nodered.org/t/chicken-coop-predator-lighting-blink-javascript-question/76972/4 "2023-03-26T23:01:48Z")

</div>

If you want to experiment with javascript .. you could send a `msg.reset` value to clear your timer  
(and do a check before creating it)

test flow ( **EDIT :** fixed where multiple timer were created )

```auto
[{"id":"d1a5055351b39cd2","type":"inject","z":"54efb553244c241f","name":"start","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":490,"y":2020,"wires":[["fd28f1d954d39f50"]]},{"id":"fd28f1d954d39f50","type":"function","z":"54efb553244c241f","name":"blinker","func":"let BLINKDELAY = 250;\nlet light = true;\nlet blinker = global.get(\"blinker\");\n\nif (msg.reset === true) {\n node.warn(\"RESET\");\n clearInterval(blinker)\n global.set(\"blinker\", undefined);\n}\nelse if (!blinker) {\n node.warn(\"blinker\");\n blinker = setInterval(blink, BLINKDELAY);\n global.set(\"blinker\", blinker);\n}\n\n\nfunction blink() {\n if (light) {\n msg.payload = 1;\n light = false;\n }\n else {\n msg.payload = 0;\n light = true;\n }\n node.send(msg);\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"dns","module":"dns"}],"x":650,"y":2060,"wires":[["042cd7614f931375"]]},{"id":"042cd7614f931375","type":"debug","z":"54efb553244c241f","name":"debug 36","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":840,"y":2060,"wires":[]},{"id":"b22d06079c73066c","type":"inject","z":"54efb553244c241f","name":"reset","props":[{"p":"payload"},{"p":"reset","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":490,"y":2100,"wires":[["fd28f1d954d39f50"]]}]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [26 March 2023 23:08 UTC](https://discourse.nodered.org/t/chicken-coop-predator-lighting-blink-javascript-question/76972/5 "2023-03-26T23:08:39Z")

</div>

My suggestion is not robust enough, it's possible for the last blink off to get sent after the switch node sends on.

To fix it, put a delay node set to a bit more than the on/off duration between the switch node and the output.

 ![Untitled 5](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/4/c43e1ab60f2c5d898ff35def14e5f58deb93b23e.jpeg)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 April 2023 23:08 UTC](https://discourse.nodered.org/t/chicken-coop-predator-lighting-blink-javascript-question/76972/6 "2023-04-09T23:08:42Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
