Nodebots / Hardware Controlling an LED with Johnny Five
Transcript from the "Controlling an LED with Johnny Five" Lesson
>> Steve Kinney: Just to reiterate, one of the questions that we received as we were transitioning was we are skipping a haul. That is absolutely correct. We have the ground as the very first port but our first GPIO port is the third port, right? So we don't want to plug it into that second one.
[00:00:18] We wanna skip one to the first and the third ports.
>> Steve Kinney: So we're gonna move on from direct Tessel code to make LEDs blink to actually writing some Johnny-Five code over in this one blinking LED file. So with Johnny-Five, it is an npm library. If you have not run npm install in this directory, take a moment and do so.
[00:00:43] You can run that and then we'll write the code as that's installing, and we should be good to go. So I'll just give you one second for that. Cool. So that's installing, let's write some code together. The first thing we're gonna do is, we're gonna require the Johnny-Five library, which is, we'll just call it five because writing Johnny-Five repeatedly is not fun.
>> Steve Kinney: And we'll require johnny-five. The other thing we need to do is, we're actually gonna pull in a slightly different version of the Tessel library. As I said before, the Johnny-Five is an abstraction over a number of boards but it's not a psychic, you do need to tell it what board you'd like to use it with.
[00:01:30] So we'll pull in the Johnny-Five library and we will pull in the this Tessel-io library. And then, the next thing to do is to bring them together. So we'll say, const board = new five.Board, and that the io will be going through a new Tessel. This is going to be at the top of effectively every single file that we write today.
[00:02:00] Later on, I have included it in the files for you, but this is effectively, we are pulling in Johnny-Five and we were telling it about the board that we are using. That semicolon is not gonna work. Cool. So this will pull in Johnny-Five, tell it they work on a Tessel, get it all wired up and ready to go.
[00:02:22] So as you saw before, the board takes a little bit of time to start up, and so it fires an event, and it fires an event when it's ready to go. That event is conveniently named Ready. So we'll say board.on('ready' and again, this will be kind of something that we use in every single example that we do today.
[00:02:47] And then, we'll give it a function of what we want to do when the board itself is ready. So what we'll say is const led.
>> Steve Kinney: Say, new five.Led and we plugged it into the a0 port. If you like capital letters, you are welcome to capitalize that a, it will work.
>> Steve Kinney: Right? And the .blink takes an argument, it takes a series of arguments. I think, it all takes a callback and something along those lines. But .blink basically takes some number of milliseconds, right? That number of milliseconds is, as you could probably guess, how often it should turn on and off, how often it should blink.
[00:04:00] Does anyone have a favorite number that they wanna go with?
>> Speaker 2: 47.
[00:04:25] Technically, I could do true plus true times 200 as well. That would work. Cool. So I'll say, const led = new five.Led('a0') port. And then, we'll say, blink every 470 milliseconds. So with this done, we can go ahead and run t2 run and 01 and hit tab, and we'll get this blinking LED.js.