Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

tyzjames/Button

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
4 Commits
 
 
 
 
 
 

Repository files navigation

Button

Read an input switch on an Arduino pin and determine the press status

Steps

- Initialize the switch ```c++ Button pb1(pin, button_state, read_type) ``` Type|Parameter|Description ---|---|--- int|pin| Pin number the switch is connected to. int|button_state| Set the initial state of the pin (HIGH or LOW). Default HIGH. boolean|read_type | If true, switch will read long presses.
  • Read switch status
#include <Button.h>
#define SW1 5

Button pb1 (SW1, HIGH, false);

setup(){

}

loop() {

  //Read switch here. Create function to read multiple switches if necessary
  pb1.read_switch();

  if (pb1.get_long()) {
    Serial.println("long press");
  } else if (pb1.get_short()) {
    Serial.println("short press");
  }

  //Reset switch here to clear values
  pb1.reset();

}

This reads any switch state changes. Call get_long() to get the boolean value if the switch is held down (500ms default) and get_short() to get the boolean value if the switch has been pressed down.

If read_type == true, short presses will only be read on-release instead of on-press.

  • Read multiple switch press

To read multiple switches, see example:

#include <Button.h>
#define SW1 5
#define SW2 6

Button pb1 (SW1, HIGH, false);
Button pb2 (SW2, HIGH, false);

Button arr_pb[] = {pb1, pb2); //Add switches into an array

setup(){
  Serial.begin(9600);
}

loop() {

  //Read switch here. Create function to read multiple switches if necessary
  read_switches();

  if (arr_pb[0].get_short() && arr_pb[1].get_short()){
    Serial.println("SW1 & SW2 pressed");
  } else if (arr_pb[0].get_short()){
    Serial.println("SW1 pressed");
  } else if (arr_pb[1].get_short()){
    Serial.println("SW2 pressed");
  }

}

// To read the status of multiple switches/
void read_switches(){

  //Reset all switch values before reading
  reset_switches();

  for (int j = 0; j < 1000; j++) {
    for (int i = 0; i < 5; i++){
      arr_pb[i].read_switch();
      delayMicroseconds(8);
    }

    for (int i = 4; i >= 0; i--){
      arr_pb[i].read_switch();
      delayMicroseconds(8);
    }
  }
}

// Reset all switch values
void reset_switches(){
  for (int i = 0; i < 5; i++){
    arr_pb[i].reset();
  }
}

About

For use with Arduino and SPST switches

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.