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)] |