Nodebots / Hardware Nodebots / Hardware

Programming the Magnetic Door Switch

Check out a free preview of the full Nodebots / Hardware course:
The "Programming the Magnetic Door Switch" Lesson is part of the full, Nodebots / Hardware course featured in this preview video. Here's what you'd learn in this lesson:

Steve writes code to detect when a door has been opened.

Get Unlimited Access Now

Transcript from the "Programming the Magnetic Door Switch" Lesson

>> Steve Kinney: So we have it wired up. What we're gonna do next, is just clear out some of our previous code.
>> Steve Kinney: So we'll get rid of everything in board.on('ready', cuz we're gonna change a bunch of stuff.
>> Steve Kinney: I don't need these either, or superagent. So very, very, very back to basics like we were at the beginning of this course.

[00:00:31] Cool, and so what we'll do is we'll say const door = new five.Switch.
>> Steve Kinney: And the pin is a2, unless I made a terrible mistake wiring up before.
>> Steve Kinney: We'll leave this when we have to invert. If it's the wrong one, we'll actually switch it in a second.

[00:01:00] Cool, and it's very much like the button. With button, it was either pressed or released. This is a switch, so we'll say the switch basically on a certain level is either open or closed. And that is not because this is a door switch, it is just the circuit is either open or closed.

[00:01:18] It just happens to be that this is a door, which is arguably either open or closed in this case.
>> Steve Kinney: door.on('open') and we will just console.log for now.
>> Steve Kinney: Say console.log('open').
>> Steve Kinney: door.on('close'.
>> Steve Kinney: It's console.log('close'). Cool, I'm gonna deploy that, and I'll switch back over to this, in case you're reading along with me.

>> Steve Kinney: All right, cool, give that a second or two, so you all catch up. So very, very similar to about a. This is a very simple component, even though the applications are more fun than a button.
>> Steve Kinney: I'm feeling ready to check in on my code. So it is currently closed, which makes sense.

>> Steve Kinney: I can't show both the door switch and the code at the same time, so believe me. For those of you not in the room, I am about to reach over and separate the two components.
>> Steve Kinney: Separate them, I knocked into it first, that's why you saw all that data.

[00:03:06] They are separated.
>> Steve Kinney: Reaching is not super helpful here, cool.
>> Steve Kinney: We can open, we can close. Now, there is a chance we wanna either throttle that or bounce it. Generally speaking, if you are not reaching over a table in front of a bunch of people, it is a little bit more consistent on open and close.

[00:03:27] But as I keep bumping it and the wires move, we see both. Well, we can see everything is open and we can see where everything is closed, and that is pretty cool.