Arduino Tutorial 29: Using Push Buttons to Create Dimmable LED

preview_player
Показать описание
You guys can help me out over at Patreon, and that will help me keep my gear updated, and help me keep this quality content coming:

In this lesson we challenge you to create a dimmable LED using two pushbuttons. One button will brighten the LED while the other one will dim it. The project also includes a buzzer to give user audible feedback that the LED has reached maximum or minimum brightness.

You can get the kit I am using for this series at the following link:

Also, you can get the Mastech Digital Voltmeter I am using here:

Follow this lesson on our WEB site:

#Arduino
Рекомендации по теме
Комментарии
Автор

whoever says these videos are boring just isn't excited about learning something new and useful. Great job Paul.

Mouse_
Автор

13:23 "Too long and too boring". In my opinion, the longer the video, the better. I can't get enough of this. You don't have to watch it all in one go if you don't want to. Keep up with the excellent Paul.

srduke
Автор

Hey Paul, thank you for the easy to follow and inspiring series! I’m 12 years old and I find your series innovative and intriguing. I was able to win the science fair only using the knowledge you supplied to me! BTW I figured out how to build the dimmable LED with pushbuttons😁

heinrichsutton
Автор

Paul, you are not boring. I love your lessons.
They help me keep my Alzheimer's in check

sigulf
Автор

I've waited 29 lessons to say anything, but you are a pretty damn good teacher. I'm not a math guy, but you've made the math easy to understand, and I've been able to do almost all the exercises before you've explained them because you've been so thorough as you've helped your students build their coding muscle bit by bit. I appreciate all the in-depth explanations of the physics and math, so keep it coming.

Labattfartoui
Автор

thank you Paul, your tutorials are excellent, please keep them coming.

daveharkin
Автор

Hi Paul, could you please make a tutorial about how to connect arduino to internet with esp8266, I try many codes and watch many tutorial but it really frustrating, so it will be great and helpful if you make such video

sanasabah
Автор

I was close but I couldn't quite get there without Paul

travisderose
Автор

Another great experiment you guys can do is to use the RGB LED and control the blue, red, and green color levels with the switches! But you would need 6 switches though. One switch to increase red and another to decrease it. The same for the blue and the green color. If you have 4 switches then keep one color constant, and play around with the other two by pressing their respective switches.

ahmada.
Автор

I think you skipped the part where you say what the buzzer is for 😅

FF
Автор

Your videos are not too long. I would say boring only to those who are more advanced. I am a firm believer in repetition in learning to give one a fluid understanding. I love how you repeat things, it really helps me to reinforce. Speaking as a industrial electronics technician for over 40 years. I see the potential and that is what excites me and provides the motivation to learn this coding.

silverroxx
Автор

Hello Sir this is my assignment I think that there will be 2 assignments because I saw a buzzer there so not to get confused this is the dimmable led with push button assignment.
My code:
int btnIn = 2; // Full form = button Increase
int ledPin = 3; // Led is connected to pin 3
int btnDe = 4; // Full form = button Decrease

int bright; // Brightness of the led
int waitT = 10;

void setup() {

pinMode(btnIn, INPUT_PULLUP); // Use the pull-up resistors in the microcontroller and set the pin as input
pinMode(ledPin, OUTPUT); // Set ledPin as an output
pinMode(btnDe, INPUT_PULLUP); // Use the pull-up resistors in the microcontroller and set the pin as input
}

void loop() {

if(digitalRead(btnIn ) == 0 && digitalRead(btnDe) == 1 && bright < 255) { // When btnIn is pressed and btnDe is released

bright++;
}

else if(digitalRead(btnIn) == 1 && digitalRead(btnDe) == 0 && bright > 0) { // When btnIn is released and btnDe is pressed

bright--;
}

analogWrite(ledPin, bright);
delay(waitT);
}
Thank you Sir now i'll continue watching the video:)

mayankshigaonker
Автор

I learned something--the behavior of the A0-A5 pins is different that the behavior of the analog pins on the digital side of the micro-controller. My sketch didn't work well at all using A5 for the LED output but worked OK using digital pin 9 as analog. So I've accomplished more today than I thought I would and it's not even lunchtime yet.
My solution:


// Paul McWhorter YouTube Arduino Tutorial 29
// Using Push Buttons to Create Dimmable LED

// i/o pin # constants
#define ledPin 9
#define buzzPin 3
#define upButton 5
#define downButton 6

// pb down state - for clarity and readability
#define pressed 0

// led up/down constants
uint8_t ledStep=10;
// ledMin and ledMax MUST be multiples of ledStep
uint8_t ledMin=ledStep * 0;
uint8_t ledMax=ledStep * 25;
uint8_t ledLevel = 0;

// button debouncing
uint8_t debounceDelay=200;
uint32_t msLastPress = 0;


void setup() {

// setup i/o pins
pinMode(ledPin, OUTPUT);
pinMode(buzzPin, OUTPUT);
pinMode(upButton, INPUT);
pinMode(downButton, INPUT);

}

void loop() {

// debounce delay check
if(millis() - msLastPress > debounceDelay) {

// down button check and adjust
if(digitalRead(downButton) == pressed) {
msLastPress = millis();
if(ledLevel > ledMin) ledLevel -= ledStep;
if(ledLevel == ledMin) buzzerChirp();
}

// up button check and adjust
if(digitalRead(upButton) == pressed) {
msLastPress = millis();
if(ledLevel < ledMax) ledLevel += ledStep;
if(ledLevel == ledMax) buzzerChirp();
}
// write the LED output level
analogWrite(ledPin, ledLevel);
}
}

// buzzer sound function
void buzzerChirp(void) {

digitalWrite(buzzPin, HIGH);
delay(40);
digitalWrite(buzzPin, LOW);

}

colepdx
Автор

I was able to do it on my own. I did do it differently. I used 2 nested if statements. But I did use Serial.print to check my work as I was going along. Great project. Keep up these great videos.

markfuentes
Автор

This was great fun project! Your videos are never too long or boring. Your detailed approach is very nice and unique and very helpful for deep learning. Thank you Paul for all your hard work. Also, I thought your soft “booms” were very amusing!

shvideo
Автор

Another great lesson, but did I misunderstand the assignment? I thought you instructed us to program the buttons such that a press and release would change the brightness by a small amount(e.g. +/-8), but if you held the button down, the brightness would change by a much larger amount (e.g. +/-64). So the code must detect if it is a single press or if the button is held down. Took me a while to make that work and I was really wanting to see your solution. That makes it a more challenging assignment. Maybe you'll give me extra credit? ;-) ;-) ;-)

alanoestacado
Автор

I did it on my own to and I was really stoked to see it in action. I’m 14 years old and I’m really interested in these things. I didn’t realize how much I loved coding after I bought my own starter kit. Anyways, I coded mine almost exactly like yours, just a couple format differences. When I look back it’s insane how far we’ve all come. Thank you so much Paul 👍👍

glendsouza
Автор

Paul - I could do this on my own (I am jumping and looking to do more complex things now!!) - Thanks to you!!! You are too good as a teacher!!!

robinsimpson
Автор

Yes, I got it on my own, but I did some things differently. I used an increment of +/- 1 for the LED brightness. I found I could control the ramp up/ramp down speed more easily by changing the delay time. By using 2 different delay times, I could make the visually apparent brightness change upward more closely match the change downward. I also used compound if statements to detect which button was pressed and check if the LED level was above 255 or below 0.

rukidding
Автор

I just want to comment letting you know how much I appreciate these videos. Thank you for these!! You are an excellent teacher.

F