Pages: [1]   Go Down
Author Topic: 2 relays with delays  (Read 852 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys im in urgent need of putting together code for an arduino duemilanove to control two relays. One relay needs two continually run when it is started by a push button and then the sequence will begin, start motor then after 2 mins start second relay, run 2nd relay for 2mins then stop. this will continually run until told to stop. i know how to wire everything up but im having trouble with the code. Any ideas?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any ideas?
Don't use delay().

Quote
One relay needs two continually run when it is started by a push button
Relays don't run. The open or close. Push buttons don't start relays. They may generate a signal that the Arduino can read.

Quote
and then the sequence will begin
What sequence?

Quote
start motor then after 2 mins start second relay, run 2nd relay for 2mins then stop.
Motors run. Relays don't.

Quote
but im having trouble with the code.
No, you are having trouble with the requirements and terminology. You haven't even started to code. Get the terminology straight, define the requirements correctly, and the code will be easy.
Logged

the land of sun+snow
Offline Offline
Faraday Member
**
Karma: 149
Posts: 2790
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

To get around having to use long delay(xxx) calls in your code, which stall
the processor, take a look in the IDE at the "File > Examples > 2.Digital >
BlinkWithoutDelay" sketch.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys, yes i dont have my terminology right. At the mometn i have:

const byte bIn1=5;
const byte HEATER=12;
const byte MOTOR=13;

void setup()
{
  pinMode(bIn1,INPUT);
  pinMode(HEATER,OUTPUT);
  pinMode(MOTOR,OUTPUT);
}

void loop()
{
  if (digitalRead(bIn1)==LOW)
  { digitalWrite(MOTOR,HIGH);
 
   digitalWrite(HEATER,HIGH);
   delay(5000);
   
   digitalWrite(12,LOW);
   delay(5000);}
   
   else { digitalWrite(MOTOR,LOW);
         digitalWrite(HEATER,LOW);};
   
  ;
}

It does what i want it to with the switch being on a door so it stays on (lid closed ) and off (lid open), the only issue i have is that i want the motor and heater to stop immediately when i open the lid but it waits until the delay is finished and then stops. How can i get it to stop immediately?
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 115
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys, yes i dont have my terminology right. At the mometn i have:

const byte bIn1=5;
const byte HEATER=12;
const byte MOTOR=13;

void setup()
{
  pinMode(bIn1,INPUT);
  pinMode(HEATER,OUTPUT);
  pinMode(MOTOR,OUTPUT);
}

void loop()
{
  if (digitalRead(bIn1)==LOW)
  { digitalWrite(MOTOR,HIGH);
 
   digitalWrite(HEATER,HIGH);
   delay(5000);
   
   digitalWrite(12,LOW);
   delay(5000);}
   
   else { digitalWrite(MOTOR,LOW);
         digitalWrite(HEATER,LOW);};
   
  ;
}

It does what i want it to with the switch being on a door so it stays on (lid closed ) and off (lid open), the only issue i have is that i want the motor and heater to stop immediately when i open the lid but it waits until the delay is finished and then stops. How can i get it to stop immediately?

Thus why everybody told you to not use delay, forget that it exists.  Delay like goto is something you should NEVER need to use unless it's part of interfacing with low level hardware and even then it's dodgy.  Instead of delay 5000 you setup a variable and set it to millis() + 5000 and switch it off via an if statement.  Please go look at the blink without delay sample sketch it's all in there.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Instead of delay 5000 you setup a variable and set it to millis() + 5000
Addition with unsigned longs is not guaranteed to work. Subtraction is. Now - then > interval as test will always work. Then + 5000 > interval may not. Might as well learn to do it properly from the beginning.
Logged

Massachusetts
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like others explained it above but i found this pretty helpful, I was doing the same thing:

http://arduino.cc/playground/Code/AvoidDelay
Logged

Pages: [1]   Go Up
Jump to: