Arduino Tutorial 28: Using a Pushbutton as a Toggle Switch

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 มิ.ย. 2024
  • 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:
    / paulmcwhorter
    In this lesson we learn how to use a pushbutton as a toggle switch in Arduino Projects. This uses the same circuit as was used in Lesson 27, but we add the toggle feature to the software.
    You can get the kit I am using for this series at the following link:
    amzn.to/2I7N4Ek
    Also, you can get the Mastech Digital Voltmeter I am using here:
    amzn.to/2Ks9nHN
    Check out our most awesome web site here:
    toptechboy.com/arduino-tutori...
    [Disclosure of Material Connection: I am a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com. This means if you visit the link and purchase the item, I will receive an affiliate commission. Regardless, I only recommend products or services I use personally and believe will add value to my readers.]
    #Arduino
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 1K

  • @northadventure5654
    @northadventure5654 4 ปีที่แล้ว +386

    please don't stop after lesson 68! this is so awesome! You are a very good teacher, and there is no one out there like you :D

    • @tatereinke5166
      @tatereinke5166 4 ปีที่แล้ว +5

      North Adventure on video away from greatness

    • @srinivasyalala1767
      @srinivasyalala1767 4 ปีที่แล้ว +5

      Are there videos till 68? I see those videos are as Private (from 43 to 68) and cannot watch them. Please confirm.

    • @parulpari7346
      @parulpari7346 4 ปีที่แล้ว

      Yeah

    • @vitustillebeck4965
      @vitustillebeck4965 3 ปีที่แล้ว +4

      He hearted the comment. must mean he just might.

    • @jaythebay8425
      @jaythebay8425 2 ปีที่แล้ว +1

      I agree

  • @jasonengblom2993
    @jasonengblom2993 4 ปีที่แล้ว +132

    Loving the series. Drinking beer though, kids and wife are asleep and I'm geeking out in the garage.

    • @JoeMakaFloe
      @JoeMakaFloe 4 ปีที่แล้ว +19

      Sounds like a nice life

  • @johnvincentjimar8257
    @johnvincentjimar8257 2 ปีที่แล้ว +41

    You sir, are the Organic Chemistry Tutor of Arduino. I always go to your videos if I want to continue in learning arduino. Great teacher, love to see more of your videos.

  • @erwinvelasquez9018
    @erwinvelasquez9018 3 ปีที่แล้ว +11

    As a professional programmer, I thought I could do this on my 1st trial. I finally got it to work (after trying hundreds of times, with sips of coffee, and believing holding my breath will help) when I found out that delaying the loop was my only solution. I had doubts that what I did wasn't the best way of doing it. And then I saw it at 18:42 that explained what I was missing. And now I can't sleep because of the coffee :) Nevertheless, I finally felt the true satisfaction of saying "BOOOOOM!"

  • @karlgross877
    @karlgross877 4 ปีที่แล้ว +18

    I've finished 28 lessons in 4 days and have completed all the homework and lessons including this one. I retired from teaching jr high coding and robotics. We never got into programming Arduino and I always wanted to, so I bought an Ellego and Vilros kit and when they arrived, I started on your lessons and I am having a blast.

    • @aisssboudi
      @aisssboudi หลายเดือนก่อน

      Wow how 😮😮

  • @hughpatterson1480
    @hughpatterson1480 2 ปีที่แล้ว +5

    I started out wanting to get a Technicians license for Ham Radio. Flash forward two years later and I have an extra class license, I went back to school to become certified in IoT, just so I could build my own SDR radio. I now teach Arduino and and Raspberry Pi classes to teenagers. I built an electronics lab in my house as well. I decided to work through all your videos because I recommend them to anyone wishing to learn this type of material. My teaching has greatly improved due to your videos. I am so glad that you explain the math and get students to try things on their own. I came up with a similar sketch (although I prefer to call it code) when trying to do it on my own. I'm currently working on a self driving robot car based on the Arduino platform. I could do this stuff all day and night! Thanks for the great lessons!

  • @mikemanny1533
    @mikemanny1533 4 ปีที่แล้ว +92

    Hi, Paul...got to be honest with you, I couldn't work this one out by myself! The problemo was a tad confusing to solve, Old Bean, and after many cups of tea and a whole lotta head scratching and coding errors, I gave up and admitted defeat. The answer to the problem was even harder than I had imagined, so it was a good job that you went on to explain how to overcome this particular problem. Thanks again from across the pond....hope all's well over there with you and family, what with all this Coronavirus malarky. Stay in...stay safe...and stay "switched on".....pardon the pun. Regards....the Grumpy Limey.

  • @adampuckey
    @adampuckey ปีที่แล้ว +6

    I attempted this on my own, and got as far as assigning an independent tracker - in my case I used a boolean. What I did not think of was using && , so I kept falling down, cycling through random states. I also did not think of placing a small delay in the loop to make things a little cleaner; that was a very useful lesson. I then watched through and, although I wasn't able to work through it alone, the lesson afterwards made a lot more sense for having at least tried! Thanks Paul!

  • @Spiffycaius
    @Spiffycaius 4 ปีที่แล้ว +4

    I've been binge-watching these tutorials lately, ever since 2015, I've been trying to figure language out but couldn't at least until now,
    You made this very easy, and I'm finally understanding how it works, most importantly remembering it, it's hard to remember some things when you have a learning disability, so tysm for making this easy.

  • @kageshirou
    @kageshirou 3 ปีที่แล้ว +3

    I've been looking at the code over and over again and it's amazing how efficient the code is. It's quite confusing at first but I am starting to understand why it works. Many thanks for showing how to use a pushbutton as a toggle switch!

  • @nahuel3256
    @nahuel3256 3 ปีที่แล้ว +2

    I was not able to do it by myself, but I saw the light when you mentioned that we should consider the previous logic state and the actual logic state of the button!!! That was a great insight!!
    Your videos are fabulous!!!

  • @yousefessam5235
    @yousefessam5235 2 ปีที่แล้ว

    Thank you for your amazing tutorials, Paul. As for the toggle switch assignment, I managed to do it in another way before watching you. When the value read by digitalRead becomes 0, we enter a loop which keeps reading the value of digitalRead until it becomes 1 and when it becomes a 1, we toggle a boolean variable - which represents the state of the LED.

  • @travisrichardson5366
    @travisrichardson5366 4 ปีที่แล้ว +22

    Have to admit, this one stumped me. Thanks for walking us through it.

  • @whynot548
    @whynot548 ปีที่แล้ว +4

    I was able to use your tutorial to turn an ultrasonic sensor into a switch whenever it read a value under a certain number. Thank you so much for making the tutorials you do, you explain them very well and cover a lot of very useful things!

  • @patrickdoherty3371
    @patrickdoherty3371 2 ปีที่แล้ว +2

    Love how you instruct!
    Repetitive programming is the best way to learn. I've replayed and re-programmed many of your lectures! Thanks!

  • @jjedwards1
    @jjedwards1 ปีที่แล้ว

    That took me a long time to wrap my head around. I tried numerous things with counters and switch values but need to see your example and then "draw it out" several times to see why it worked. Very elegant in the end. Thanks

  • @alanoestacado
    @alanoestacado 4 ปีที่แล้ว +11

    Hey Paul, I'm really enjoying your new Arduino series. Please keep the lessons coming. I can tell you are a great teacher and wished you had been my high school science teacher many years ago. I often feel like I am back in high school science class during the sessions.
    This was the most challenging exercise to date. Took me a couple of tries to dust off enough brain cells to reach a solution. I always try to complete the lesson before watching your solution. BTW, I am using parts of your series as inspiration in teaching my six year old grandson about electronics. He loves working with the Arduino.

  • @patrickdelvisohopkins
    @patrickdelvisohopkins 3 ปีที่แล้ว +16

    Hi Paul! Your Arduino tutorials are amazing. Your teaching approach is "most excellent" and inspirational! I tried this tutorial and solved it with a while loop and two if statements. It was a great feeling to achieve this on my own. Keep up the good work, sir! Thanks again!

  • @tretty07
    @tretty07 3 ปีที่แล้ว

    I couldn't figure out the coding on my own and had to see your code, then I had to watch the video 3 times to understand when the condition, "if(buttonOld ==0 && buttonNew==1)" became true, then the light literally turned on and I was able to understand. I am learning so much from you, keep up these most excellent tutorial videos.

  • @nicholasknapik1423
    @nicholasknapik1423 ปีที่แล้ว

    Network admin and beginner tinkerer here. Found your tutorial Playlist when searching for teaching that wasn't filled with fluff. These videos are great. I could likely skip around and go ahead but I'm enjoying the fundamentals you teach on every component. Thank you. Looking forward to getting to display screens.

  • @quaternion-pi
    @quaternion-pi 4 ปีที่แล้ว +5

    As always- excellent, practical explanation - including debouncing the switch. My solution used logical not (!) on LEDState in the if statements.

  • @libbytardalo2071
    @libbytardalo2071 3 ปีที่แล้ว +6

    I was lost. You had to 'baby step' me through this one and that's okay.
    I'm here to learn. And you sir, are a teacher.

  • @stevenfinken5835
    @stevenfinken5835 2 ปีที่แล้ว +1

    I have been watching the series (old as well as the new and improved) repeatedly. Here is the thing for me. If you listen once, you heard it...if you follow along...you copied it...if you do it on your own after the previous, you remembered it...but, if you can create from what you have done previously, you have gained experience and knowledge, therefore...you have learned. I have LEARNED things by reviewing and practicing via your lessons. The things I have taken from your lessons is far more than I learned from dozens of books. For this I thank you, and ask that you continue to keep it simple and speak in "English" vs. speaking in trueNerd (bumpy text used there just for you). Keep up the good work!

  • @Sam-rz5hw
    @Sam-rz5hw 2 ปีที่แล้ว +1

    IT WORKED! THANKS A LOT SIR!
    // PULL DOWN RESISTOR
    int readPin = 2;
    int outPin = 3;
    int delayTime = 500;
    int signal = 0;
    void setup()
    {
    pinMode(readPin, INPUT);
    pinMode(outPin, OUTPUT);
    Serial.begin(9600);
    }
    void loop()
    {
    while (digitalRead(readPin) == 0)
    {
    digitalWrite(outPin, signal);
    }
    while (digitalRead(readPin) == 1)
    {
    digitalWrite(outPin, signal);
    }
    signal = !signal;
    digitalWrite(outPin, signal);
    delay(delayTime);
    }

  • @yp4577
    @yp4577 4 ปีที่แล้ว +3

    These lessons are great! You are such an amazing teacher. Having said that, I will have to reread some of the code written to follow along exactly how the commands make up a toggle switch. As you can probably deduce from that, I was NOT able to come up with the solution all by myself.

  • @thefierceninja2557
    @thefierceninja2557 3 ปีที่แล้ว +4

    Honestly, toggle switches are the bane of my existence as I have always had issues with making them in both programs and videogames, and it's really annoying too because I have made very complex logic systems and made flight control chips using logic gates in games like Scrap Mechanic but I couldn't make a toggle switch, but now I finally found a way that works for all scenarios thanks to this video. Amazing teaching Paul!

  • @markelven1552
    @markelven1552 4 ปีที่แล้ว +1

    Cannot believe it has taken me so long to start watching this series! Excellent tutorials, keep up the great work it is much appreciated.

  • @sourishthakral
    @sourishthakral ปีที่แล้ว +2

    Hello Mr. Paul, I am a student in middle school but I have to really admit ur lessons are superb & the best thing I have seen that you are also teaching the lessons of raspberry pi & raspberry pi pico. but the main motive of commenting you now is that I've already figured out how to make the pushbutton a toggle one, you just have to write this code :-
    if (digitalRead(buttonPin)==0){
    while (digitalRead(buttonPin)==0){}
    digitalWrite(LEDPin, HIGH);
    }
    else if (digitalRead(buttonPin)==1){
    while (digitalRead(buttonPin)==1){}
    digitalWrite(LEDPin, LOW);
    }
    This code will definately work.

    • @samueltheprogrammer6145
      @samueltheprogrammer6145 ปีที่แล้ว

      I don't think this code will work for a Toggle Switch, let me explain why. When you press the button the Arduino terminator will go into the if statement "if (digitalRead(buttonPin)==0)" and then the Arduino terminator goes into the while loop "while (digitalRead(buttonPin)==0)" and stays in there until we stop pressing the button, after we are done with pressing the button the Arduino terminator goes to the following line of code which is turning on the LED and then the Arduino terminator gets out of the if statement and then it goes into the else if statement because we are not pressing the button, and then the Arduino terminator goes into the while loop and stays in there until we press the button and then it gets out of the while loop, and the Arduino terminator turns off the LED and then it gets out of the if else statement, and this is where the problem is, sins we are pressing the button now the Arduino terminator will go into if (digitalRead(buttonPin)==0) again and goes into the while loop and after we stop pressing the button we will turn on the LED again. I hope now you can see what the problem is. The problem is that the LED will always be on when you are not pressing the button, which to me is not a Toggle Switch. I hope that makes sense to you.
      Samuel

  • @nortetrading3136
    @nortetrading3136 4 ปีที่แล้ว +10

    18:04 is the funniest part on this series yet! Thanks Paul!

  • @rebeccahare897
    @rebeccahare897 4 ปีที่แล้ว +5

    Great video series, thanks very much Paul.
    I came at this problem in a slightly different way: since the LED modes are binary (at least in this scenario), if you do a button counter (i++ [or j++]), you can then write an "if" statement to check if it is an even number - LOW if even, HIGH if odd. This also gives an advantage of being able to count the number of times the LED has been on & times button has been pressed (if you add a Serial.print!).

    • @426F6F
      @426F6F ปีที่แล้ว

      That's pretty much how mine worked too, but instead, I made 'x += 1;'. When x == 1, the light turns on, then after the next iteration when 'x==2', the light turns off and in the same block I put 'x = 0;' to reset it. It was a pretty simple way to code it.

  • @TheTradesman1111
    @TheTradesman1111 3 ปีที่แล้ว +1

    Hi Paul - Gratitude to you for providing super quality content. I am a big fan of you and your teaching style and learning a lot everyday.
    I feel delighted as I got through the toggle switch code after few tries without going into the solution that follows. I captured lag value of the push button and passed it inside the while loop where i reread current value of push button and compared for combination (0,1) and if true then used MOD operator on the counter variable to toggle LED :)

  • @danbishop4035
    @danbishop4035 4 ปีที่แล้ว +1

    Another great video...my mother passed away a couple of years ago and she had this old flashlight that she always used. Her old flashlight will soon get an upgrade to an Arduino nano, as many LEDs as I can fit into the lens area and a variant of this toggle code. Also thanks to your Fusion 360 tutorials I'll be 3D printing and insert to fill the void of the battery compartment which will hold the nano and the batteries. Thanks for the inspiration!

  • @prankshot7971
    @prankshot7971 4 ปีที่แล้ว +6

    I tried many ways but never succeeded ..after watching this video i am feeling confident.. thanks so much

  • @vladstrulev
    @vladstrulev 4 ปีที่แล้ว +6

    Thank you very much. Great. I've learned to do it with millis(), not delay()

  • @zachhamersley967
    @zachhamersley967 3 ปีที่แล้ว

    This was a tough one, but had some fun! I had to watch the video to work this one out. Then I had major issues with the brackets in the code...Figured it out eventually, learned a lot about bracket placement. I used a lot of the troubleshooting skills that I learned in the earlier lessons, "comment out" is a great tool. Used the code from Lesson 27 to troubleshoot the circuit hookup. It reminded me of something you said in an earlier lesson," the code is doing exactly what you tell it to do." Very important lesson here, Thanks Paul!!!

  • @user-zu1ix3yq2w
    @user-zu1ix3yq2w 3 ปีที่แล้ว +1

    I did this on my own, but the explanation on graph paper was incredibly insightful.

  • @Ikke-xd4nl
    @Ikke-xd4nl 3 ปีที่แล้ว +5

    Got this exercise suprisingly quick tbh but did it in a different way, I read the value of the buttonPin and when it's pressed I use a while loop to wait until the button is released again to then switch the state of the LED. This uses less variables (no old/new) and makes it a bit easier to understand in my opinion. Still very interesting to see how you did it! Discovering multiple ways to solve a problem is the best way to learn and really get a solid understanding of solving problems on your own.
    I only recently got a kit and even though I have some programming experience in Python I'm quite new to electronics and racing through the vids right now.

    • @msmnutech2814
      @msmnutech2814 3 ปีที่แล้ว +1

      I'm similar to you. Programming experience with little to no electronics experience.
      I also solved it the same way as you did.
      Check it button pushed, if it is, set the LED on, then wait for it to be released.
      When released, wait for it to be pushed again, when its pushed a second time, set the LED off and wait for it to be released again.
      when its released, let the program end so you start at the top again.
      I'm enjoying the combination of electronics and programming.

    • @lvessen
      @lvessen ปีที่แล้ว +1

      Thanks, this worked for me too.

    • @manuellomote1425
      @manuellomote1425 ปีที่แล้ว

      Your comment has everything I was going to say 😊

  • @kenmeyer100
    @kenmeyer100 3 ปีที่แล้ว +4

    After banging my head against a wall for a microsecond, I got my version running before watching the video. (my secret: I did hold my breath)

  • @nicholasmakas7356
    @nicholasmakas7356 3 ปีที่แล้ว

    I am loving this series, the utility of the internet is in action. I tried to program the toggle switch but struggled with exiting loops, I am use to programming in Python, so it was frustrating with the new 'rules' but I ended up using a return statement in my if statements and I was able to program the toggle switch successfully. Thankyou for the content Paul

  • @wayneswan3092
    @wayneswan3092 3 ปีที่แล้ว

    There were multiple other sources I was able to look over to set up toggle button. However, as always, yours has proven to be most educational! After going through it with you on this video, however, I have since been able to set up multiple void loops to manipulate 4 RGB-LEDs in various ways, that I was then able to toggle through at the touch of a button. This has been my primary goal since I started playing with Arduino using LEDs. Next I'll be investing in other RGB LED cables and other fun light toys to program to dance to my own liking. Thank you Paul!

  • @jamess009
    @jamess009 4 ปีที่แล้ว +18

    After hours of banging my head against a wall I finally managed to get it working, with a bit of help from google i admit. I did it a bit differently, I made a counter and each time you press the button it adds 1. Then if the count is at an odd number the led is off, if its an even number it turns on

    • @GLH8
      @GLH8 3 ปีที่แล้ว

      I did this, but it made it flash whenever you held the button down.

  • @allensun
    @allensun 3 ปีที่แล้ว +21

    Thanks for the tutorial!
    I was able to figure it out beforehand, however I used a slightly different approach. In my code, when digitalRead() detects a "0", I enter a while loop which continually performs more digitalReads() until it detects a "1". This indicates the button was released, so I proceed to do an LED state change which was quite similar to your implementation (though I used a boolean variable for LEDState rather than an integer).

    • @karanjaspreetsingh3807
      @karanjaspreetsingh3807 3 ปีที่แล้ว

      I too did the same way

    • @tacowithgiantbeansallover
      @tacowithgiantbeansallover 2 ปีที่แล้ว

      SAME

    • @echanical2156
      @echanical2156 2 ปีที่แล้ว +3

      If you use a while loop, your program will stuck in there until you release the button. It can't be used in programs with multiple things running simultaneously.

    • @FarmBoyTech
      @FarmBoyTech ปีที่แล้ว +1

      Did u do this without delay ?

  • @coconutyesse
    @coconutyesse ปีที่แล้ว

    Yes. I did it after I checked the strategy drawing.
    It is the most important part before the coding.
    Thanks for this great lesson!

  • @erygion
    @erygion 4 ปีที่แล้ว +1

    I could not do this on my own Mr. Mcwhorter. This was a great lesson and I fully understand what's going on. What a handy function. Very clever. Thank you sir.

  • @oleksandr_honcharov
    @oleksandr_honcharov 4 ปีที่แล้ว +5

    Hi, Paul🙂
    Interesting tutorial, thank you very much as always)
    I did it like that before watching how you did it.
    const int redPin = 12;
    const int buttonPin = 10;
    int buttonRead;
    int redLEDState = LOW;
    void setup() {
    Serial.begin(9600);
    pinMode(redPin, OUTPUT);
    pinMode(buttonPin, INPUT);
    buttonRead = digitalRead(buttonPin);
    }
    void loop() {
    digitalWrite(redPin, redLEDState);
    int newRead = digitalRead(buttonPin);
    if (buttonRead != newRead && newRead == 1) {
    redLEDState = !redLEDState;
    }
    buttonRead = newRead;
    Serial.println(buttonRead);
    }

    • @nahuel3256
      @nahuel3256 3 ปีที่แล้ว

      this is very elegant

    • @michaeltanner4404
      @michaeltanner4404 2 ปีที่แล้ว

      @@nahuel3256 It is, with one exception. He's declaring a new newRead variable with every loop. I don't think the Arduino does garbage collection, so he has a memory leak. Better to declare newRead as a global and just assign it a new value every pass through the loop. Other than that, it's definitely less code than my attempt :)

  • @jitunehete
    @jitunehete 4 ปีที่แล้ว +8

    Sir please make lesson on SPI interface. .
    Thanks in advance..

  • @edwardowen8668
    @edwardowen8668 11 หลายเดือนก่อน

    Just wanted to share how I made a toggle switch, minimal code:
    int swPin=8;
    int LEDPin=10;
    int LEDstate=0;
    int j;
    void setup() {
    // put your setup code here, to run once:
    pinMode(8,INPUT);
    pinMode(10, OUTPUT);
    Serial.begin(9600);
    }
    void loop() {
    // put your main code here, to run repeatedly:
    int swVal;
    swVal=digitalRead(8);
    if (swVal==HIGH){
    LEDstate=abs(LEDstate-1);
    digitalWrite(10,LEDstate);
    Serial.println(LEDstate);
    delay(500);
    }
    }
    Using the ABS function returns either 1 or 0, opposite of the previous state. Thanks for all the great lessons.

  • @yusielrodriguez4153
    @yusielrodriguez4153 2 หลายเดือนก่อน

    This one, took me more time than I am used to solving the assignments. But finally, with logic, I could achieve it. I AM A LEGEND AGAIN. Thank you, teacher, for bringing this type of enriching content

  • @daveflatters4981
    @daveflatters4981 3 ปีที่แล้ว +3

    I got it myself by using 3 if statements and an On variable with 3 states 0, 1, & 2.
    If button = 0 & On = 0 turn led HIGH & On = 1
    If button = 1 & On = 1 turn led HIGH & On = 2
    If button = 0 & On = 2 turn led LOW & On = 0

  • @Ben-fy3dl
    @Ben-fy3dl 3 ปีที่แล้ว +3

    Damn this pushbutton toggle is logic intensive. I need more brainpower lol

  • @ovi_4
    @ovi_4 4 ปีที่แล้ว

    Ahhhh, I was thinking of the need to use new and old state variables and I have used them but did not realize that you have to actually ,,manually,, change the LEDstate after the IF statement and then manually change it again after the else statement. So my LED switched on at the first push but that was it. I could no longer switch it OFF after that. I must have tried about a dozen different combinations and lots of ,, head scratches,,. Oh...what a bummer...:))) But you know what I'm absolutely hucked on your teaching style. You are a true native teacher. Thank you very much and please, please do not stop the series. It is....well... it goes without saying one of the best teachings I've ever encountered here on youtube. Once again, Thank You, Sir.

  • @TMRu22311
    @TMRu22311 2 ปีที่แล้ว

    I didn't even have a clue how to go about this one. I really appreciate the time you have taken to help us all better understand this stuff. Has anyone else wondered how any of these videos could get a "Dislike?" Personally, I am very grateful for these videos and always looking forward to the next lesson. I can't wait until the day when I can start creating my own circuits and projects. Thank you Paul (10^100) times!!!!

  • @evreid6800
    @evreid6800 4 ปีที่แล้ว +3

    This one stumped me. I tried to get the program to look at LEDPin and ButtonPin. I got it to come in and stay on but not turn off

    • @shatann
      @shatann 4 ปีที่แล้ว

      I had the same problem. Turned out to be the wiring. I only had pin12 going to the switch, but you also need the 5V.

    • @wonderwang1585
      @wonderwang1585 4 ปีที่แล้ว

      I have the same problem with yours. Push buttom, it is on; push button again, it still on.

  • @AnshishRay
    @AnshishRay 3 ปีที่แล้ว +3

    cant do it by myself! :(

  • @jonnycorleone13
    @jonnycorleone13 4 หลายเดือนก่อน

    I did it! Used a while loop white the button is pressed, and changed a boolean inside the while loop with an if, else if statement!!
    Thank you so much for your lessons Sir!

  • @srikannandairy
    @srikannandairy 4 ปีที่แล้ว +1

    I tried myself, it is working. It's great on your part to insist on writing the code by myself.

  • @TheGauld89
    @TheGauld89 4 ปีที่แล้ว +1

    Another great lesson. I have only very recently started trying to learn how to code with Arduino. I have been following your lessons and they've been a massive help. Keep it up, this is the very best of Arduino coding video tutorials out there!!

  • @jakerice1593
    @jakerice1593 ปีที่แล้ว +1

    I'm loving the videos so far! for anyone watching this in the future, there are actually a few ways to code this. The most efficient I've found, was to just use while-loop-waits, much like waiting for user input.

  • @jordynvanevenhoven6305
    @jordynvanevenhoven6305 2 ปีที่แล้ว +1

    I never thought that I'd learn so much from such a simple task! I figured it out my own way after a bit of head scratching but still thoroughly enjoyed how you broke it down and explained it.
    Love your videos and plan on using what I learn in my club, my future classes, and hopefully even one day on the job.

  • @enriqueeenriquez2180
    @enriqueeenriquez2180 2 ปีที่แล้ว +1

    i forgot to mention in my previous comment, that your code is simple, elegant and clever and very important, validated. You kept the button pressed on to ensure it will not change, simple but effective validation.. WELL DONE!!!!

  • @user-su5sq5ib3i
    @user-su5sq5ib3i ปีที่แล้ว

    Had to go back to this just to marvel at how powerful yet simple, brilliant!

  • @anandg825
    @anandg825 10 หลายเดือนก่อน

    I don't think any other code has such ingenuity with fewer
    statements but works without any glitches and oscillation, especially
    at the release of the switch. Moreover, the way you explained with the
    dots on graph is amazing. No one can match you. Thanks Mr.Paul.

  • @minhtam5051
    @minhtam5051 3 ปีที่แล้ว

    the concept you introduce in this video is sophisticated!

  • @souljr.
    @souljr. 3 หลายเดือนก่อน

    I was definitely mistaken by the appeared simplicity of the homework. I then fell into my bad habit trap of overthinking the absolute hell out the problem. Gave up and felt quite silly when I came and saw the answer. I'm happy you gave us this assignment, it seems to have gotten most people thinking quite critically! You are truly one of the most amazing teachers I've ever had the pleasure of studying under (even though not in a typical sense).

  • @evanbudd649
    @evanbudd649 2 ปีที่แล้ว

    It took me longer than I thought it would. I had to leave it alone for a while and come back to it. I remembered dealing with "lead edge" and "lag edge" signals when troubleshooting a different problem which led me to a similar solution, but as others have said, yours was more efficient. Awesome challenge! I was tempted to give up because it seemed like the solution should be so obvious. Glad I worked it out myself. Thanks for the great videos.

    • @paulmcwhorter
      @paulmcwhorter  2 ปีที่แล้ว

      This is a classic programming problem, and one much harder than you would expect. Glad you understand it now

  • @Dragonsecho3
    @Dragonsecho3 3 ปีที่แล้ว +1

    I've looked up other arduino help before, and it's patchy at best. You lay it out and actually explain the content. You've answered questions I've long had that no one else has come close to answering. Thank you

  • @rg_1971
    @rg_1971 4 ปีที่แล้ว

    Thank you for this video. It was great to see this done live versus in a wall of text. Keep up the great videos, I am working through the backlog.

  • @dcplett1
    @dcplett1 2 หลายเดือนก่อน +1

    This is my first comment, firstly have never enjoyed such Epic content like what you are creating here. I have never gotten so excited to learn before! I was up till 1am trying to figure this one out. Ended up going to bed thinking about this toggle switch. When I woke up this morning I just lay in bed thinking of what it could be. I grabbed a pen and paper and started writing out the code, I had in my head. I managed to get it to work perfectly using while and if statements. Thank you for these videos!

    • @paulmcwhorter
      @paulmcwhorter  2 หลายเดือนก่อน +1

      Glad you enjoy it!

  • @jimfisher5099
    @jimfisher5099 3 ปีที่แล้ว +1

    I will admit, it took all day tinkering with it, but I got it to work this evening!! Now, my reward it watching this video before I go to bed...

  • @rexaustin2885
    @rexaustin2885 4 ปีที่แล้ว +1

    Hey Paul, thanks so much for your series. your new patron here. Not even exams have kept me awake like your lessons or as excited for the next. i was able to do it using three 'while' loops within one 'if' loop. also, i am not sure if i missed it before but that 'else' command came out of the blue. good thing i went through the whole tutorial. that was a much more efficient way to do it, perhaps

  • @Traxonen
    @Traxonen 2 ปีที่แล้ว +1

    I missed the initial test but your explanations were so clear that it seemed almost easy... Wonderful!

  • @danbishop4035
    @danbishop4035 4 ปีที่แล้ว

    Although I didn't complete the project on my own I did recognize that we needed several variables to keep track of the button state. Maybe a little laziness on my part because had a diagrammed it out I think I'd have come up with the solution. I will be using this to transform an old school flashlight that my mother used when she drove trucks into an LED flashlight with a toggle switch. A lot of good information in this lesson...Thanks!!!

  • @Ronavargas
    @Ronavargas 4 ปีที่แล้ว

    This project was very difficult. Completed along with your explanation regarding the 2 variables and conditions associate with pushbutton and LED respectively. Thanks for your support.

  • @rogthedodge99
    @rogthedodge99 3 ปีที่แล้ว

    Only did it with help from the internet! I managed to fathom out a toggle variable, button didn't track the button state. Not many of us would have realised that an 'Else' was required, as youv'e not shown this before.
    Made me think though. Keep up the good work.

  • @kojitw
    @kojitw 3 ปีที่แล้ว

    finally got it to working after several hours. forgot the "==" sign, didn't connect the pin correctly, there are many things that can go wrong, but I am glad to have made it. thanks, Paul!

  • @adamcurtice3584
    @adamcurtice3584 2 ปีที่แล้ว

    I got my solution for this on my first try. I added an additional digital input to monitor the state of the LED. I then used 2 If statements to compare LED states when the switch was pressed. Whatever state the LED was in when the switch was pressed was toggled to the next. Other than some debounce issues with the switch it all worked great. Thanks for the challenge.

  • @anthonyj.dipasqua4676
    @anthonyj.dipasqua4676 3 ปีที่แล้ว

    It is fun to think of what I could do with this new power. I did not figure it out on my own and had to follow your demo. Thanks again Mr McWhorter! I hope you had and will continue to have a happy holiday!

  • @warrenscorner
    @warrenscorner 4 ปีที่แล้ว

    I had no clue where to start! This is a fairly easy accomplishment in PLC ladder logic programming but this one stumped me.

  • @IvanLopez-kw2hv
    @IvanLopez-kw2hv 2 ปีที่แล้ว +1

    Hi Paul, this is a great tutorial. Thanks very much. About this problem, I have to admit that it was the first really challenging one for me. It took me one full day after many attempts trying to find out the logic behind and drawing several flow charts unsuccessfully . Finally came up with a solution directly in the programming window, kind of similar to yours. I introduced a variable that changes to 0 or to 1 in case the actual reading value is 0 or 1, by comparison to that variable (I guess like the previous state). So here is my coding (starting with A=1): buttonRead=digitalRead(buttonPin)
    if (buttonRead==A) {
    A=1;} else {
    A=0;}
    if (A==0){
    digitalWrite (LEDPin,HIGH); }
    if (A==1){
    digitalWrite (LEDPin,LOW); }
    delay(d);

  • @robertcurpan9047
    @robertcurpan9047 3 ปีที่แล้ว +1

    Hi Paul! I managed to do the task on my own and in another way. What I did different is that I did 2 digitalReads with a little delay in between ( buttonVal1=digitalRead(buttonPin) ; delay(20) ; buttonVal2=digitalRead(buttonPin) ). Then I pretty much did as you did, only that I used a digitalRead for the LEDstate as well (yellowVal in my code). Thanks for the lesson!

  • @wesleypingleton9519
    @wesleypingleton9519 3 ปีที่แล้ว

    Paul, after some trial and error , I was able to perform this before watching your video . However I did this by putting a digital read onto the LED as well . So now I could keep track where the button was and what state the Led was constantly . I then set a couple IF statements and Boom!!

  • @RobsFatboy
    @RobsFatboy 4 ปีที่แล้ว

    Completed the task. Prior programming novice level helps. Drank lots of HOT coffee with creamer and sweetener. Not to mention 23 years in the service as an electronic technician. I definitely need the refresher. Great job keep the tutorials coming.

  • @johndavid360
    @johndavid360 3 ปีที่แล้ว +1

    I’m so amazed about all of these new programs!

  • @stefanfarier7384
    @stefanfarier7384 4 ปีที่แล้ว +1

    You reminded me to be aware of the power of boolean algebra and reassignment of variables. Thanks much! Love from Guyana

  • @lorisrobots
    @lorisrobots 2 ปีที่แล้ว +1

    I was not successful on my own. I had the basic logic but the "bounce" in the circuit kept making me think I was totally wrong. Adding in a delay was the key. Thanks for the great lesson. Very practical.

  • @dannycollins5698
    @dannycollins5698 ปีที่แล้ว

    I didn't get it without you. The answer was harder than I thought! I did give it multiple tries before watching you. I got close using a while loop.

  • @rontanedo
    @rontanedo 2 ปีที่แล้ว +1

    Thanks for making it easier to comprehend! :D How this code works is really blowing my mind off. Looking forward to more lessons you teach!

  • @pasteltiara
    @pasteltiara 4 ปีที่แล้ว

    WOuld not have solved this problem. Did not think about the two-state concept.Had to follow on this lesson. And that made it much more clearer. A good one that made me think deeper.

  • @EthanElkorr
    @EthanElkorr ปีที่แล้ว

    was not able to figure it out my self but followed along and now I understand how to. Love your Tutorials!!!

  • @DGworksIvan
    @DGworksIvan 2 ปีที่แล้ว

    the line if(buttonOld==0 && buttonNew==1) made me so confused, I rewatch the video 3 times and started analyze the logic of the code,
    since if(buttonOld==1 && buttonNew==0) made way more sense to me! What made it more confusing is the program works both ways.
    Then i studied with 100% concentration, serial printed all values to analyze if i missed something....BOOM!
    the code works both ways, however the trigger time is different!
    It has been so long I lost the joy of studying and figuring stuff by myself.
    Thanks Mr. McWhorter, your lesson is way too good to be free on internet.

  • @kevinroden5503
    @kevinroden5503 2 หลายเดือนก่อน

    My new friend Gemini and I made the toggle switch slightly differently. Your excellent tutorials make it a lot easier to understand, and correct, the AI suggestions.

  • @russellford366
    @russellford366 ปีที่แล้ว

    Anyone else get preoccupied with code that they have written and never think of all the new things you're going to need for the new one? I'm going to start erasing all of what i've written before starting one of these "projects". Awesome job Paul!!! I'm trying to change my career path and these videos as well as this arduino are making learning to code SO much easier!!!!!

  • @danielbrown9813
    @danielbrown9813 ปีที่แล้ว

    man, all i had was "okay, when it goes from 0 to 1 it's gotta do somethin" but had no real idea how to make that 'somethin' happen til seeing it and now it's so obvious. Thanks for the great content!

  •  ปีที่แล้ว

    Fantastic episode, thank you. We had to watch because we weren't able to figure out the delay due to the Button bouncing by ourselves. Thanks one more time.

  • @laxmanrao4554
    @laxmanrao4554 3 ปีที่แล้ว +2

    Many thanks again Paul. I am just loving these awesome lessons! ❤️
    I made an addition to the task for myself. Led will be on on every 3rd release of push button. Codes below:
    Variables
    int ledPin=2;
    int buttonPin=4;
    int buttonOld=1;
    int buttonNew;
    int ledState=0;
    int dt=500;
    Void setup(){
    Serial.begin(9600);
    pinMode(ledPin,OUTPUT);
    pinMode(buttonPin, INPUT);
    // I AM POWERING THE BUTTONPIN USING A 5V VOLTAGE OUTPUT SLOT IN ARDUINO.
    void loop(){
    buttonNew=digitalRead(buttonPin);
    Serial.println(buttonNew);
    if(buttonOld==1 && buttonNew==0){
    if(ledState==0){
    digitalWrite(ledPin,LOW);
    ledState=1;
    buttonOld=buttonNew;
    }
    }
    if(buttonOld==0 && buttonNew==1){
    if(ledState==1){
    digitalWrite(ledPin,LOW);
    ledState=2;
    buttonOld=buttonNew;
    }
    }
    if(buttonOld==1 && buttonNew==0){
    if(ledState==2){
    digitalWrite(ledPin,LOW);
    ledState=3;
    buttonOld=buttonNew;
    }
    }
    if(buttonOld==0 && buttonNew==1){
    if(ledState==3){
    digitalWrite(ledPin,LOW);
    ledState=4;
    buttonOld=buttonNew;
    }
    }
    if(buttonOld==1 && buttonNew==0){
    if(ledState==4){
    digitalWrite(ledPin,LOW);
    ledState=5;
    buttonOld=buttonNew;
    }
    }
    if(buttonOld==0 && buttonNew==1){
    if(ledState==5){
    digitalWrite(ledPin, HIGH);
    delay(dt);
    ledState=0;
    }
    buttonOld=buttonNew;
    }

  • @paulrockwell9914
    @paulrockwell9914 3 ปีที่แล้ว

    Echoing the "great job" on this lesson. I had solved this on my own, but my solution was bit more complex. I detected the button press, then waited for the button release before toggling the LED. Your analysis and solution made me realize all I had to focus on is detecting when the button was released if it was already pressed.

  • @davebroster5560
    @davebroster5560 2 ปีที่แล้ว

    Nearly got it first time. I was trying to make it as concise as possible so was using a while loop to do nothing while the button was up and only act when it was down. It mostly worked, but was a bit clunky and relied on the timer to make it work. Great tutorial.

  • @dangarbutt6717
    @dangarbutt6717 3 ปีที่แล้ว

    Hi Paul,
    I included the line Serial.println(LEDState) ; in order to view the state on the Serial Monitor, works great.
    Lesson 27 cleared up my misunderstanding of Pull-up and Pull-Down resistors.
    Thanks again.

  • @mrktm65sx
    @mrktm65sx 5 หลายเดือนก่อน

    Didn't get this one going by myself. Knew I needed state variables but couldn't finish it. Thanks Paul for the clear instructions!

  • @adityashauryavardhansingh1491
    @adityashauryavardhansingh1491 2 ปีที่แล้ว +1

    int readpin = 4;
    int ledpin = 8;
    int v;
    int i = 1;
    void setup() {
    // put your setup code here, to run once:
    pinMode(readpin, INPUT);
    pinMode(ledpin, OUTPUT);
    Serial.begin(9600);
    }
    void loop() {
    // put your main code here, to run repeatedly:
    v = digitalRead(readpin);
    while (true)
    {
    if (v==0)
    {
    if (i % 2 != 0)
    {
    digitalWrite(ledpin, HIGH);
    }
    else
    {
    digitalWrite(ledpin, LOW);
    }
    i++;
    }
    v = digitalRead(readpin);
    Serial.println(v);
    delay(140);
    }
    }
    It was working but maybe it was not that efficient method.
    Thank you, Paul sir for this great lecture and I really appreciated it.