欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1048|回復: 4
打印 上一主題 下一主題
收起左側

如何使用Arduino設計延時繼電器?

[復制鏈接]
跳轉到指定樓層
樓主
如題,求大俠!
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:444392 發表于 2024-8-31 10:26 | 只看該作者
簡單程序直接用等待語句,復雜場合用中斷?
回復

使用道具 舉報

板凳
ID:155507 發表于 2024-8-31 12:50 | 只看該作者
給你個參考。

int led_pin = 18;
int relay_pin = 12;
int sensor_pin = 4;

bool detected = false;
bool stop_relay = false;
unsigned long motion_stopped_time = 0; //will track the elapsed time after the motion stop

void setup() {
  pinMode(led_pin, OUTPUT);
  pinMode(relay_pin, OUTPUT);
  pinMode(sensor_pin, INPUT_PULLUP);
  Serial.begin(9600);
}


void loop(){

  if(digitalRead(sensor_pin) == HIGH){ //the sensor has detected motion

        //enclose the code here so it only executes once when motion is detected
        if(detected == false){
            Serial.println("Motion detected!");
            detected = true;

            digitalWrite(led_pin, HIGH); //turn led on
            digitalWrite(relay_pin, HIGH); //switch the relay on

        }

  }else{ //the sensor has stopped detecting motion

      if(detected == true){ //execute this only once when motion stops
            Serial.println("Motion stopped!");
            detected = false;

            digitalWrite(led_pin, LOW); //turn the led off

            motion_stopped_time = millis(); //remember the current time
            stop_relay = true;

       }else{
          if(stop_relay == true){ // ensures that this block will only execute once after the delay timer logic below

            //the current millis() time is 60sec more than the remembered time when the motion stopped
            if(millis() - motion_stopped_time >= 60000){
                digitalWrite(relay_pin, LOW); //switch the relay off
                stop_relay = false;
            }

          }
       }
  }
}

回復

使用道具 舉報

地板
ID:291549 發表于 2024-8-31 14:18 | 只看該作者
1、輸入定時延時函數數值。2、為了模擬,輸出為13,板子上面有LED觀察,可改為其它引腳為繼電器輸出。
3、2、3設定為定時開始和停止按鍵。
由于要求未說明白,本例使用MsTimer2定時器和硬件中斷,以提高穩定性。

  1. #include <MsTimer2.h>
  2. // 設定各類函數,定時、開始、停止、執行
  3. volatile int atime;
  4. volatile boolean start;
  5. volatile boolean stop;
  6. volatile boolean led;

  7. void msTimer2_func() {
  8.   start = start;
  9.   digitalWrite(13,start);
  10. }

  11. void attachInterrupt_fun_RISING_2() {
  12.   // MsTime2定時器
  13.   MsTimer2::set(atime, msTimer2_func);
  14.   MsTimer2::start();
  15. }

  16. void attachInterrupt_fun_RISING_3() {
  17.   MsTimer2::stop();
  18.   digitalWrite(13,stop);
  19. }

  20. void setup(){
  21.   atime = 5000;//定時時間設定
  22.   start = true;
  23.   stop = false;
  24.   led = true;
  25.   pinMode(2, INPUT_PULLUP);
  26.   pinMode(13, OUTPUT);//執行輸出
  27.   pinMode(3, INPUT_PULLUP);
  28.   interrupts();// 硬件中斷,2、3接開始和停止按鈕
  29.   attachInterrupt(digitalPinToInterrupt(2),attachInterrupt_fun_RISING_2,RISING);
  30.   attachInterrupt(digitalPinToInterrupt(3),attachInterrupt_fun_RISING_3,RISING);
  31. }

  32. void loop(){

  33. }
復制代碼

看能否適用這個要求。
[color=rgba(0, 0, 0, 0.85)]
回復

使用道具 舉報

5#
ID:204072 發表于 2024-9-2 22:04 | 只看該作者
感謝前輩指點!的確沒有描述清楚,奉上原理圖及時序圖。煩請指點,謝謝!
如圖,按鈕AN按下后,D2、D3點亮,D4保持熄滅;按鈕松開,開始延時,D2熄滅,D3延時后熄滅,D4點亮;延時結束后全部熄滅。
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表