Arduino 开关以打开继电器计时器 [英] Arduino Switch to Turn a Relay timer
问题描述
简而言之:我想在打开开关后打开继电器 30 秒.
Briefly: I would like to turn on a relay for 30 seconds, after I toggle a switch on.
我正在尝试在家中进行百叶窗自动化.
I'm trying to do a blinds automation at home.
我有一个简单的 ON-OFF-ON 开关,连接到连接到继电器的 Arduino.
I have a simple ON-OFF-ON switch, attached to an Arduino connected to Relays.
如果我从中心向下拨动开关,我想打开继电器#1 最多 30 秒.换句话说,当我切换时继电器打开,当定时器达到 30 秒时继电器关闭.
I want to turn on Relay#1 for a maximum of 30 seconds if I toggle the switch down from center. In other words, relay turns on when I switch, and when timer reaches 30 seconds relay turns off.
同样,如果我从中心向上拨动开关,我想打开 Relay#2 正好 30 秒
similarly I want to turn on Relay#2 for exactly 30 seconds if I toggle the switch up from center
当我切换回中心时,我希望计时器重置.
And when I switch back to center, I would like the timer to reset.
我不知道怎么做.有人可以帮忙吗?
I could not figure out how. Could anyone help?
为此我一直在尝试使用 elapsedMillis 库,这是一个很好的库,可以帮助我避免使用延迟:http://playground.arduino.cc/Code/ElapsedMillis
I have been trying to use elapsedMillis library for this, which is a nice library that helps me avoid using Delays: http://playground.arduino.cc/Code/ElapsedMillis
然而,即使我可以在没有 30 秒限制的情况下工作继电器,我也无法找出结束继电器工作的代码.这是我当前的代码:
However even though I could work the relays without the 30 second limitation, I couldn't figure out the code to end working of the relays. Here is my current code:
#include <elapsedMillis.h>
#define RELAY_ON 0
#define RELAY_OFF 1
#define RELAY1_TURNS_ON_BLINDS 5
#define RELAY2_SHUTS_DOWN_BLINDS 6
#define shutswitch A0
#define openswitch A1
bool LocalCommandToOpen;
bool LocalCommandToShut;
void setup() ////////SETUP////////
{
digitalWrite(RELAY1_TURNS_ON_BLINDS, RELAY_OFF);
digitalWrite(RELAY2_SHUTS_DOWN_BLINDS, RELAY_OFF);
pinMode(RELAY1_TURNS_ON_BLINDS, OUTPUT);
pinMode(RELAY2_SHUTS_DOWN_BLINDS, OUTPUT);
pinMode(shutswitch, INPUT);
pinMode(openswitch, INPUT);
} ////SETUP
void loop() { ///////LOOP
if (digitalRead(shutswitch) == 1)
{
LocalCommandToOpen = 1;
}
else
{
LocalCommandToOpen = 0;
}
if ( digitalRead(openswitch) == 1)
{
LocalCommandToShut = 1;
}
else
{
LocalCommandToShut = 0;
}
unsigned int CloseInterval = 14000;
elapsedMillis timeElapsedSinceCloseButtonPush = 0;
unsigned int OpenInterval = 14000;
elapsedMillis timeElapsedSinceOpenButtonPush = 0;
//MANUAL SWITCH OPERATION
if ( LocalCommandToShut == 1 )
{
digitalWrite(RELAY1_TURNS_ON_BLINDS, RELAY_OFF);
digitalWrite(RELAY2_SHUTS_DOWN_BLINDS, RELAY_ON);
}
else
{
digitalWrite(RELAY2_SHUTS_DOWN_BLINDS, RELAY_OFF);
}
//MANUEL DUGME ILE ACMA
if ( LocalCommandToOpen == 1)
{
digitalWrite(RELAY2_SHUTS_DOWN_BLINDS, RELAY_OFF);
digitalWrite(RELAY1_TURNS_ON_BLINDS, RELAY_ON);
}
else
{
digitalWrite(RELAY1_TURNS_ON_BLINDS, RELAY_OFF);
}
delay(500);
} /////////////////LOOP////////////////////////////////////
推荐答案
您可能会使用状态机;这让事情更容易理解.
You might use a state machine; this makes things a bit easier to follow.
类似于:
关于状态机的精彩讨论在这里:
A nice discussion of state machines is here:
这篇关于Arduino 开关以打开继电器计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!