PUSH BUTTON

WHAT ARE THOSE?
Push button is the most commonly used switch in electronics an there is no need for further explanation. Very briefly, using one image, we will show what and how it works, and then we will immediately connect it to Dasduino.
PULL-UP RESISTOR
Each push button must be connected to Dasduino through either pull-up or pull-down resistor. We have already talked about pull-up resistors and Dasduino’s pins floating. Let’s see what happens when the push button has, or does not have its own resistor.
We connect the push button on the left to +5V which will, when under pressure, be switched to digital pin 7. The push button on the right is connected to pin 6, but we add a 10k resistor as shown in the picture below.
What we expect is that Dasduino’s pins 7 and 6 will have LOW(0) readings when not activated, or HIGH(1) when buttons are pushed. Let’s check what is actually happening:
We will write down a small sketch that will read the digital pins 7 and 6 states:
int
lijevoT = 7;
// push button on the left, without pull-up resistor, is on the pin 7
int
desnoT = 6;
// push button on the right, with pull-up resistor, is on the pin 6
void
setup
()
{
pinMode
(lijevoT,
INPUT
);
pinMode
(desnoT,
INPUT
);
// we define them as input pins
Serial.begin(9600);
// starting serial communication, through which we will read the pins' states
}
void
loop
()
{
// writing down states of pins 7 and 6
Serial.write(
"Tipkalo lijevo: "
);
Serial.write(
digitalRead
(lijevoT) );
Serial.write(
"\t"
);
Serial.write(
"Tipkalo desno: "
);
Serial.writeln(
digitalRead
(desnoT) );
}
Our readings look approximately like this:
The pin, where the push button without a pull-up resistor is located, floats, therefore it is useless as it is. So, pull-up/pull-down resistor is something a push button can not work without.
PUSH BUTTON AS AN ACTIVATOR
We want to use the push button in a way that when it is pushed/activated it turns the LED diode on the 13th pin on. No problem:
PUSH BUTTON AS A SWITCH
We can also use push button as a switch. We will use the boolean function and by pushing the button on the pin 6 switch the condition from LOW(0) to HIGH(1) and vice versa. We do it as follows:
PUSH BUTTON AS A COUNTER
We will use the variable counter which we will increase by 1 each time we push the button. Here we must look out for debouncing. It is about how, no matter how fast we release the button, the code will read it as active several times and unnecessarily increase the counter. This can be solved by using the function delay() which will stop the code until we have enough time to release the button, or a bit more sofisticated as in the sketch below: