close
KnowledgeBase

Arduino Starter’s Guide (3/7): Push Button (Switch) Exercises

videotogif_2018.05.31_21.32.27[1]

Up to this point we have focused entirely on outputs, time to get our Arduino to listen, watch and feel. We’ll start with a simple pushbutton. Wiring up the pushbutton is simple. There is one component, the pull up resistor, that might seem out of place. This is included because an Arduino doesn’t sense the same way we do (ie button pressed, button unpressed). Instead it looks at the voltage on the pin and decides whether it is HIGH or LOW. The button is set up to pull the Arduino’s pin LOW when it is pressed, however, when the button is unpressed the voltage of the pin will float (causing occasional errors). To get the Arduino to reliably read the pin as HIGH when the button is unpressed, we add the pull up resistor.

 

Hardware Used:

  • 2 – Pushbuttons
  • 1 – LED
  • 2 – 10k Resistor
  • 1 – 100 ohm Resistors
  • 1 – Arduino Uno

You can buy all this Hardware at Createlabz.

Software Used:

Arduino IDE

Set up the Hardware:

 

Code:

Code Breakdown:

The difference of int and const int is that the int will change because it is a variable while the other one is a constant. We are taught by our math teachers about constant and variables. Constant are fixed no matter what you will do and Variables has an infinite number outcomes its value will depend on the equation.

The if else allows greater control over the flow of code than the basic if statement, by allowing multiple tests to be grouped together. An else clause (if at all exists) will be executed if the condition in the if statement results in false. The else can proceed another if test, so that multiple, mutually exclusive tests can be run at the same time.

Each test will proceed to the next one until a true test is encountered. When a true test is found, its associated block of code is run, and the program then skips to the line following the entire if/else construction. If no test proves to be true, the default else block is executed, if one is present, and sets the default behavior.

Note that an else if block may be used with or without a terminating else block and vice versa. An unlimited number of such else if branches is allowed.

This were explained in the Blinking LED.

Try MORE!!!

On button off button:

The initial example may be a little underwhelming, so let’s make it a little more complicated. One button will turn the LED on and the other will the LED off. Change the code to:

And upload the program to your board, and start toggling the LED on and off.

On off Button

 

Fade up and down:

Lets use the buttons to control an analog signal. To do this you will need to change the wire connecting the LED from pin 13 to pin 9, also change this code.

Add this code above the void setup() :

Add this code inside the void set up():

Remove all codes from void loop() and replace with this:

Fade

 

Tags : arduinobuttonsLedpush buttonpushbuttonsStarter Kitswitch

Leave a Response