|
1、輸入定時(shí)延時(shí)函數(shù)數(shù)值。2、為了模擬,輸出為13,板子上面有LED觀察,可改為其它引腳為繼電器輸出。
3、2、3設(shè)定為定時(shí)開(kāi)始和停止按鍵。
由于要求未說(shuō)明白,本例使用MsTimer2定時(shí)器和硬件中斷,以提高穩(wěn)定性。
- #include <MsTimer2.h>
- // 設(shè)定各類(lèi)函數(shù),定時(shí)、開(kāi)始、停止、執(zhí)行
- 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定時(shí)器
- MsTimer2::set(atime, msTimer2_func);
- MsTimer2::start();
- }
- void attachInterrupt_fun_RISING_3() {
- MsTimer2::stop();
- digitalWrite(13,stop);
- }
- void setup(){
- atime = 5000;//定時(shí)時(shí)間設(shè)定
- start = true;
- stop = false;
- led = true;
- pinMode(2, INPUT_PULLUP);
- pinMode(13, OUTPUT);//執(zhí)行輸出
- pinMode(3, INPUT_PULLUP);
- interrupts();// 硬件中斷,2、3接開(kāi)始和停止按鈕
- attachInterrupt(digitalPinToInterrupt(2),attachInterrupt_fun_RISING_2,RISING);
- attachInterrupt(digitalPinToInterrupt(3),attachInterrupt_fun_RISING_3,RISING);
- }
- void loop(){
- }
復(fù)制代碼
看能否適用這個(gè)要求。
[color=rgba(0, 0, 0, 0.85)] |
|