2012-09-14 117 views
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网站复制的,但我对其进行了编辑。如何使用Arduino UNO和按钮使电路上的LED闪烁?

以上是我的代码。我的目标是当我按下面包板上的按钮时,在非焊接面包板上使用带有电阻的LED点亮。它全部连接好了,我可以让LED点亮,但是当我按下按钮时不能点亮。每次按下按钮时,我都希望LED亮度增加10%,然后当亮度达到最大亮度时,请在下次按下时关闭。我的问题是,现在,LED一直亮着,按下按钮不会起作用。

+0

我必须投票这一个: “relaxen UND watchen DAS blinkenlights”(HTTP://en.wikipedia .ORG /维基/ Blinkenlights)。 – dasblinkenlight

回答

5

您必须将LED连接到其中一个Arduino的PWM输出。

PWM输出可以从0到255被设置为值,这意味着成比例的时间应当设定电流到该输出的值,以及其余的时间将是在0   V.

在Arduino的官方网站上查看以下示例,以fade an LED

您还应该使用function 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); 
+0

对于开/关,你不需要PWM,所以任何引脚都可以。 –

+1

您可能想要在更新LED之后放置延迟,否则在按下按钮和亮度变化之间会有0.5秒的暂停。 – mike

+0

@mike:你完全正确。 –