【【【简单代码出现错误,小白求助~~~】】】-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3873|回复: 5

[已解决] 【【【简单代码出现错误,小白求助~~~】】】

[复制链接]
发表于 2014-1-22 17:55 | 显示全部楼层 |阅读模式
我想把《爱上Arduino》书上的例子1(LED闪烁)和例子3(用开关控制LED灯)综合起来,实现如下效果:
当按下开关时,LED灯闪烁,再次按下开关时,LED灯熄灭。


我写的代码如下:


const int LED=13;
const int BUTTON=7;
int val=0;
int oldval=0;
int state=0;


void setup(){
  pinMode(LED,OUTPUT);
  pinMode(BUTTON,INPUT);
}


void loop()
{
  val=digitalRead(BUTTON);
  if((val==HIGH)&&(oldval==LOW))
  {
    state=1-state;
    delay(100);}
   
    oldval=val;
   
    if(state==1)
    {digitalWrite(LED,HIGH);
    delay(500);
  digitalWrite(LED,LOW);
delay(500);}
    else{digitalWrite(LED,LOW);}
}

发表于 2014-1-23 11:32 | 显示全部楼层
本帖最后由 i7456 于 2014-1-23 15:51 编辑

[mw_shl_code=c,true]
// constants won't change. Used here to
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin
const int buttonPin =  7;

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
boolean state = false;

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 100;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);   
  Serial.begin(9600);
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();

if(digitalRead(buttonPin)==LOW)
   {
     delay(40);
     if(digitalRead(buttonPin)==HIGH)
     state = !state;
   }  
   
  //增加下面两行,解决常亮的问题
  if(!state)
    digitalWrite(ledPin,LOW);
   
  if((currentMillis - previousMillis > interval) && state){
    // save the last time you blinked the LED
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);

    if(ledState == LOW)
      Serial.println(LOW);
    else Serial.println  (HIGH);
  }
[/mw_shl_code]
 楼主| 发表于 2014-1-23 15:24 | 显示全部楼层

不稳定啊,有时候按下是一直亮着而不是闪烁
发表于 2014-1-23 15:36 | 显示全部楼层
本帖最后由 i7456 于 2014-1-23 15:51 编辑
z347830597 发表于 2014-1-23 15:24
不稳定啊,有时候按下是一直亮着而不是闪烁

按键接7和gnd,按键弹起后看效果。//增加下面两行,解决常亮的问题
  if(!state)
    digitalWrite(ledPin,LOW);

发表于 2014-1-23 18:49 | 显示全部楼层
z347830597 发表于 2014-1-23 15:24
不稳定啊,有时候按下是一直亮着而不是闪烁

有上拉或是下拉电阻吗
发表于 2014-1-24 09:19 | 显示全部楼层
nemon 发表于 2014-1-23 18:49
有上拉或是下拉电阻吗

内部上拉
[mw_shl_code=c,true]pinMode(buttonPin, INPUT_PULLUP); [/mw_shl_code]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-12-4 17:49 , Processed in 0.105000 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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