Tuesday, July 21, 2020

Arduino Tutorial - Two Buttons

Arduino Tutorial - On/OFF buttons

Two Buttons


Introduction

  • this is the third of my arduino tutorial. we will be expanding what we learned in part two to include a second push button. and a bit more control logic in the code
  • This project will involve having an LED turn on and stay on when button 1 is pressed and then turn off only when button 2 is pressed.
  • What you will need:
    • arduino
    • breadboard
    • wires
    • push button
    • (2) LED
    • (1) 220 ohm resistor
    • (2) 2K ohm or higher resistor

circuit

here is the circuit diagram for this project

  • it's identical to the circuit in project two except for the addition of a second push button
    • the second button is connected to pin 12 on the arduino.
    • it also includes 2k pull-down resistor.
  • note that the blue and green wires are not connected, they simply cross over each other.

picture of the circuit i built for this project.


code

screenshot of the code for this project. the full code can be found here

  • you should recognize almost all of this from the previous two posts.
  • the new items are:
    • bool reset = false
      • this is boolean data type
        • bools can hold either true or false.
    • &&
      • this is a logical AND operator.
        • it checks whether the statements on the left hand side and right hand side are true.
          • if they are both true, then the condition in parenthesis is considered false.
          • if one or both are false then the condition in parenthesis is considered false.
    • else if
      • this is another if statement.
        • it only evaluates the condition in parenthesis if the if statment above it is false, otherwise it and the code in its brackets is ignored

congragulations,if everything is working properly you should now have a LED which lights up when button 1 is pressed and turns off when button 2 is pressed.


review

  • learned about logical AND operator
  • learned about boolean values
  • learned how else if statement works
  • got a first taste at digital logic

going further

what can you do to keep going with this leasson

  • make the LED light up/turn off only if both buttons are pressed at the same time.
  • make the LED turn on/off only if both buttons were pressed, but not simultanously.(press button 1 then press button 2)
  • using a delay can you make it so that the LED flashes on then off if button 2 is pressed?

thank you for checking out my tutorial. check back soon for the next part of this tutorial.

No comments:

Post a Comment

Arduinot Tutorial - Serial Monitor

Arduino Tutorial - Serial Monitor Introduction ...