---
# System prepended metadata

title: 亮滅自保 debounce

---

# 亮滅自保 debounce

```c++=
// 程式介紹
int ledState;          // LED狀態，沒賦值，預設為0
int buttonState;       // 0.001豪秒以前，按鈕狀態
int lastButtonState;   // 0.001豪秒以前，按鈕狀態
unsigned long ChangeTime;            // 按按鈕的時間點
unsigned long debounceDelay = 50;    // 用50ms debounce

void setup() {
  pinMode(2, INPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  int now = digitalRead(2);//現在按鈕狀態
  
  if (now != lastButtonState) 
  //!=不等於
  //現在按鈕狀態  不等於  0.001毫秒以前按鈕狀態 
  //按了按鈕
  //if滿足條件  執行下面一行
  //如果有兩行  要用{  }包住
  
    ChangeTime = millis();
  //millis()  系統開機時間
  //系統開機開始記錄時間  以毫秒為單位  約可記錄50天
  //超過50天  從0從新紀錄時間
  //按了按鈕  記錄當下的時間點
  
  if ((millis() - ChangeTime) > debounceDelay) 
  //系統時間-按按鈕時間 > 50ms
  //例如587-501 >50
  //消除雜訊  消除電路彈跳  消抖debounce
   if (now != buttonState) 
   //現在按鈕狀態  不等於  0.001豪秒以前，按鈕狀態
   //按了按鈕
      { buttonState = now;
        if (now == HIGH) 
           ledState = !ledState;//!0就是1   !1就是0       
      }
  
  digitalWrite(13, ledState);
  lastButtonState = now;
}
```

```c++=
void loop() {                   //1.燈滅，沒按按鈕
  int now = digitalRead(2);    //now=0
  if (now != lastButtonState) //0!=0  不成立  不執行
    ChangeTime = millis();   //不執行
  if ((millis() - ChangeTime) > debounceDelay) 
  //  約138-0 > 50  成立
   if (now != buttonState) //0!=0  不成立  不執行
      { buttonState = now;       //不執行
        if (buttonState == HIGH) //不執行
           ledState = !ledState; //不執行      
      }
  digitalWrite(13, ledState);    //燈滅
  lastButtonState = now;     //lastButtonState= 0    
}
```

```c++=
void loop() {                   //2.燈滅，按按鈕，燈亮
  int now = digitalRead(2);    //now=1
  if (now != lastButtonState) //1!=0  成立 
    ChangeTime = millis();   //ChangeTime= 約2657
  if ((millis() - ChangeTime) > debounceDelay) 
  //  約2721-2657 > 50  成立
   if (now != buttonState) //1!=0  成立  
      { buttonState = now;       //buttonState = 1
        if (buttonState == HIGH) //1=1成立
           ledState = !ledState; //0=!0=1=亮      
      }
  digitalWrite(13, ledState);    //燈亮
  lastButtonState = now;     //lastButtonState= 1    
}
```

```c++=
void loop() {                   //3.燈亮，不按按鈕
  int now = digitalRead(2);    //now=0
  if (now != lastButtonState) //0!=1  不成立 
    ChangeTime = millis();   //不執行
  if ((millis() - ChangeTime) > debounceDelay) 
  //  約4567-2657 > 50  成立
   if (now != buttonState) //0!=1  成立
      { buttonState = now;       //buttonState = 0
        if (buttonState == HIGH) //0=1不成立  不執行
           ledState = !ledState; //不執行     
      }
  digitalWrite(13, ledState);    //ledState=1  燈亮
  lastButtonState = now;     //lastButtonState= 0    
}
```

```c++=
void loop() {                   //4.燈亮，按按鈕，燈滅
  int now = digitalRead(2);    //now=1
  if (now != lastButtonState) //1!=0  成立 
    ChangeTime = millis();   //ChangeTime= 約12657
  if ((millis() - ChangeTime) > debounceDelay) 
  //  約12721-12657 > 50  成立
   if (now != buttonState) //1!=0  成立  
      { buttonState = now;       //buttonState = 1
        if (buttonState == HIGH) //1=1成立
           ledState = !ledState; //1= !1= 0= 滅      
      }
  digitalWrite(13, ledState);    //ledState=0 燈滅
  lastButtonState = now;     //lastButtonState= 1   
}
```

```c++=
void loop() {                   //5約=1.燈滅，沒按按鈕
  int now = digitalRead(2);    //now=0
  if (now != lastButtonState) //0!=1  成立  
    ChangeTime = millis();   //ChangeTime= 約13456
  if ((millis() - ChangeTime) > debounceDelay) 
  //  約13511-13456 > 50  成立
   if (now != buttonState) //0!=1  成立 
      { buttonState = now;       //buttonState = 0
        if (buttonState == HIGH) //0=1  不成立不執行
           ledState = !ledState; //不執行      
      }
  digitalWrite(13, ledState);    //ledState=0 燈滅
  lastButtonState = now;     //lastButtonState= 0   
}
```