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.