I have made the code for door open and close the door based on RTC .
1(When open time triggered the door will be open immediately and its working fine.
1)When close time triggered the, door needs to be wait for specified time before closing the door. This condition did not work, door closed immediately.
Please advice me, where i have done mistakes.
Here is my code:-
#include <Wire.h>
#include <EEPROM.h>
#include <DS3231.h>
DS3231 rtc(SDA, SCL);
Time t;
int irPin = 12;
int Hcount = 5;
unsigned long now;
boolean Hstate = true;
int led11 = 5;
int led10 = 6;
int led09 = 9;
int led08 = 10;
int Close = 7;
int Open = 8;
unsigned long lastChange = 0;
unsigned long lastHen;
boolean Time_enable = false; //=true;
boolean Cstate = false;
boolean WaitMilli = false;
int CloseRead;
int OpenRead;
int minu;
boolean Pstate = false;
int press = 0;
void setup() {
Serial.begin(9600);
Wire.begin();
rtc.begin();
pinMode(irPin, INPUT);
pinMode(Close, INPUT_PULLUP);
pinMode(Open, INPUT_PULLUP);
pinMode(led11, OUTPUT);
pinMode(led10, OUTPUT);
pinMode(led09, OUTPUT);
pinMode(led08, OUTPUT);
digitalWrite(Close, HIGH);
digitalWrite(Open, HIGH);
// DS3231_init(DS3231_CONTROL_INTCN);
CloseRead = digitalRead(Close);
Serial.println(CloseRead);
OpenRead = digitalRead(Open);
if (CloseRead == HIGH && Pstate == false)
{
digitalWrite(led11, LOW);
digitalWrite(led10, HIGH);
analogWrite (led09, 255);
}
if (CloseRead == LOW) //SmstF!=SmstT&&UPstate==true&& BypassState==true)
{
// delay(500);
digitalWrite(led11, LOW);
digitalWrite(led10, LOW);
//analogWrite (led09,0);
Pstate = true;
}
Serial.println(CloseRead);
delay(10);
}
void loop() {
String time1 = rtc.getTimeStr();
//parameters1[9] = rtc.getTimeStr();//rtc.getTimeStr();
unsigned long currentMillis = millis();
Serial.print(" clock: ");
Serial.println(time1);
t = rtc.getTime();
//DOOR OPEN EVENT
CloseRead = digitalRead(Close);
OpenRead = digitalRead(Open);
//Serial.print("CloseRead:");
//Serial.println(CloseRead);
//delay(1000);
// Serial.print(" OpenRead:");
//Serial.println(OpenRead);
if (t.min == 48) //
{
Time_enable = true;
}
if (Time_enable == true) //&&CloseRead==HIGH&&OpenRead==LOW)
{
if (!digitalRead(irPin) && Hstate && Cstate == false) {
Hcount--;
Hstate = false;
Serial.print("Count: ");
Serial.println(Hcount);
}
if (Hcount == 0)
{
Cstate = true;
}
if (digitalRead(irPin))
{
Hstate = true;
delay(100);
}
if (CloseRead == LOW && OpenRead == HIGH)
{
Serial.print("open: ");
digitalWrite(led11, HIGH);
digitalWrite(led10, LOW);
analogWrite (led09, 55);
}
if (OpenRead == LOW) //SmstF!=SmstT&&UPstate==true&& BypassState==true)
{
digitalWrite(led11, LOW);
digitalWrite(led10, LOW);
}
}
//DOOR CLOSE EVENT
if (t.min == 49&& WaitMilli == false)
{
Time_enable = false;
WaitMilli = true;
}
if (Time_enable == false && CloseRead == HIGH) //&&OpenRead==LOW)
{
if (CloseRead == HIGH && OpenRead == LOW)
{
if (!digitalRead(irPin) && Hstate) {
if (Hcount >= 0 && Hcount < 6)
{
Hcount++;
}
Hstate = false;
Serial.print("Count: ");
Serial.println(Hcount);
}
if (digitalRead(irPin))
{
Hstate = true;
delay(100);
}
}
/* if(Hcount==5&& CloseRead==HIGH)
{
Serial.print("close: ");
digitalWrite(led11, LOW);
digitalWrite(led10, HIGH);
analogWrite (led09,55);
}
if( CloseRead==LOW)
{
Serial.print("close: ");
digitalWrite(led11, LOW);
digitalWrite(led10, LOW);
Cstate=false;
//Time_enable=true;
//analogWrite (led09,255);
}*/
}
if ( WaitMilli == true)
{
unsigned long now = millis();
Serial.print("now");
Serial.print(lastHen);
if (now - lastHen >= 6000&&WaitMilli == true) // See if it's time
{
Serial.print("now - lastChange:");
Serial.print(lastHen - now);
Serial.print("close: ");
digitalWrite(led11, LOW);
digitalWrite(led10, HIGH);
analogWrite (led09, 55);
lastHen = now;
}
}
if ( CloseRead == LOW && WaitMilli == true)
{
Serial.print("close: ");
digitalWrite(led11, LOW);
digitalWrite(led10, LOW);
Cstate = false;
WaitMilli = false;
}
}
@paulpaulson might have just said there is no need to put any part of your text in large bold text.
Words are fine. We are accustomed to reading the inquiries here carefully, many of us every word, in order to discern the circumstances of your plight and the particular problem(s) you are experiencing.
Screaming the “important part” is a distraction and for some an annoyance.
No large deal. But try to rely on the power of your prose, not visual tricks.
'K? I’d scream that for a bit of fun, but TBH I would have to figure out how to even do that.
The code is fairly difficult to understand, partly because the variable names are not really descriptive of what they are used for. Your timing delay with millis is not going to work, the only place lastHen is set is within the if statement, so you are basing the time duration off the last time the door closed, which presumably is going to be more than 6000mS (unless you are intending to open and close this door more often than once per minute).
if ( WaitMilli == true)
{
unsigned long now = millis();
Serial.print(F("now"));
Serial.print(lastHen);
if (now - lastHen >= 6000 && WaitMilli == true) // See if it's time
{
Serial.print(F("now - lastChange:"));
Serial.print(lastHen - now);
Serial.print(F("close: "));
digitalWrite(led11, LOW);
digitalWrite(led10, HIGH);
analogWrite (led09, 55);
lastHen = now;
}
}
Can you give a general description of what the code is suppose to do? I'm guessing it controls the door to a chicken coop, and once the closing time has been reached you want to wait an additional time based on how long it has been since the last chicken crossed the doorway. Is there also code to keep count of the chickens?
Yes.
My purpose of code is ,When close time triggered the door reamains open for a specified time for entering the all chicken inside the cage and door will be closed.
Else if door will be close after the specified time.
There are three input switches.
1- Door close switch- switch become low when door closed.
2- Door open switch-switch become low when door opened.
3-Ir sensor switch for chicken counting.
RTC module for maping the real time.
Above mentioned all are my inputs.
Initially door closed condition.When Open time reaches Doo r will open immediately.
When Closed time reaches,Door will wait for specific time for enter the chicken inside cage ,the time elapsed door will close.
Suppose i have 5 chickens,so morning 9 o clock door open and all chicken went out and door remains open till close time.
I have set close time evening 6 o clock, when close time triggered door will wait to specific time for all 5 chickens returned ,if all 5 chicken returned immediately than door will close (this portion i have comment out),if chicken not returned and specific time elapsed door will close.
In my program only this waiting time is not working, remaining things aare ok.
At this moment I can’t properly look into your code.
But 6000 is only 6 seconds, for one minute you need 60000 milliseconds.
Use 600000UL, I’m not sure you need the UL but it won’t hurt. It just means to make sure to interpret that relatively large number as unsigned long.
Try it with that… to speed up testing you could,d use smaller larger numbers like 20000UL to see if the door stays open for 20 seconds, the put in the number you need or want.
Unless you already see it isn’t even waiting for 6 seconds. Does the door close right away like almost instantly?
Life is too short to spend a minute every time you test. ;-|
Please take a moment and post the current code in your next response. It is customary to do so when a sketch is evolving, that way we don’t make mistake editing your new changes into,your old code, and well all be 100 percent sure we are looking at the same code.