Chicken Coop Predator Lighting - Blink - JavaScript Question

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

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;```

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

[{"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?

1 Like

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

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 )

[{"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"]]}]

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.

1 Like

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