九鼎创展论坛
标题: arduino ibox项目实战12-通过内核滴溚实现LED闪烁实验 [打印本页]
作者: armeasy 时间: 2014-10-25 18:31
标题: arduino ibox项目实战12-通过内核滴溚实现LED闪烁实验
硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个
软件平台:WIN7操作系统,android4.0或android4.4系统
打开arduino的IDE开发工具,依次点击文件->示例->01.Basics->BlinkWithoutDelay,BlinkWithoutDelay的示例程序将会被打开,其源码如下:
- // constants won't change. Used here to set a pin number :
- const int ledPin = LED1; // the number of the LED pin LED1 = 22.
-
- // Variables will change :
- int ledState = LOW; // ledState used to set the LED
-
- // Generally, you shuould use "unsigned long" for variables that hold time
- // The value will quickly become too large for an int to store
- unsigned long previousMillis = 0; // will store last time LED was updated
-
- // constants won't change :
- const long interval = 1000; // interval at which to blink (milliseconds)
-
- void setup() {
- // set the digital pin as output:
- pinMode(ledPin, OUTPUT);
- }
-
- 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(currentMillis - previousMillis >= interval) {
- // 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);
- }
- }
复制代码 本实验和前面的LED灯闪烁实验效果相同,只不过实现的方法不同。前面使用了延时函数,而这里通过millis函数读取系统的当前滴溚,然后再用当前滴溚和前面保存的数据比较,如果相差1000,即1秒,则取反ledState,最终达到LED灯每隔一秒闪烁一次的效果。使用本方法将不占系统资源。
欢迎光临 九鼎创展论坛 (http://bbs.9tripod.com/) |
Powered by Discuz! X3.2 |