如何使用闪烁一个Arduino UNO和一个按钮的电路上的LED? [英] How to blink an LED on a circuit using an Arduino UNO and a button?

查看:586
本文介绍了如何使用闪烁一个Arduino UNO和一个按钮的电路上的LED?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    /*
  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;
}

有些上面code是从Arduino的网站复制,但我编辑了。

Some of the above code is copied from the Arduino website, but I edited it.

以上是我的code。在这里,我的目标是使一个LED与非焊锡面包板电阻光,当我美元的线路板p $ PSS按钮。这是所有的有线了,我可以得到的LED光,而不是当我preSS的按钮。我想要LED获得百分之十的亮每次我按下按钮的时间,然后当它在最大亮度,关闭在接下来的preSS。我的问题是,现在的LED是不断上pressing按钮不会做的事情。

Above is my code. My goal here is to make an LED with a resistor on a non solder breadboard to light when I press button on that breadboard. It is all wired up, and I can get the LED to light, but not when I press the button. I want the LED to get brighter by ten percent for every time I push the button, and then when it is at max brightness, turn off on the next press. My issue is that right now, the LED is constantly on and pressing the button won't do a thing.

推荐答案

您必须连接 LED 的到Arduino的 PWM输出之一。

You have to connect the LED to one of the Arduino's PWM outputs.

PWM输出可从0到255被设置为值,这意味着一个比例的时间将被设置的电流到该输出的值,以及其余的时间将是在0 NBSP;:V

PWM outputs can be set to values from 0 to 255, meaning that a proportional time to the value it will be setting current to that output, and the rest of the time it will be at 0 V.

检查下面的例子在Arduino的官方网站淡出了LED

Check the following example at Arduino's official site to fade an LED.

您也应该使用功能地图,因为它简化了您的code映射值

You should also use the function map, as it eases your code mapping values.

至于你的code,你可以试试这个(我还没有编译它,所以请原谅任何错误)

As for your code, you could try this (I haven't compiled it, so forgive any mistake):

// 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);
//}

您还应该考虑增加周期之间的延迟(),否则你会被更新的LED太快因为注意到它(你会打电话力度 digitalRead(buttonPin); 次数太多太快)。一个好地方可以analogWrite后`()(您的建议感谢@mike):

You should also think about adding delay() between cycles, or you'll be updating the intensity of the LED too quickly as to notice it (you'll call digitalRead(buttonPin); too many times too quickly). A good place can be after `analogWrite()' (Thanks @mike for the suggestion):

analogWrite(led, x);
delay(500);

这篇关于如何使用闪烁一个Arduino UNO和一个按钮的电路上的LED?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆