PushButton - skyrockprojects/wearable-tech GitHub Wiki

1. Push button

Button
Because the push button’s design works as picture, it's best to place in the middle over the DIP.

(picture Breadboard from http://ops.ieeebruins.org )

We need to be aware that there can be variance in electronic signals and “noise” that interfere with and within electronic circuits. This can cause unwanted and even dangerous changes in the electronic signal. To control this we’ll add resistors to reduce current flow, divide voltages, and more.

(gif from http://nolatlabs.com)
In this case, we place the resistor in order to reduce noise at the push button’s negative pin.

  • Copy and Paste the code:
int buttonPin = 2;

void setup() {
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(buttonPin) == LOW) {
    Serial.println("OFF");
  } 
  else if (digitalRead(buttonPin) == HIGH) {
    Serial.println("ON");
  }
  delay(100);
}    

2. State Change Detection

Normally, when you click once button and it would turn on something, and click the other once, it should turn off. But what's the difference form pushbutton above?

From 1. Push button, it just show the basic turn on and turn off, but you need to keep pushing to turn on. So, here is going to show you how to use code to click once and twice.

int led = 9;
int buttonPin = 2;
int buttonCounter = 0;  // counter for the number of button presses
int state = 0;  // current state of the button
int lastState = 0;  // previous state of the button

void setup() {
  pinMode(buttonPin, INPUT); // initialize the button pin as a input
  Serial.begin(9600); // initialize serial communication
  pinMode(led,OUTPUT);
}

void loop() {
  state = digitalRead(buttonPin); // read the pushbutton input pin
      
  // compare the buttonState to its previous state
  if(state != lastState){ 
    if(state == HIGH){ //press down
      buttonCounter++; //count how many times of pressing down 
      Serial.print(buttonCounter);
      Serial.println("on");
      }
    else{ //press up
      
      Serial.print(buttonCounter);
      Serial.println("off");
      }
    // %2 means the remainder of dividing by two
    if(buttonCounter%2==0){  //the remainder of dividing by two equal 0
      Serial.println("LED off");
      digitalWrite(led,LOW);
      }
    else{  //the remainder of dividing by two equal 1
      Serial.println("LED on");
      digitalWrite(led,HIGH);
      }
    delay(100);
    
    }
   lastState = state; 
  
  
}    

2.1 Ture table

For short, it's a logic.
But we just use a little bit of it's concept here.
(1 = HIGH, 0 = LOW)

Round lastState state buttonCounter NOTE
1 0 0 0
2 0 1 1 press one time
3 1 0 1
4 0 1 2 press two times
5 1 0 2
6 0 1 3 press three times
7 1 0 3

2.2 We've already have "state" to record button state, why "lastState"?

To save last state and state right now at same time.
If so, we can use the method to distinguish every "click".
One click means one press down and one press up.
No matter how long you push at once.
That's why we need to use this one as a condition:

  if(state != lastState)

2.3 How we use "buttonCounter" to turn on/off LEDs:

Because of the value of buttonCounter is regular, we can figure by ourselves.
(Let's why we need to write down!) (1 = HIGH, 0 = LOW)

Round lastState state buttonCounter NOTE1 NOTE2
1 0 0 0
2 0 1 1 press one time the time we want turn on something
3 1 0 1
4 0 1 2 press two times the time we want turn off something
5 1 0 2
6 0 1 3 press three times the time we want turn on something
7 1 0 3

When we want to turn on something, all the value of "buttonCounter" are odd number.

So, you can make a guess, what's the sequence of value when we want to turn off?