Hardware with Arduino & JavaScript

Hardware with Arduino & JavaScript Controlling LED with Button Exercise

Check out a free preview of the full Hardware with Arduino & JavaScript course:
The "Controlling LED with Button Exercise" Lesson is part of the full, Hardware with Arduino & JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to modify the code so the button controls the LED light.

Get Unlimited Access Now

Transcript from the "Controlling LED with Button Exercise" Lesson

>> I'm gonna give you a few minutes. Your job is you now have an LED and a button. LEDs can go on and off. Buttons can go down and up. In fact, I'm not even gonna tell you that when the button goes down, the LED should go on.

[00:00:18] If you wanna go the opposite way, that's fine. But like, if you need some guides, I would say button goes down, LED should go on. Button comes up, LED should go off, right? We will now blend the two things. If you wanna get crazy and fade the button in, you're on a pulse-width modulation pin.

[00:00:36] You do what you want. But generally speaking, control the LED in a way that makes you happy from the button in a way that pleases you. All right, so our mission was simply to try to see if we can get the button to turn, or the LED to turn on when the button is pressed and turn off when it's not.

[00:01:00] So kind of combining the two here. So ideally what we ought to be able to do is to say when the button's down we wanna do LED on. When the button is off we wanna do LED off. So this is my sanity check as well to make sure that this all works.

[00:01:16] Load it up and back over I go. Hopefully find out, okay so we got a look. Did I miss anything up? Check out my code I'm gonna start by just doing a quick console log coz that was working so verify visit my button for, I didn't change much while we were wiring this all up, so we'll find out.

[00:01:56] All right, let's see, is it my button? The button works so it looks like it's my LED going on and off. That's our issue at this moment. So let's take a look. No shouldn't go into the ground. So the next thing to do to triage this a little bit is to see, can I get my LED to blink?

>> Someone asked online, the wire for the LED doesn't go to the ground?
>> I'm off by one, that's it. I was just off slightly from this angle. All right, code work is off by one, which is the ground from the LED. And then that ground wire there was just off slightly so, I was one position over.

[00:02:50] So, fixing that, solved my issue. So, there's some other things I can play around with here if I wanted to explore. I can, for instance, we could say. Via that fade in and fade in, I believe takes a number of milliseconds we say fade in over half a second.

[00:03:12] And then we can fade out. Let's try that out as well. And again, if you're not plugged in to a pulse width modulation pin, then you will get an error if you try to fade in fade out, coz fading involves something between a 0 and a 1. So try that out.

[00:03:41] Fades in, fades out. My fade out was a little bit slower than my fade in cuz I didn't put a value in here, which is totally fine. The code still works. Now we can fade in and we can fade out on that same pin as we hit the button.

>> What is the default value for a fade in and fade out?
>> It doesn't say clearly in the docs, but my sense is given the fact that I have 500 milliseconds for the fade-in, and the fade-out seems slower. I'm going to guess, I can probably dig into the actual source code over lunch, but, like, my guess is that it's a thousand.

[00:04:17] And I'm doing that from, that seems fast that seems slower [LAUGH]. Seems by my scientific calculations about twice the length and speeds I'm gonna guess. And also like 1000 seems like a good default. So all of those factors combined I'm gonna go with 1000, but as you can see that is not based on like particularly good science that is based on feels.

[00:04:40] Just like most of JavaScript.
>> Yeah just a little question about the circuit. Why does the button need the resistor if it's going to ground?
>> Yeah a big part of that is for just the, to stabilize the amount going, otherwise, there's a variability we can get false positives, right?

[00:05:01] So having a certain amount of standardization, whereas a certain amount is getting absorbed no matter what is easier for the circuit to leave, I had to research that. And I'm like, no one's gonna ask me this question, but I should know [LAUGH] cuz I was like, why? And that is the case.