1、输入定时延时函数数值。2、为了模拟,输出为13,板子上面有LED观察,可改为其它引脚为继电器输出。
3、2、3设定为定时开始和停止按键。
由于要求未说明白,本例使用MsTimer2定时器和硬件中断,以提高稳定性。
- #include <MsTimer2.h>
- // 设定各类函数,定时、开始、停止、执行
- volatile int atime;
- volatile boolean start;
- volatile boolean stop;
- volatile boolean led;
- void msTimer2_func() {
- start = start;
- digitalWrite(13,start);
- }
- void attachInterrupt_fun_RISING_2() {
- // MsTime2定时器
- MsTimer2::set(atime, msTimer2_func);
- MsTimer2::start();
- }
- void attachInterrupt_fun_RISING_3() {
- MsTimer2::stop();
- digitalWrite(13,stop);
- }
- void setup(){
- atime = 5000;//定时时间设定
- start = true;
- stop = false;
- led = true;
- pinMode(2, INPUT_PULLUP);
- pinMode(13, OUTPUT);//执行输出
- pinMode(3, INPUT_PULLUP);
- interrupts();// 硬件中断,2、3接开始和停止按钮
- attachInterrupt(digitalPinToInterrupt(2),attachInterrupt_fun_RISING_2,RISING);
- attachInterrupt(digitalPinToInterrupt(3),attachInterrupt_fun_RISING_3,RISING);
- }
- void loop(){
- }
复制代码
看能否适用这个要求。
[color=rgba(0, 0, 0, 0.85)] |