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

Steve programs the RGB LED to rotate through red, green, and blue colors.

Get Unlimited Access Now

Transcript from the "Programming & Debugging RGB LED" Lesson

[00:00:00]
>> Steve Kinney: So I'm back in my playground JS file. I have some of the code from earlier that I'm just gonna get rid of. Cuz I don't have any buttons anymore. Cool. So, like most things in Johnny5, I guess our new motto is, there's a class for that. new five.Led.RGB.

[00:00:27]
>> Steve Kinney: Cool, and so because, remember, when with the regular LED or the button, we said, okay, here's the pin that it's in. Now, we just did the wire together. We know that there are more than one pin, so we'll give it an object. And we'll say red is in a5.

[00:00:52]
>> Steve Kinney: Green is in a6, and if these are not where yours are, then you can just adjust these values as appropriate. And then blue is in b5, all right? Super cool. And what I'm gonna do is, I'm going to iterate over, we'll start with red, green, and blue.

[00:01:17] That seems like a good set of colors to iterate over. And we'll be able to just kind of work through each of those so I'm going to make an array of colors in the index. I'm gonna keep moving through the array and like changing the color of the light.

[00:01:31] So I'll say let index,
>> Steve Kinney: Equal 0 for starters, and we'll make a constant of colors.
>> Steve Kinney: Red, green, blue, all right. And then what we'll do is, there's a helpful method called board.loop, and it's not too much different than like a set interval or something along those lines.

[00:02:02] And we'll say every 500 milliseconds, what I would like you to do is run this function. And in this function, we're gonna say led.color, and we're gonna basically go ahead and use a modulo to figure out which color we're gonna be on. So what we'll do is we'll say colors, index + +modulo colors.length.

[00:02:36] And what that will effectively do is, so that will be the remainder of a constantly incrementing number over the length of the colors. Which basically means it's gonna be 0, 1, 2, 0, 1, 2, 0, 1, 2. Which will translate to red, green, blue, red, green, blue, cool.

[00:02:58] So what I am going to do is I am going to switch to my timer real quick. I am going to get the process moving of deploying this code. I'll switch back to this so you can still see it, as well. T2 run playground.
>> Steve Kinney: And I'll go back to this code.

[00:03:23] And I soon enough will either see a red, green, blue color changing LED, or I will not. And then I will, in the second half of that probability, have to debug and figure out what's going on. Perfect. Yep. That is definitely true. So, to repeat basically, it's not just passing in red green blue, it is pins.

[00:03:59]
>> Speaker 2: That's right.
>> Steve Kinney: Yep. It's why you pair program with 20 people at the same time. It's really effective.
>> Steve Kinney: Mob programming? Yeah, I like it. Very cool. It's one of those things, you said it and I immediately looked down at my notes. I'm like, yep, that is it.

[00:04:25]
>> Steve Kinney: All right, building our project. It is one of those things like, this is not a really long time to deploy to the device. But when there's a color changing RGB LED on the other end, it's like an insufferable amount of time. It's like what, 15, 30 seconds in reality?

[00:04:43] But,
>> Steve Kinney: All right, so console login.led is a lot of fun. You see it' as a very big object. So my code works. Looking at my LED, I can see that it's just flashing red on and off. My suspicion right now is that I've 50% chance of putting the pin in the wrong way.

[00:05:09] I'd have hit the wrong side of that 50% chance. So I'm gonna take out that console log and I'm gonna redeploy the code. And I'm gonna go over and basically pull out the LED, flip it around, and see if that solves my problem. If it does, we'll all get to see my LED flipping colors.

[00:05:24] I see other people with color changing LEDs right now. So it makes me think that something is right, and it is I who am wrong. Cool, so I'm gonna redeploy the code without that console.log in it. So right now, my LED is flashing red back and forth. Now, this makes me think there's an issue with my ground is in the wrong place.

[00:05:43] So what I'm gonna do is just pull it out and flip it around. As I said earlier, there's like a 50% chance I put it in the wrong way. All right, bring it closer to me for a second so I can see the pins. All right, so I'm missing green in this case.

[00:06:16]
>> Steve Kinney: All right, I'm gonna bring it close for one moment. Looks like we just got a little bit of a wiring issue here because I've seen all the colors.
>> Steve Kinney: Perfect. So, for a second, I had an issue where I could see all the colors, but it wasn't consistent.

[00:06:45] So it's kinda like, it's one of the problems with effectively doing this from a few feet away from the camera, which is basically some of my resistors were touching, which kind of crossed wires. So just by spreading them out, I was able to fix it. So, in this case, I'm going between red, green and blue.

[00:07:05] Really, really cool. Now, what I want you to do is take a few minutes. Turns out, you'll notice that I just typed in red, green and blue and it just like turns out red, green and blue work. That's again, some of the abstractions of Johnny5, working out really well for us.

[00:07:22] What I'd like you to do is, any valid CSS color will work. So any of the CSS, like named colors will work, also hex codes work. So go ahead, take a few minutes, and try out some different colors. Cuz you're not just limited on a red green blue LED to red green and blue.

[00:07:39] But you can actually do other colors as well. So I would never rob you of the opportunity to put in blanched almond. And some of them like aren't like perfectly represented by the LED, like black is just off, which is not really like super great. But like try out some of your favorite CSS colors.

[00:08:01] I think salmon is a color, right? It's gonna be pretty boring. Like it's not like a perfect representation of all those colors, but try out some different CSS colors and take it for a spin.