const byte swPin=2; //switch pin
const byte ledPin=13; //built-in LED
boolean ledState=LOW; //initial state of LED
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(swPin, INPUT_PULLUP); //enable pull-up resistor
digitalWrite(ledPin, ledState); //set LED OFF
}
void loop() {
boolean swState=digitalRead(swPin);
if (swState==LOW) { //switch pressed
if (ledState==HIGH) {ledState=LOW;} //ON:turn LED OFF
else {ledState=HIGH;} //OFF:turn LED ON
digitalWrite(ledPin, ledState); //toggle LED
}
}
因為 loop() 執行速度很快, 所以在按鈕按下的瞬間, digitalRead() 已經讀取到數十次到數百次 HIGH 與 LOW 切換的值了, 因此如果在到達穩定狀態前讀取到偶數次的 HIGH-LOW 變換, 那麼 LED 就明滅偶數次, 看起來 LED 就好像沒變換狀態; 如果是奇數次, 就跟程式邏輯相符, LED 會變換狀態. 如果在上面測試 1 程式中加入計數器, 就可以知道在按下按鈕時迴圈已跑多少圈了, 參考 "Arduino 輕鬆入門" 第 3-3 節 :
測試 2 :
const byte swPin=2; //switch pin
const byte ledPin=13; //built-in LED
boolean ledState=LOW; //initial state of LED int count=0; //bounce counter
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(swPin, INPUT_PULLUP); //enable pull-up resistor
digitalWrite(ledPin, ledState); //set LED OFF
Serial.begin(9600);
}
void loop() {
boolean swState=digitalRead(swPin);
if (swState==LOW) { //switch pressed
if (ledState==HIGH) {ledState=LOW;} //ON:turn LED OFF
else {ledState=HIGH;} //OFF:turn LED ON
digitalWrite(ledPin, ledState); //toggle LED Serial.print("Low count:"); Serial.println(count); //output counter value count++; //increment bounce counter
}
}
const byte swPin=2; //switch pin
const byte ledPin=13; //built-in LED
boolean ledState=LOW; //initial state of LED
int debounceDelay=200; //debounce delay (ms) unsigned long lastMillis; //record last millis
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(swPin, INPUT_PULLUP); //enable pull-up resistor
digitalWrite(ledPin, ledState); //set LED OFF initially
}
void loop() {
boolean swState=digitalRead(swPin);
if (swState==LOW) { //switch pressed
if (debounced()) { //debounced: reverse LED state
ledState = !ledState; //reverse LED state
digitalWrite(ledPin, ledState); //toggle LED
}
}
}
boolean debounced() { //check if debounced
unsigned long currentMillis=millis(); //get current elapsed time
if ((currentMillis-lastMillis) > debounceDelay) {
lastMillis=currentMillis; //update lastMillis with currentMillis
return true; //debounced
}
else {return false;} //not debounced
}
const byte swPin=2; //switch pin
const byte ledPin=13; //built-in LED
boolean ledState=LOW; //initial state of LED
int debounceDelay=50; //debounce delay (ms)
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(swPin, INPUT_PULLUP); //enable pull-up resistor
digitalWrite(ledPin, ledState); //set LED OFF
}
void loop() {
boolean swState=digitalRead(swPin);
if (swState==LOW) { //switch pressed
if (debounced()) { //debounced: reverse LED state
ledState = !ledState; //reverse LED state
digitalWrite(ledPin, ledState); //toggle LED
}
}
}
boolean debounced() { //check if debounced static unsigned long lastMillis=0; //record last millis
unsigned long currentMillis=millis(); //get current elapsed time
if ((currentMillis-lastMillis) > debounceDelay) {
lastMillis=currentMillis; //update lastMillis with currentMillis
return true; //debounced
}
else {return false;} //not debounced
}
const byte swPin=2; //switch pin
const byte ledPin=13; //built-in LED
boolean ledState=LOW; //initial state of LED
int debounceDelay=100; //debounce delay (ms)
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(swPin, INPUT_PULLUP); //enable pull-up resistor
digitalWrite(ledPin, ledState); //set LED OFF
}
void loop() {
if (debounced(swPin)) { //switch pressed and statble for 50 ms
ledState =! ledState; //reverse LED state
digitalWrite(ledPin, ledState); //toggle LED
}
}
boolean debounced(int pin) {
boolean currentState; //current pin state
boolean previousState; //previous pin state
previousState=digitalRead(pin); //record current state as previous
for (int i=0; i<debounceDelay; i++) { //detect if stable delay(1); //delay 1 ms
currentState=digitalRead(pin); //get current state
if (currentState != previousState) { //still unstable
i=0; //reset counter
previousState=currentState; //updtae previous state
}
} if (currentState==LOW) {return true;} //switch pressed (pull-up) else {return false;} //switch released
}
將下載之 zip 檔解壓縮到 Arduino IDE 安裝目錄的 libraries 下, 重開 IDE 即可看到 "檔案/範例" 下有 Bounce2 的範例. 根據此範例改寫為如下測試 5 :
測試 5 :
#include <Bounce2.h>
const byte swPin=2; //switch pin
const byte ledPin=13; //built-in LED
boolean ledState=LOW; //initial state of LED
int debounceDelay=50; //debounce delay Bounce debouncer=Bounce(); //create bounce object
void loop() {
debouncer.update();
int swState=debouncer.read();
if (swState==LOW) {
ledState = !ledState; //reverse LED state
digitalWrite(ledPin, ledState); //toggle LED
}
}
const int buttonPin=2; //the number of the pushbutton pin
const int ledPin=13; //the number of the LED pin
int ledState=HIGH; //the current state of the output pin
int buttonState; //previous stable state of the input pin
int lastButtonState=LOW; //the previous reading from the input pin
long lastDebounceTime=0; //the last time the output pin was toggled
long debounceDelay=50; //the debounce time; increase if the output flickers
void loop() {
int reading=digitalRead(buttonPin);
if (reading != lastButtonState) { //button state changed
lastDebounceTime=millis(); //update last debounce time
}
if ((millis() - lastDebounceTime) > debounceDelay) { //overtime
if (reading != buttonState) { //button state has changed
buttonState=reading; //update previous stable button state
if (buttonState == LOW) { //button pressed
ledState = !ledState; //reverse LED state
}
}
}
digitalWrite(ledPin, ledState);
lastButtonState=reading; //update last button state
}
const int buttonPin=2; //the number of the pushbutton pin
const int ledPin=13; //the number of the LED pin
int ledState=HIGH; //the current state of the output pin
int buttonState; //previous stable state of the input pin
int lastButtonState=LOW; //the previous reading from the input pin
long lastDebounceTime=0; //the last time the output pin was toggled
long debounceDelay=50; //the debounce time; increase if the output flickers
void loop() {
if (debounced(buttonPin)) { //debounced
ledState = !ledState; //reverse LED state
}
digitalWrite(ledPin, ledState);
}
boolean debounced(int pin) { //check if debounced
boolean debounced=false; //default
int reading=digitalRead(pin); //current button state
if (reading != lastButtonState) { //button state changed
lastDebounceTime=millis(); //update last debounce time
}
if ((millis() - lastDebounceTime) > debounceDelay) { //overtime
if (reading != buttonState) { //button state has changed
buttonState=reading; //update previous stable button state
if (buttonState == LOW) { //button pressed debounced=true;
}
}
}
lastButtonState=reading; //update last button state
return debounced;
}
#define BLYNK_PRINT Serial //Comment this out to disable prints and save space
#include <SoftwareSerial.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SimpleTimer.h>
"First theorized in 1971 by Leon Chua, a memristor, or "memory resistor", is an analog circuit element designed to exhibit varying resistance in order to "remember" the electrical signals it is experiencing, as well as the signals experienced in the past. This resistive memory allows circuits to emulate biological processes, such as the synapse, through artificial neural networking circuits."