我们知道,Arduino里要多个LED灯能基于不同的延时值来亮灭,用普通的Delay是无法达到效果。一般要么上系统,要么用分时的方法,而本人基于数组和Millis()函数自制一个库,实现类似于时间继电器延时ON和OFF的功能,很方便能使用。以下为本人原创,网友可自行研究改进
一、先看库的头文件#ifndef_DelayRun_h #define_DelayRun_h #defineMAX_DELAY 10 #defineIF_DELAY_OFF(a,b,c) if(DelayRun.delay_off(a,b,c)) #defineIF_DELAY_ON(a,b,c) if(DelayRun.delay_on(a,b,c)) #defineIF_DELAYUS_OFF(a,b,c) if(DelayRun.delayus_off(a,b,c)) #defineIF_DELAYUS_ON(a,b,c) if(DelayRun.delayus_on(a,b,c)) #defineIF_RUN_ONCE(a,b) if(DelayRun.once_func(a,b)) #defineIF_RUN_NORMAL(a) if(DelayRun.normal_func(a)) #defineWHILE_DELAY_OFF(a,b,c) while(DelayRun.delay_off(a,b,c)) #defineWHILE_DELAY_ON(a,b,c) while(DelayRun.delay_on(a,b,c)) #defineWHILE_DELAYUS_OFF(a,b,c) while(DelayRun.delayus_off(a,b,c)) #defineWHILE_DELAYUS_ON(a,b,c) while(DelayRun.delayus_on(a,b,c)) /************************************************* *Definitions *************************************************/ class DelayRunClass { public: static void init_delay(uint8_t rstindex,uint8_t setindex); static uint8_t delay_on(uint32_t delaytime,uint8_t index,uint8_t setindex); static uint8_t delay_off(uint32_t delaytime,uint8_t index,uint8_t setindex); static uint8_t delayus_on(uint32_t delaytime,uint8_t index,uint8_t setindex); static uint8_t delayus_off(uint32_t delaytime,uint8_t index,uint8_t setindex); static uint8_t normal_func(uint8_t index); static uint8_t once_func(uint8_t index,uint8_t setindex); }; extern DelayRunClass DelayRun; extern uint8_t runstate[MAX_DELAY]; #endif
[size=13.3333px]二、库的实现
//For Arduino 1.0 #if ARDUINO>= 100 #include<Arduino.h> #else #include<WProgram.h> #endif #include "DelayRun.h" uint32_t delaytick[MAX_DELAY]; //自定义延时程序 void DelayRunClass::init_delay(uint8_t rstindex,uint8_tsetindex) { runstate[rstindex]=0; runstate[setindex]=1; } uint8_tDelayRunClass::delay_on(uint32_t delaytime,uint8_t index,uint8_t setindex) { if(runstate[index]==0) { runstate[setindex]=1; return 0; } if(delaytick[index]==0) delaytick[index]=millis()+delaytime; if(millis()>=delaytick[index]) { delaytick[index]=0; runstate[index]=0; runstate[setindex]=1; return 1; } else { return 0; } } uint8_tDelayRunClass::delay_off(uint32_t delaytime,uint8_t index,uint8_t setindex) { if(runstate[index]==0) { runstate[setindex]=1; return 0; } if(delaytick[index]==0) delaytick[index]=millis()+delaytime; if(millis()<=delaytick[index]) { return 1; } else { delaytick[index]=0; runstate[index]=0; runstate[setindex]=1; return 0; } } uint8_tDelayRunClass::delayus_on(uint32_t delaytime,uint8_t index,uint8_t setindex) { if(runstate[index]==0) { runstate[setindex]=1; return 0; } if(delaytick[index]==0) delaytick[index]=micros()+delaytime; if(micros()>=delaytick[index]) { delaytick[index]=0; runstate[index]=0; runstate[setindex]=1; return 1; } else { return 0; } } uint8_tDelayRunClass::delayus_off(uint32_t delaytime,uint8_t index,uint8_t setindex) { if(runstate[index]==0) { runstate[setindex]=1; return 0; } if(delaytick[index]==0) delaytick[index]=micros()+delaytime; if(micros()<=delaytick[index]) { return 1; } else { delaytick[index]=0; runstate[index]=0; runstate[setindex]=1; return 0; } } uint8_tDelayRunClass::normal_func(uint8_t index) { if(runstate[index]==1) return 1; else { return 0; } } uint8_tDelayRunClass::once_func(uint8_t index,uint8_t setindex) { if(runstate[index]==1) { runstate[index]=0; runstate[setindex]=1; return 1; } else { runstate[setindex]=1; return 0; } } DelayRunClassDelayRun;
三、把制作的库放在ArduinoIDE的Libraries文件库里,然后在自己的Arduino里使用,见如下Sample
本示例实现数字口2、3不同的时基闪灭,互不干扰,4为按键,按下后,5延时LOW
#include <DelayRun.h>
//IF_DELAY_OFF(a,b,c) if(DelayRun.delay_off(a,b,c))
//IF_DELAY_ON(a,b,c) if(DelayRun.delay_on(a,b,c))
//IF_DELAYUS_OFF(a,b,c) if(DelayRun.delayus_off(a,b,c))
//IF_DELAYUS_ON(a,b,c) if(DelayRun.delayus_on(a,b,c))
//IF_RUN_ONCE(a,b) if(DelayRun.once_func(a,b))
//IF_RUN_NORMAL(a) if(DelayRun.normal_func(a))
//WHILE_DELAY_OFF(a,b,c) while(DelayRun.delay_off(a,b,c))
//WHILE_DELAY_ON(a,b,c) while(DelayRun.delay_on(a,b,c))
//WHILE_DELAYUS_OFF(a,b,c) while(DelayRun.delayus_off(a,b,c))
//WHILE_DELAYUS_ON(a,b,c) while(DelayRun.delayus_on(a,b,c))
uint8_t key2;
uint8_t runstate[MAX_DELAY];
void setup() {
// put your setup code here, to run once:
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,INPUT);
pinMode(5,OUTPUT);
runstate[0]=1;
runstate[2]=1;
runstate[4]=1;
}
void loop() {
// put your main code here, to run repeatedly:
//if(DelayRun.delay_off(1000,0,1))
IF_DELAY_OFF(1000,0,1)
{
digitalWrite(2,HIGH);
}
IF_DELAY_OFF(1000,1,0)
{
digitalWrite(2,LOW);
}
IF_DELAY_OFF(300,2,3)
{
digitalWrite(3,HIGH);
}
IF_DELAY_OFF(300,3,2)
{
digitalWrite(3,LOW);
}
if(digitalRead(4)==LOW)
{
delay(10);
if(digitalRead(4)==LOW)
{
key2=1;
}
}
if(key2)
{
IF_RUN_ONCE(4,5)
{
digitalWrite(5,HIGH);
}
IF_DELAY_ON(1000,5,4)
{
digitalWrite(5,LOW);
key2=0;
}
}
}
上效果图见GIF
全部资料51hei下载地址:
DelayRun.rar
(6.47 KB, 下载次数: 11)
|