九鼎创展论坛中文版English
登录 | 立即注册 设为首页收藏本站 切换到宽版
查看: 3853|回复: 0
打印 上一主题 下一主题

arduino ibox项目实战12-通过内核滴溚实现LED闪烁实验

[复制链接]
跳转到指定楼层
楼主
发表于 2014-10-25 18:31:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
硬件平台: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灯每隔一秒闪烁一次的效果。使用本方法将不占系统资源。

回复

使用道具 举报

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

本版积分规则

QQ|手机版|小黑屋|深圳市九鼎创展科技官方论坛 ( 粤ICP备11028681号-2  

GMT+8, 2024-6-15 08:52 , Processed in 0.020792 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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