如何設定開始時 LED 狀態是 OFF??-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 535|回复: 5

[已解决] 如何設定開始時 LED 狀態是 OFF??

[复制链接]
发表于 2022-1-12 20:15 | 显示全部楼层 |阅读模式
本帖最后由 andywwf 于 2022-1-13 08:41 编辑

我想程式開始前, LED 狀態是關閉的.
請問是如何設置??
Thankyou



  1. // this constant won't change:
  2. const int  buttonPin = 2;    // the pin that the pushbutton is attached to
  3. const int ledPin = 13;       // the pin that the LED is attached to

  4. // Variables will change:
  5. int buttonPushCounter = 0;   // counter for the number of button presses
  6. int buttonState = 0;         // current state of the button
  7. int lastButtonState = 0;     // previous state of the button

  8. void setup() {
  9.   // initialize the button pin as a input:
  10.   pinMode(buttonPin, INPUT);
  11.   // initialize the LED as an output:
  12.   pinMode(ledPin, OUTPUT);
  13.   // initialize serial communication:
  14.   Serial.begin(9600);
  15. }


  16. void loop() {
  17.   // read the pushbutton input pin:
  18.   buttonState = digitalRead(buttonPin);

  19.   // compare the buttonState to its previous state
  20.   if (buttonState != lastButtonState) {
  21.     // if the state has changed, increment the counter
  22.     if (buttonState == HIGH) {
  23.       // if the current state is HIGH then the button went from off to on:
  24.       buttonPushCounter++;
  25.       Serial.println("on");
  26.       Serial.print("number of button pushes: ");
  27.       Serial.println(buttonPushCounter);
  28.     } else {
  29.       // if the current state is LOW then the button went from on to off:
  30.       Serial.println("off");
  31.     }
  32.     // Delay a little bit to avoid bouncing
  33.     delay(50);
  34.   }
  35.   // save the current state as the last state, for next time through the loop
  36.   lastButtonState = buttonState;


  37.   // turns on the LED every four button pushes by checking the modulo of the
  38.   // button push counter. the modulo function gives you the remainder of the
  39.   // division of two numbers:
  40.   if (buttonPushCounter % 4 == 0) {
  41.     digitalWrite(ledPin, HIGH);
  42.   } else {
  43.     digitalWrite(ledPin, LOW);
  44.   }

  45. }
复制代码
发表于 2022-1-12 21:15 | 显示全部楼层
// initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
后面再加一句
digitalWrite(ledPin, LOW);
因为你只是把灯设置成输出模式,但是并没有选择输出高还是输出低喔
 楼主| 发表于 2022-1-12 22:13 | 显示全部楼层
本帖最后由 andywwf 于 2022-1-12 22:40 编辑
ArBug 发表于 2022-1-12 21:15
// initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
后面再加一句

你好!
我試了, 但不行喔...
明明是這樣設置, 就是不行, 想不通......

发表于 2022-1-13 00:14 | 显示全部楼层
andywwf 发表于 2022-1-12 22:13
你好!
我試了, 但不行喔...
明明是這樣設置, 就是不行, 想不通......

接線圖?
合理懷疑你的開關初始狀態就是 HIGH,
所以不必按開關就亮燈了
发表于 2022-1-13 07:07 | 显示全部楼层
本帖最后由 lwq1947 于 2022-1-13 07:12 编辑

因为int buttonPushCounter = 0;所以if (buttonPushCounter % 4 == 0)条件成立,使得LED 狀態是ON.设置
int buttonPushCounter = -1;就满足你的要求了.
 楼主| 发表于 2022-1-13 08:40 | 显示全部楼层
lwq1947 发表于 2022-1-13 07:07
因为int buttonPushCounter = 0;所以if (buttonPushCounter % 4 == 0)条件成立,使得LED 狀態是ON.设置
int  ...

真的可以了, 謝謝您.
我問了好多人, 都解決不了呢.
原來是要用 - 來解決, 真是受教了.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 17:37 , Processed in 0.110052 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表