Nodebots / Hardware Programming the Button
Transcript from the "Programming the Button" Lesson
>> Steve Kinney: I've left the LED, we're gonna use it a little bit later. But right now we have just this button, we wanna kinda get it working.
>> Steve Kinney: Cool, so,
>> Steve Kinney: Go back over to my code in that playground.js.
>> Steve Kinney: And you can leave that code. It's not gonna work anymore if you moved it back, so I'm actually gonna comment it out for a moment.
[00:00:27] And what I really want to do first is I want to get the button working. So what I'll do is I'll say const button is a new Johnny five.Button. Cool, and that is plugged into the a2 pin. It's worth looking at your board, the button is a GPIO, it's doesn't matter which one you've plugged it into.
[00:01:01] So if you look at your board, no, I have mine in the a3 pin, just change this code to a3. Or else move it to the a2 pin, either one is fine. This is not one of the ones where it's like, it needs to be in this certain place.
[00:01:17] Cool, and then this is a pull up button.
[00:01:43] Would anyone like to venture a guess of some of the events that could happen to a button.
>> Speaker 2: Press.
>> Steve Kinney: You could press it, right. We are actually going to look at the docs. I think it's kind of interesting. The Johnny Five docs are really great and I kind of want to take a moment.
[00:01:59] There is both API and examples. We are going to look at the API but if you go to the examples, which really kinda great is, it'll actually show you how to wire up a bunch of stuff as well and some code to execute it. We're gonna to to the API though.
[00:02:12] And we're gonna go take a look at a button. And we see the isPullup boolean. And we can kinda go ahead and we can see some of the events. So, as someone said just a second ago, is yes, events can happen on buttons and it's the ones that you think.
[00:02:32] They can be pressed, they can also be released, and they can be held. And the aliases for press and release are down and up as well, right? So when the button is down, the button is up, those both do the same thing. If one set of those made more sense to your brain than the other, do the one that made more sense for you.
[00:02:56] So what I'll do is I'll say button.on('press').
>> Steve Kinney: We'll just say console.log('pressed'). And we'll say on release, console.log release, and you can also do hold. You can do down or up, whichever one that's kinda make you happy. So I'm gonna go, and I'm gonna go back and run my playground code.
>> Steve Kinney: It's gonna deploy to my device, cool. I'm going to keep this on the terminal right now but I'm going to press the button. As I press it down, we see pressed, as I release it, we see release. Press it, it's pressed, I release it, I see release.
[00:04:04] Right, so this is the inverted flow of control that we had before. Previously we said hey, LED, I want you to blink, or I want you to pulse, or I want you to turn on or off. And we were controlling hardware with code. Now, the code is actually, or the hardware is actually firing events that we can react to with code.
[00:04:37] We can do all sorts of things. We can work with all of the Internet, as it's known, right? Now, from the physical world. And for me, that's the part where this gets really exciting, right? Okay, we can do all sorts of stuff. There's some fun stuff in this kit.
[00:04:51] There's a door sensor that whenever the two things separate, that can trigger an event. So now we can figure out how to integrate that with other things. So now we have the button in place.