Untitled

自製按鈕diyButton.ino
mail@pastecode.io avatar
unknown
plain_text
2 years ago
668 B
0
Indexable
Never
//diyButton.ino
const int BUTTON_PIN = 14;  // 按鍵的接腳D5
const int LED_PIN =  2;   // LED燈的接腳D4

int buttonState = 0;   // 按鈕的狀態

void setup() {
  pinMode(LED_PIN, OUTPUT);   //設定LED的PIN腳為輸出
  pinMode(BUTTON_PIN, INPUT_PULLUP); //設定按鈕的接腳為輸入,因為我們要讀取它的狀態
}

void loop() {
  buttonState = digitalRead(BUTTON_PIN);  //讀取按鍵的狀態

  if(buttonState == LOW){          //如果按鍵按了
    digitalWrite(LED_PIN, LOW);   //LED就亮了 
  }else{                           //如果按鍵是未按下
    digitalWrite(LED_PIN, HIGH);    //LED就不亮
  }
}