boolean 布林變數只有兩種數值,truefalse


int LEDpin = 5;       // LED 接 pin 5
int switchPin = 13;   // 按鈕開關一端接 pin 13,另一端接地

boolean running = false;

void setup()
  pinMode(LEDpin, OUTPUT);
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH);      // 開啟 pull-up 電阻

void loop()
  if (digitalRead(switchPin) == LOW)
  {  // 押下開關
    delay(100);                        // 延遲時間 (防止開關彈跳)
    running = !running;                // 切換 LED 控制變數
    digitalWrite(LEDpin, running)      // 控制 LED

See also

boolean operators
Variable Declaration


本頁由熱血青年 LBU 譯自英文版。

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.