找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 962|回复: 4
收起左侧

如何使用Arduino设计延时继电器?

[复制链接]
ID:204072 发表于 2024-8-30 18:42 | 显示全部楼层 |阅读模式
如题,求大侠!
回复

使用道具 举报

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

使用道具 举报

ID:204072 发表于 2024-9-2 22:04 | 显示全部楼层
感谢前辈指点!的确没有描述清楚,奉上原理图及时序图。烦请指点,谢谢!
如图,按钮AN按下后,D2、D3点亮,D4保持熄灭;按钮松开,开始延时,D2熄灭,D3延时后熄灭,D4点亮;延时结束后全部熄灭。
无标题.jpg
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表