Exactly the explanation I've needed for the past few months to continue with my project! Thank you immensely for making this simple.
@tomassasovsky44124 жыл бұрын
#define button 3 //Push button on D3 #define redLED 5 //red LED on D5 #define greenLED 6 //green LED on D6 #define yellowLED 7 //yellow LED on D7 int state = 0; //integer to hold current state int old = 0; //integer to hold last state int buttonPoll = 0; //integer to hold button state void setup() { pinMode(button, INPUT_PULLUP); //button as input pinMode(redLED, OUTPUT); //LEDs as output pinMode(greenLED, OUTPUT); pinMode(yellowLED, OUTPUT); digitalWrite(redLED, LOW); //set initial state as off digitalWrite(greenLED, LOW); //set initial state as off digitalWrite(yellowLED, LOW); //set initial state as off } void loop() { buttonPoll = digitalRead(button); if(buttonPoll == 1){ delay(50); buttonPoll = digitalRead(button); if(buttonPoll == 0){ state = old + 1; }} else{ delay(100); } switch(state){ case 1: digitalWrite(redLED, HIGH); digitalWrite(greenLED, LOW); digitalWrite(yellowLED, LOW); old = state; break; case 2: digitalWrite(redLED, LOW); digitalWrite(greenLED, HIGH); digitalWrite(yellowLED, LOW); old = state; break; case 3: digitalWrite(redLED, LOW); digitalWrite(greenLED, LOW); digitalWrite(yellowLED, HIGH); old = state; break; default: digitalWrite(redLED, LOW); digitalWrite(greenLED, LOW); digitalWrite(yellowLED, LOW); old = 0; break; } }
@mrgraememell45265 ай бұрын
Very useful tutorial. One change in the in default case - the yellowLED should be set to LOW
@tomassasovsky44125 ай бұрын
@@mrgraememell4526 thanks for the code review, man! I just edited the original comment. awesome to see this is still being used
@seditiousmonkeyart5 жыл бұрын
Best and simplest explanation and demonstration of debouncing I have found. Thank you for making and sharing this with us. looking forward to more.
@casperjuniorviriri71776 жыл бұрын
This tutorial actually works the best, for a day i wrote the same code over and over again and it wasn't working, jus to realise i was typing "state = old = 1" when it should've been "state = old + 1)....Just goes to show how the smallest mistake can make code non functional.
@chbonnici6 жыл бұрын
Dear Casper please can you send me the code to my email : chbonnici@gmail.com . I checked over and over the code but still cannot get it working and when I compile it compiles OK. Unbelievable but true.
@jlr36365 жыл бұрын
Thanks for the clear and concise video. After reading the remarks I’m amazed at the lazy people. You took the time to produce the video, purchased the equipment to produce the video, and put out a video to help other people learn something you already knew. All anyone needs to do is take their own time to re-wright the code as is shown, they MIGHT learn something by doing it themselves. And they MIGHT actually have to take their own time to watch multiple videos from multiple sources and learn how to combine them to produce what they want. They might learn something to help themselves or I guess they could just go to the store and purchase the item (already built) that they are trying to build. “Life is hard, then you die”. Thanks for your time !
@learnelectronics5 жыл бұрын
THANKS
@alloycrow9175 жыл бұрын
I agree, This has been the best explained video I have seen in regards to both code and hardware setup. and the code is not even that long for him to post it, millenials and their instant gratification...
@KOAlleyCat Жыл бұрын
Yeah, man. That’s exactly how I learn. When I first read the comments I just assumed he didn’t even visually show the code. Then I came to the 3:14 minute mark and the code is right there in front of their faces! Is writing it down really that difficult to some people? 😂
@scottneels26283 жыл бұрын
Perfect, Thanks for this. I've been trying to get clear on state changes and this finally made it click for me. Go well!
@alloycrow9175 жыл бұрын
Man, best and most organized code. Keep those tutorials coming!! Cheers.
@lukass16043 жыл бұрын
Coming form a programing background, the code isn't clean at all lol
@fish94683 жыл бұрын
@@lukass1604 coming from an asskisser background, only assembly is clean B) libtard owned
@nathanharding77375 жыл бұрын
Thank you for the awesome video! Also, thank you for not posting the code. This forced me to write/debug my own code, and in the process learn a lot.
@clairecheng9134 жыл бұрын
agreed!!! :)
@chbonnici6 жыл бұрын
Thank you I solved the problem one missing = ALL works perfect. Great explanation
@karlcheng_52103 жыл бұрын
thank you very much!!!! I did search and learn for this effect around few days! And watched your video unexpectedly! I must say I am very new in arduino, however you teach me something in a short term! VERY appreciate!
@RealGenX2 жыл бұрын
Thanks a bunch for this, I’m new to programming and this will definitely help me on a relay project I’m working on!
@JoxMiguel3 жыл бұрын
Muchas Gracias! justo lo que estaba buscando para mi proyecto, Saludos desde México!
@dr.mailman Жыл бұрын
Thanks, this might have been exactly what I was looking for. Im inexperienced with electronics and was looking for a way to switch states on a timer but I am experienced with coding so this gave me the push in the direction to writing the code to do what Im looking for.
@errolsternberg81854 жыл бұрын
Thank you now i finally understand the button state and why it is done.
@ericr19544 жыл бұрын
Thank you! Extremely organized and easy to follow code, great work.
@markdropesr.182 Жыл бұрын
Thank you this is what I was looking for to work with my smart car.
@tmburns44 жыл бұрын
Great video, super informative. One request: when showing code on the screen, please zoom in on it so it is more readable on KZbin. Some of us are already watching on small screens. Thank you!
@howlround2 жыл бұрын
You always have the perfect video. Great stuff. Thanks!
@Arthurk3465 жыл бұрын
i wish the day i ordered RGB LED's with common anode never existed... but it worked somehow. thanks!
@s001software93 жыл бұрын
Exactly what I was looking for!! Thank You!
@tj93823 жыл бұрын
Very nice neat setup and good code layout.
@naboulsikhalid77635 жыл бұрын
very instructive, clear cristal. wonderfull tutorial. it helped me with a project. thank you for sharing your knowledge.
@TJenga2 жыл бұрын
This is almost exactly what I’m trying to do. Now I need to figure out how to add a second button to switch between a different set of lights and continue using button one to switch states. Any hope on you making that video before I figure it out on my own? 😅
@TheFairytail4ever4 жыл бұрын
This debouncing routine helps me a lot m8 :D cheers!
@grudge5795 жыл бұрын
Lovely work. Thanks for the video, it is going to be used for my robot's sensor calibration
@ADhiman19917 жыл бұрын
Thank you ,this is what i was looking for.
@ricardocruz90667 жыл бұрын
I congratulate you, very good job . Hey you can perform an exercise in which you have two buttons and two led to turn on the first and turn off the second at the same time.
@pdxfunk4 жыл бұрын
Super helpful, thank you! Was looking for just this.
@RAILWAY_FILMS6 жыл бұрын
I really enjoyed your video. You do a great job of explaining this sort of thing. Thanks.
@learnelectronics6 жыл бұрын
Thanks. There's over 300 videos on my channel.
@johneagle43842 жыл бұрын
Very clear explanation!
@gary16793 жыл бұрын
Finally, I found this video!!!!!!!!!!!!!!!
@bens4234 жыл бұрын
This is the video i searched for
@mohd.rizanabdulrahim65393 жыл бұрын
Ur code is perfect. If I press on and set a timer for e.g. 30 sec, and then it off automatically after 30 sec, what is the code for each case?
@BellaSkinUS7 жыл бұрын
I like it. I would like to know how to add some blinking to the LED's , let's say when red is is on will stay ON for 2 seconds and off for 1 sec, and green ON for 3 seconds and OFF for 0.5 sec, and yellow ON for 4 sec and OFF for 1.5 sec. Every time we push the button each led will have that blinking cycle and finally all of them OFF at the 4th push on the button. Thanks
@learnelectronics7 жыл бұрын
+Hernan Melgar Personal support is available to patrons. www.patreon.com/learnelectronics
@HackTechMW2 жыл бұрын
Thanks man this is all I wanted😊😊
@goatelope75394 жыл бұрын
Thats awesome!!! To use the same button state code to change color patterns on a neopixel strip, Instead of commanding different pins, i could paste in the rainbow/animation code, right? or is it more complicated than that? I'm new and getting into Cosplay and lighting projects. Thank you
@aarondsouza10243 жыл бұрын
That was amazing it helped me out so much thank you very much
@Dunalduck6 жыл бұрын
Thank you, for a beginner this was great video
@learnelectronics6 жыл бұрын
Welcome
@RSchef7 ай бұрын
Important concept!
@hansifansi5 жыл бұрын
Thank you for you video. This helped me a lot. I have a few questions. 1. Is it possible to add a function that makes a long press of the button the code to jump to defult? And 2. Is it possible to add a second button and for both buttons to control their own switch cases (button 1 for case 1,2 and 3, and button 2 for case 4, 5 and 6)?
@v0llk0rnbr0t203 жыл бұрын
Yes, this is exactly what I need!!!
@melaniewilson-bruneau90793 жыл бұрын
Hello, what would you change to get the following: 1st button press = LED1 2nd button press = LED2 3rd button press = LED1 & LED2 4th (1st) button press = LED1 (start over again)
@Kinko017656 жыл бұрын
Thank you !!!!!! This video is helping me a lot .
@reubeyonduty6 жыл бұрын
Ok, I set this up with a button and changed a few things... Instead of turning one led on at a time, I have them all turn on one at a time then shut off. That was easy enough to do. Now, I want to make that exact thing happen with an rc transmitter and receiver. I have the pwm signals on the channel 2 stick set so that 1 is neutral, you push it up, it reads a 0, if you push it down it reads a 2. So in theory I want to push up on the joystick, it turns on the first led, push up again it turns on the second, and push up a 3rd time it turns on the last led. Push up a 4th time it shuts them all off. If I push down on the joystick its puts power to an output pin. Take you finger off the joystick power stops. Can you help me. Ive been trying to do this for days! TIA, any help would be appreciated!
@damianrecinski73294 жыл бұрын
What if button has been pressed and released during 100ms delay? It would not be detected. Another error: if button would be released after checking `if (buttonPoll == 0)` and before `if (buttonPoll == 1)`, then release would not be detected again
@faroff44353 жыл бұрын
You could do the same but with 3 buttons and change state as it is pressed, button1- led1, button2- led2 ... what would be the modification?
@qzorn44403 жыл бұрын
nice video, i have an Arduino Pro Micro setup for keyboard emulation and it works great on the bench. however, when it is hooked up on a machine with a DC motor and not connected to the arduino, and with the one pro input connected to common to act as a switch input. the F11 keyboard command keeps strobing. any ideas before making my changes? thanks a lot...:)
@DeepSpaceAlien5 жыл бұрын
How can you add delays between the leds going on or off in 1 case
@alejandraurena35334 жыл бұрын
Thank you! This really helped me
@sam-lg4 жыл бұрын
This really useful project 👌
@bagusadikin74164 жыл бұрын
thanks really helpful, and i was thinking can we use Interrupts function to replace the button? to replace it HIGH and LOW signal?
@patracy5 жыл бұрын
Thanks for the basics of this. How would I define this to cycle backwards in the same pattern but reverse instead of off. Is that just basically adding more cases that invert the pattern until we wrap up at the first state?
@xsoul68385 жыл бұрын
i need button Handling Multiple States with if else ?
@RCBros20005 жыл бұрын
im using this to run an LED tracer loop with different settings. i noticed with this code it will only change modes if i press the button between the time the last led goes off and the first led fires. is there any way around this?
@georgechambers31977 жыл бұрын
This is a very good explanation of states. Would it be possible to have the code and a wiring diagram with these videos? I've programmed in C a lot but it was many years ago. Thanks for the video.
@dekaezyofficial97406 жыл бұрын
thank you for sharing, i have tested your sketch and succeeded, but there is a little problem, yellow led flame is not like bright red and green light, i have replaced the led with new one and change output pin, but it did not work, am i miss something?
@joverstreet245 жыл бұрын
Excellent tutorial.
@DocAlexLego5 жыл бұрын
I may use this for a Star Trek Phaser Type 2 Cobra Head Display where Each LED must stay on like this -=OFF *=ON **------ ******** This requires multiplex and I'm hoping if this can work by telling the arudino to keep the last LED on and then light the other and so on - if I hold the other button it resets the phaser or move down if I press it. But will these States work in the multiplex format?
@MattG1016803 жыл бұрын
Hey, Im trying to program a switch like this to just toggle between basically 2 lights, how would you code it to just be 2 lights and not have a full off except when first powered up?
@umiturgutaswwsa3 жыл бұрын
Can anyone help me about my problem?My setup just runs the code on the default mode.I cant switch on states.
@electron4me3 жыл бұрын
Good Afternoon. Thank you for a great tutorial. I'm wondering if you could advise me how to tailor it to my requirements? I'm using an hall switch to switch the states. the important pins in my example are one and two in each case. This case is turning on and off an electromagnet but has it is case one is turning the magnet on and case 2 off. What I'm looking for is case 1 to turn on and then turn off before case 2 happens and then the same in case two. I've been on it all day and cant get it to do this. I'd really be grateful for some advice. Many thanks. switch (state) { case 1: digitalWrite(magnet_pin_one, HIGH); digitalWrite(magnet_pin_two, LOW); digitalWrite(dial_pin_three, LOW); digitalWrite(dial_pin_four, HIGH); old = state; break; case 2: digitalWrite(magnet_pin_one, HIGH); digitalWrite(magnet_pin_two, LOW); digitalWrite(dial_pin_three, HIGH); digitalWrite(dial_pin_four, LOW); old = state;
@karlcheng_52103 жыл бұрын
is it possible to change the case statement be blink and fade without delay?
@antoniogutierrez32545 жыл бұрын
Hello, amazing project, how must i change the code if i want to use a Hall effect sensor instead of a button to have the same beheavour of the leds? thank you very much!
@bagusadikin74164 жыл бұрын
have you already made it work? im intended too, to use digital Interupts to replace the button so the LED can be controlled by a signal or sensor
@jimmywang78626 жыл бұрын
nice tutorial just what i was looking for. I do have on question in fact, can i place the digitalwrite in each case with other codes like analog write?
@learnelectronics6 жыл бұрын
Yes you csn
@leonbakeroot32914 жыл бұрын
How can I put different delays on each LED and still have it to go to the next button if I press?
@marcodoria56355 жыл бұрын
Obrigado por ajudar um amigo brasileiro!
@OutOfNameIdeas23 жыл бұрын
I need to make this work with the button being on 1v and not 5v. Is this possible? It doesn't work for me.
@xsoul68386 жыл бұрын
it would be more perfect. if you could upload the code for practice
@topten70645 жыл бұрын
You are really great
@Design.Studio-DS7 жыл бұрын
Very useful tutorial. its perfect. but i would like to know how to do something after press the button. i mean, if i need to flash RED bulb with 1 sec delay after first press and so on.. i tried to do it. but if i do so, next bulb not response after second press of the button. could you please explain me how to use this type code for handle multiple programs just using one button press. if it is possible, it will be able to use as a menu selector & will be v.usefull to automation.
@habibhabibov4610 Жыл бұрын
Hi you are the best really thank you 👏👏👏💪
@everythingcool4913 жыл бұрын
Hi, thanskfor the tutorial, ive followed it to a tee, but it never lights up the first LED,?
@buzzbuddi68463 жыл бұрын
Hi, how to add timer function? I wanna all leds turn on for one second only.
@jamesanderton9036 жыл бұрын
Thanks, well explained.
@deanbrothersfilms4 жыл бұрын
Would it be possible to swap out the button with a mini photocell so different levels of brightness would switch on one of the 3 lights?
@learnelectronics4 жыл бұрын
Sure
@VictorRodriguez-cg1kg4 жыл бұрын
Hi, hope you can help me out. Basically I want to do the following: I want to control two LEDs separately using one single push button, example if push the button once LED 1 will lit up and immediately turn off (momentarily) and if I push the button again now LED 2 will lit up and turn off immediately, the cycle should repeat. Thanks.
@learnelectronics4 жыл бұрын
Look at this video: kzbin.info/www/bejne/hnzadmRmds6al8U You will have to adjust the code to make it momentary but it will do what you want.
@VictorRodriguez-cg1kg4 жыл бұрын
@@learnelectronics thank you
@akokJ6193 жыл бұрын
for some reason say if I hae 6 leds and 2 toggle switch and I want to divide 3 leds each for each switch, I put the same code but it doesn't work for one set. Please help me out
@scottneels26283 жыл бұрын
Toggle switches are not momentary so you have no rising edge like what happens when he lifts his finger off the button.
@AmateurInventor5 жыл бұрын
Good job buddy
@santoshbabu5174 жыл бұрын
can we turn back LED on to the stste in which it was after poer failure
@musaaftabi62693 жыл бұрын
hello i make a project with arduino uno and IRremote and stepper motor .the remote control turn the motor shaft to the right and left. i need a little help with the codes. is it possible to define just one button of remote control to turn the shaft to right and left ??? how to write the code ? i can send the codes i wrote if its needed.
@juangil69256 жыл бұрын
Hi. Very/extremely useful. My respects. Thank you for sharing!. 2 questions, if you could please advise me. If I need to go back from case 3 to 1, instead of going OFF all 3 lights, I guess I should make a case 4 with Case 1 statements, and at the end, write OLD = 1, and then, it should keep cycling, right?. Second question... to apply this for a servo, I guess that I should load SERVO library, and state the angle I want for the position of servo ( servo.write(angle) ), and I should be able to cycle trough different pre-programmed positions with the click of the button. (ej_ 0, 90, 180, 0, 90, 180..). Off course, after selecting the angle outputs to the proper/same PWM Output.. Do you think it might work? I have the project on mind, and getting all components in a week, so, I am gathering all information I can, before I can do some testing. Juan
@learnelectronics6 жыл бұрын
Yes you are on the right track in both cases. You will definitely need the servo library for your second question.
@juangil69256 жыл бұрын
I will work on this base once I get all the components. Thanks for sharing!!
@WiktorErikAndersson3 жыл бұрын
I have a problem.. I wrote a code using this format! Works great but.. in case 2 I want to ad a delay that will turn of one LED after 3 sec! And I know how to do that but the arduino loops that delay 3 times for some reason! Any idea?
@PrashantPatil-zg4cz3 жыл бұрын
If we use delay cmd then its not switching to next case.
@boopeshkumarprabhakaran3 жыл бұрын
hi how to make arduino remember the last active led when power failure?
@Ifa-0976 жыл бұрын
i have problem with case 3, after case 3 running, lamp red is on, actually i not push button
@rahmatramadhana934 жыл бұрын
why not always in case 1? will change to case 2 randomly evaery time
@mohitarora87546 жыл бұрын
If you press three times continuously then third LED will Glow or not??
@MrDickales5 жыл бұрын
I typed your code exactly but consistently get "expected unqualified-id before '/' token....am i missing something?
@Emilsteixner4 жыл бұрын
i had that too... i just deleted it but now i get: expected primary-expression before ',' token
@kushalgarg33666 жыл бұрын
Can anyone give me an explanation of why we put a delay of 100 ms in the else part of after the debouncing routine?
@chagadunudev95833 жыл бұрын
Thanks heaps!!!!
@noureldinabdelrhman28604 жыл бұрын
thank you so much
@aimless38277 жыл бұрын
My light number 7 barely working. But other working good. Why?
@chbonnici6 жыл бұрын
How to introduce a timer of 10sec so when button is pressed the led switch of after 10 sec without loosing the sequence
@a6dulsalam5115 жыл бұрын
You have to use millis funcrion
@pacmangsxr750 Жыл бұрын
Thank you
@TheFlybyman6 жыл бұрын
Thanks !!!!!!!!
@cghkjhjkhjhvfghc7 жыл бұрын
nice thanks..100th thumbs up.
@Tix3d5 жыл бұрын
thanks so much :)
@sintamaritobrbakkara30724 жыл бұрын
are there the coding in pdf?
@shayaflynn86957 жыл бұрын
Can you do delay on the cases??
@caribbeanchild7 жыл бұрын
two grounds are not necessary.... ground is ground