PUSH BUTTON

PUSH BUTTON-Uncategorized

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:

int tipkalo = 6;
int led = 13; // using LED on the pin 13
              // which we will turn on or off using the push button
void setup()
{
  pinMode(led, OUTPUT);
  pinMode(tipkalo, INPUT);
}
void loop()
{
  digitalWrite(led, digitalRead(tipkalo) ); 
  
  // this is the same as:
  /*
  int stanjeTipke = digitalRead(tipkalo); // saves value 0 if the button is not pushed
                                          // or value 1 when it is
  digitalWrite(led, stanjeTipke);  // button 1(HIGH) state - LED is on
                                   // button 0(LOW) state - LED is off
 */
}

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:

int tipkalo = 6;
int led = 13; // using the LED on the pin 13
              // which we will turn on or off by pushing the button
int stanjeTipke; // the variable in which we save the last state of the push button
bool boolLed = true;
void setup()
{
  pinMode(led, OUTPUT);
  pinMode(tipkalo, INPUT);
}
void loop()
{
  stanjeTipke = digitalRead(tipkalo);
  // if the state changes pin 6
  if ( digitalRead(tipkalo) != stanjeTipke )
  {
    boolLed = !boolLed;         // change the LED state too
    digitalWrite(led, boolLed);  
  }
}

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:

int tipkalo = 6; //push button on the pin6
int brojac = 0; //counter is at 0 in the beginning
bool boolBrojac = false; // bool which we use to control entering a part of the code
                         // which increases the "counter's" value
void setup()
{
  pinMode(tipkalo, INPUT); //defining push button as an input pin
  Serial.begin(9600);  // state of the variable will be shown in the Serial Monitor
  Serial.println("Pocetno stanje brojaca: " + String(brojac)); // "greeting" message
}
void loop()
{
  if ( digitalRead(tipkalo) == HIGH && boolBrojac == false ) // if the button is pushed and we have not entered the if
  {
    boolBrojac = true; // we have entered the counter
    brojac++;  // the same as brojac = brojac + 1;
    Serial.println("Stanje brojaca: " + String(brojac)); // counter status printing
  // this if will only be done once
     // although the button's digital reading will be HIGH(1) many times
     // immediately upon entering, we change the "boolBrojac" which is a condition for entering the if
  // only when the button is deactivated, we want to change
  // "boolBrojac" in order for it to enter the first if again
  // and by pushing the button again, be able to increase the variable "brojac"
  if( digitalRead(tipkalo) == LOW && boolBrojac == true ) boolBrojac = false;