Real time clock based Door open and close ,Problem facing to close the door

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;

  }








}


Kindly help me to sort out this problem

Why are you screaming?

I am not screaming,I need helps to solve that problem.

Please describe the purpose and placement and type of the inputs to your program. What does reading HIGH and LOW indicate for each?

  pinMode(irPin, INPUT);
  pinMode(Close, INPUT_PULLUP);
  pinMode(Open, INPUT_PULLUP);
 . . .

  digitalWrite(Close, HIGH);
  digitalWrite(Open, HIGH);

Also, why are you writing to two of the inputs?



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

a7

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.

Reply#7

Please advise me

Insufficient.

This is not a guessing game for us.

Please describe the inputs to your program.

Please describe the outputs you desire.

Please explain (again, if you think you have already done) the logic which produces the outputs as informed by the inputs.

It would help to see your mechanical arrangement of all the parts. Even a hand drawn sketch would be worth 1000 words.

You will get better help if you tell us what it is you are up to, as fully and completely as possible.

a7

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.

#Rtc module for real time

OK, good, thanks!

But… why not just set the door Closed time for a specific time later than you set it?

Does something actually happen so you need to know that the door will be closing after a specific amount of time from when you set the Closed time?

Just curious, I hope you see my point.

a7

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.

I see, makes sense.

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

a7

i have done the changes but no imprvement

here is my code (edited portion)

if ( WaitMilli == true)
  {


    unsigned long now = millis();
    Serial.print("LastHen = ");
    Serial.println(lastHen);

    if (now - lastHen >= 60000UL&&WaitMilli == true) // See if it's time
    {

      Serial.print("now - lastChange = ");
      Serial.println(lastHen - now);
     

      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;

  }








}//loop end


Serial monitor shows

clock: 05:43:59
 clock: 05:43:59
 clock: 05:43:59
 clock: 05:44:00
LastHen = 0
now - lastChange = 4294841093
 clock: 05:44:00
LastHen = 126203
 clock: 05:44:00
LastHen = 126203
 clock: 05:44:00
LastHen = 126203
 clock: 05:44:00
LastHen = 126203
 clock: 05:44:00

I’m still at the beach.

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.

And fix this first:

      Serial.print("now - lastChange = ");
      Serial.println(lastHen - now);

which will make the printed numbers easier to interpret.

TIA and L8R

a7

Some how i have solved ,but i don't know whether its correct method?

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);




  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 (!digitalRead(irPin) && Hstate &&CloseRead==HIGH&&OpenRead==HIGH) {

        Serial.print("INTERUPT: ");
        digitalWrite(led11, HIGH);
        digitalWrite(led10, LOW);
        analogWrite (led09, 55);

    }
     if (digitalRead(irPin))
      {
        Hstate = true;
        delay(100);
      }
  */
  if (t.min == 33) //
  {

    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 == 34 && WaitMilli == false && CloseRead == HIGH)
  {

    Time_enable = false;
    lastHen = millis();////added this line
    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);
    }
  }
  else  if ( WaitMilli == true && CloseRead == HIGH)
  {


    unsigned long now = millis();
    Serial.print("LastHen = ");
    Serial.println(lastHen);
    Serial.print("now - lastChange = ");
    Serial.println(lastHen - now);

    if (now - lastHen >= 20000UL && WaitMilli == true) // See if it's time
    {

      Serial.print("time elapsed");


      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;

  }








}

its working fine.Please let me know the is it correct way?

Counting individuals is difficult at best.

Two hens can overlap each other and be counted as one.

Maybe some kind of transfer tunnel would be useful.

However, suggest you forget counting hens, just give them enough time to get in, they know what to do.

:smile: I have planned to send an sms to owner if any Hen missing at evening

Complexity and animals always results in failure.


Wander if turning a light on in the coup will encourage entering ? :thinking:

Often when these coop threads come up, the door open and close are based on sunrise and sunset, not a fixed time of day.