如何使用Arduino UNO和按钮在电路上闪烁LED?

3
    /*
  Button

 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. 


 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.


 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button
 */

// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int led =  11;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonHistory = 0;        //Counting variable for button being pressed

void setup() {
  // initialize the LED pin as an output:
  pinMode(led, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH){
    buttonState++;
  }

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
    if (buttonHistory >= 0 && buttonState >= 0) {  
      // turn LED on:;
      int x = x + (.1*255);
      analogWrite(led, x);  

    }
    else if (buttonState == LOW){
      analogWrite(led, 0);
    }

    if (buttonState == 11){
      buttonState = 0;
    }
    buttonHistory = buttonState;
}

以上一部分代码来自Arduino网站,但我还做了一些修改。

上面是我的代码。我的目标是在非焊接面包板上使用电阻器制作LED灯,在我按下该面包板上的按钮时点亮。它已经连接好了,我可以让LED灯亮起来,但并不会在我按下按钮时发生任何变化。我希望每次按下按钮后LED灯的亮度增加10%,然后当其达到最大亮度时,在下一次按下按钮时关闭。现在的问题是LED灯一直亮着,按下按钮也没有任何反应。


我必须点赞这个:"relaxen und watchen das blinkenlights" - Sergey Kalinichenko
1个回答

5
你必须将LED连接到Arduino的PWM输出之一。
PWM输出可以设置为0至255的值,这意味着与该值成比例的时间将设置输出电流,并且其余时间将保持在0V。
请查看Arduino官方网站上的以下示例以淡入淡出LED
你还应该使用map函数,因为它易于映射代码中的值。
至于你的代码,你可以尝试这个(我没有编译它,所以请原谅任何错误):
// Read the state of the pushbutton value, and update the fade LED value:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){
    // buttonState++; Probably this was the main bug in your code.
    buttonHistory++;
}

// We are cycling buttonHistory, not buttonState
if (buttonHistory == 11){
    buttonHistory = 0;
}

//if (buttonHistory >= 0 && buttonState >= 0) {
  // Turn LED on at desired intensity:;
  int x = map(buttonHistory, 0, 10, 0, 255); // Similar to doing x=.1*255*buttonHistory
  analogWrite(led, x);

//}
// REDUNDANT:
//else if (buttonState == LOW){
//  analogWrite(led, 0);
//}

你还应该考虑在循环之间添加delay(),否则LED的亮度更新速度太快了,你几乎察觉不到它(你会太快地调用digitalRead(buttonPin);)。一个好的位置可以在`analogWrite()`之后(感谢@mike的建议):
analogWrite(led, x);
delay(500);

对于开/关,您不需要PWM,因此任何引脚都可以。 - spring
1
你可能想在更新LED之后再延迟,否则在按下按钮和亮度变化之间会有0.5秒的暂停。 - mike
@skinnyTOD 但他说:“每次我按按钮,LED 灯要变亮十个百分点。”因此,LED 必须连接到 PWM 引脚。 - J.A.I.L.
谢谢,这个方法很有效!尤其是在我计算并理解了map方法的作用之后。 - David

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接