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

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索

如何使用Arduino設(shè)計(jì)延時(shí)繼電器?

查看數(shù): 1060 | 評(píng)論數(shù): 4 | 收藏 0
關(guān)燈 | 提示:支持鍵盤翻頁<-左 右->
    組圖打開中,請(qǐng)稍候......
發(fā)布時(shí)間: 2024-8-30 18:42

正文摘要:

如題,求大俠!

回復(fù)

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

  1. #include <MsTimer2.h>
  2. // 設(shè)定各類函數(shù),定時(shí)、開始、停止、執(zhí)行
  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定時(shí)器
  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;//定時(shí)時(shí)間設(shè)定
  22.   start = true;
  23.   stop = false;
  24.   led = true;
  25.   pinMode(2, INPUT_PULLUP);
  26.   pinMode(13, OUTPUT);//執(zhí)行輸出
  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. }
復(fù)制代碼

看能否適用這個(gè)要求。
[color=rgba(0, 0, 0, 0.85)]
ID:155507 發(fā)表于 2024-8-31 12:50
給你個(gè)參考。

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:444392 發(fā)表于 2024-8-31 10:26
簡(jiǎn)單程序直接用等待語句,復(fù)雜場(chǎng)合用中斷?

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

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表