I want to check if the sliding door of my terrarium is closed using the Puck.js (light green LED if it is closed, else light red LED). I found a cool tutorial, where I can see how to do this using a magnet.
But I think a better solution would be to attach 2 wires to the Puck.js and then put a metal plate on the side of the door. If the door is closed, the circuits is also closed and otherwise it's open.
I'm not very familiar with the Puck.js so far, so I have two questions for this idea:
Thanks for your help!
Although my experience is with the Pico, my guess is that any GPIO pin will work. The software function you are after is digitalRead() http://www.espruino.com/Reference#l__global_digitalRead
Use this tutorial to garner insight. Wire Loop Game
One wire to ground. The other through a 10K to the GPIO pin. Or rely on the internal pull-up.
Thanks for you response! It works using pinMode(PIN, "input_pullup") and digitalRead :-)
Great! Glad you got it sorted! You can also use setWatch if you want a function to be called when the pin changes state.
Only think to watch out for is that if the door is normally closed (and so the contacts are normally closed) then pullup will be wasting some power (as it's like a 40k resistor is across the battery all the time) so your battery may not last too long.
You could use pinMode(...pullup);digitalRead();pinMode("input") to use the pullup resistor only when you're reading the state of the door, and not at other times
Don't worry about formatting, just type in the text and we'll take care of making sense of it. We will auto-convert links, and if you put asterisks around words we will make them bold.
For a full reference visit the Markdown syntax.
© Espruino, powered by microcosm.
Report a problem