arduino彩灯计时器电路,Arduino的开关打开中继计时器

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.

I have a simple ON-OFF-ON switch, attached to an Arduino connected to Relays.

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.

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?

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

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

#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.

Similar to:

A nice discussion of state machines is here:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值