Go Down

Topic: Does Arduino have an automatic time-out? (Read 444 times) previous topic - next topic

PlayStationFarm

Mar 24, 2013, 06:30 pm Last Edit: Mar 25, 2013, 12:54 am by PlayStationFarm Reason: 1
I'm very new to the Arduino world and I have no experience with programming language.  I'm using my Arduino UNO R3 to run an automatic door operator.  I want the door to open, stay open for a set time, close, and stay closed for a set time, repeat (24 hour cycle).  I have developed the following sketches to accomplish this task using an Arduino R3 motor shield.  My  sketch #1 (quick test) works perfectly but when I upload sketch #2 (my desired timing) the door will open and never will shut.  I wonder if the Arduino has an automatic time-out function that I'm not aware of.  Can you guys help me trouble-shoot my sketch?  Is there a better way to do long term motor shutdown times?  Thanks.
_______________________________________________________________
Code: [Select]
// Sketch #1 (my quick test sketch)
//Drive DC motor CW 10 seconds, pause 2 seconds, run motor CCW 10 seconds, pause 2 seconds, repeat.
//L. C. H. 2/23/2013

// Motor definitions
int DIR_A = 12;
int SPEED_A = 3;
int BREAK_A = 9;

void setup()
{
 pinMode(SPEED_A, OUTPUT);
 pinMode(DIR_A, OUTPUT);
 pinMode(BREAK_A, OUTPUT);

 // release break
 digitalWrite(BREAK_A, LOW);
}

void loop()
{
 // run motor CW full speed.  (Max is 250)
 digitalWrite(DIR_A, HIGH);
 analogWrite(SPEED_A, 250);

 // run motor for 10 seconds
 delay(10000);

// stop motor
 analogWrite(SPEED_A, 0);

 // pause 2 seconds
 delay(2000);

 // move motor CCW full speed.  (Max is 250)
 digitalWrite(DIR_A, LOW);
 analogWrite(SPEED_A, 250);

 // run motor for 10 seconds
 delay(10000);

// stop motor
 analogWrite(SPEED_A, 0);

 // pause 2 seconds
 delay(2000);

}

_______________________________________________________________
Code: [Select]
//Sketch #2 (my long-term-pause sketch w/desired timing)
//Drive DC motor CW for 18 seconds, pause 14 hours, run CCW for 18 seconds, pause 10 hours, repeat.
//L. C. H. 2/24/2013

// Motor A definitions
int DIR_A = 12;
int SPEED_A = 3;
int BREAK_A = 9;

void setup()
{
 pinMode(SPEED_A, OUTPUT);
 pinMode(DIR_A, OUTPUT);
 pinMode(BREAK_A, OUTPUT);

 // release break
 digitalWrite(BREAK_A, LOW);
}

void loop()
{
 // run motor CW full speed.  (Max is 250)
 digitalWrite(DIR_A, HIGH);
 analogWrite(SPEED_A, 250);

 // run motor for 18 seconds to open
 delay(18000);

// stop motor
 analogWrite(SPEED_A, 0);

 // pause door open 14 hours (minus 18000 ms runtime)
 delay(50398200);

// run motor CCW full speed.  (Max is 250)
 digitalWrite(DIR_A, LOW);
 analogWrite(SPEED_A, 250);

 // run motor for 18 seconds to close
 delay(18000);

 // stop motor
 analogWrite(SPEED_A, 0);

 // pause door closed 10 hours (minus 18000 ms runtime)
 delay(35998200);
 
}

billroy

Your constants are being evaluated as 16-bit int variables,and some of the values are too large to fit.

Marking your large constants with UL to make them unsigned long might help:

Code: [Select]

  delay(50398200UL);


-br

perhof

Using delay or millis for schedules like that will not work well. You will be surprised how much drift you will see over a few days and if the power is lost your Arduino will start opening and closing your door at totally different times.
Consider adding an RTC to keep track of the actual time instead.

PlayStationFarm

#3
Mar 24, 2013, 11:54 pm Last Edit: Mar 24, 2013, 11:58 pm by PlayStationFarm Reason: 1
I don't know what an RTC is.  What is it and how is it used? Thanks.

Edit: I have been reading many posts here and I found that RTC means "real time clock".  I'll study more.

perhof

DS1307 is probably the most popular one but there are other options.
http://playground.arduino.cc/Main/InterfacingWithHardware#time

The easiest way to get started is a kit or ready-made module including the RTC chip, battery, crystal and other necessary components.
http://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit

Go Up