极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12595|回复: 5

【求助二】三极管触发开关如何实现触发后LED一直呼吸,再次触发后就灭掉

[复制链接]
发表于 2013-11-10 22:16:06 | 显示全部楼层 |阅读模式
int i=0;
void setup()

{

pinMode(11,OUTPUT);  

}
void breath()
{           for (int a=0; a<=255;a++)                //循环语句,控制PWM亮度的增加

           {

           analogWrite(11,a);

           delay(8);                             //当前亮度级别维持的时间,单位毫秒            

           }

           for (int a=255; a>=0;a--)             //循环语句,控制PWM亮度减小

           {

            analogWrite(11,a);

           delay(8);                             //当前亮度的维持的时间,单位毫秒  

           }
}
void loop()

{

  int n=analogRead(A0);   //读取模拟口数据      激发电压

   if  (n>0)
      {      
      delay(200);                          //适当延时跳开抖动
      i++;   
      if (i==100)
      {
        i=0;  //防止i越来越大,自己乱想的
      }
      }
      if  (i%2==0)  //偶数次LED灭掉
      {
      digitalWrite(11,LOW);
      }
      if  (i%2==1)  //奇数次亮起
     {                                
      breath(); //呼吸程序
      }
  }

下午解决一个问题后,又有新问题了,在第一次激发后,LED是在一直呼吸,但是第二次激发的时候LED并没有灭掉。
估摸原因是当二次激发的时候,LED正在执行呼吸程序,导致二次激发没有被探测到。
有什么方法?是不是有什么可以并行的方法?

回复

使用道具 举报

发表于 2013-11-10 23:10:45 | 显示全部楼层
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-11 13:04:16 | 显示全部楼层
二楼指出的方法试了下,还是没搞出来,大神。。。。。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-11 18:51:54 | 显示全部楼层
回复 支持 反对

使用道具 举报

发表于 2013-11-11 20:20:33 | 显示全部楼层
delay阻塞其他代码的运行。

在实际的程序中应该尽最大可能避免试用delay函数。

使用millis()函数获取系统当前毫秒数,自己加一个unsigned long类型变量来保存前一次的millis,当变化大到一定的值,比如你的8,再执行代码。

再加一个程序是否在运行的boolean变量做标志位,按动按钮的时候反转该标志位的值。每次loop时判断一下该标志位即可。
回复 支持 反对

使用道具 举报

发表于 2013-11-11 21:42:30 | 显示全部楼层
这个比较简单,你看一下Debounce的例子就可以了。

/*
Debounce

Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
a minimum delay between toggles to debounce the circuit (i.e. to ignore
noise).  

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

* Note: On most Arduino boards, there is already an LED on the board
connected to pin 13, so you don't need any extra components for this example.


created 21 November 2006
by David A. Mellis
modified 30 Aug 2011
by Limor Fried
modified 28 Dec 2012
by Mike Walters

This example code is in the public domain.

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

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }
  
  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-13 21:53 , Processed in 0.037622 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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