九鼎创展论坛

标题: arduino ibox项目实战12-通过内核滴溚实现LED闪烁实验 [打印本页]

作者: armeasy    时间: 2014-10-25 18:31
标题: arduino ibox项目实战12-通过内核滴溚实现LED闪烁实验
硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个
软件平台:WIN7操作系统,android4.0android4.4系统
打开arduino的IDE开发工具,依次点击文件->示例->01.Basics->BlinkWithoutDelay,BlinkWithoutDelay的示例程序将会被打开,其源码如下:
  1. // constants won't change. Used here to set a pin number :
  2. const int ledPin =  LED1;      // the number of the LED pin LED1 = 22.

  3. // Variables will change :
  4. int ledState = LOW;             // ledState used to set the LED

  5. // Generally, you shuould use "unsigned long" for variables that hold time
  6. // The value will quickly become too large for an int to store
  7. unsigned long previousMillis = 0;        // will store last time LED was updated

  8. // constants won't change :
  9. const long interval = 1000;           // interval at which to blink (milliseconds)

  10. void setup() {
  11.   // set the digital pin as output:
  12.   pinMode(ledPin, OUTPUT);
  13. }

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

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

  22.   if(currentMillis - previousMillis >= interval) {
  23.     // save the last time you blinked the LED
  24.     previousMillis = currentMillis;  

  25.     // if the LED is off turn it on and vice-versa:
  26.     if (ledState == LOW)
  27.       ledState = HIGH;
  28.     else
  29.       ledState = LOW;

  30.     // set the LED with the ledState of the variable:
  31.     digitalWrite(ledPin, ledState);
  32.   }
  33. }
复制代码
       本实验和前面的LED灯闪烁实验效果相同,只不过实现的方法不同。前面使用了延时函数,而这里通过millis函数读取系统的当前滴溚,然后再用当前滴溚和前面保存的数据比较,如果相差1000,即1秒,则取反ledState,最终达到LED灯每隔一秒闪烁一次的效果。使用本方法将不占系统资源。






欢迎光临 九鼎创展论坛 (http://bbs.9tripod.com/) Powered by Discuz! X3.2