Porta automatica pollaio - Automatic chicken coop door

being a beginner with arduino I can't solve a problem.
a piece of the code goes or if I enter the opening time the motor runs but once it reaches the finecors it certainly does not stop it is a code problem someone can help me

You are in the english part of the forum. If you want to get answers in italian ( and write in italian ) ask a moderator to move your topic to the italian part of the forum.

N.B. you need to provide more information to get help. At least you should post the sketch ( don't forget the code tags :wink: ) links to the used components and a schematic of your wiring.

#include <DS3231.h>
int Aswitch = 6;
int Cswitch = 7;
int Relay = 4;
int Relay1 = 5;
DS3231 rtc(SDA, SCL);
Time t;

const int OnHour = 18; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 0;
const int OffHour = 8; //SET TIME TO OFF RELAY
const int OffMin = 2;

void setup() {
Serial.begin(115200);
rtc.begin();
pinMode(Relay, OUTPUT);
digitalWrite(Relay, HIGH);
pinMode(Relay1, OUTPUT);
digitalWrite(Relay1, HIGH);
Serial.begin(9600);
pinMode(Aswitch, INPUT);
pinMode(Cswitch, INPUT);
}

void loop() {
t = rtc.getTime();
Serial.print(t.hour);
Serial.print(" hour(s), ");
Serial.print(t.min);
Serial.print(" minute(s)");
Serial.println(" ");
delay (1000);

if(t.hour == OnHour && t.min == OnMin){
digitalWrite(Relay,HIGH);
digitalWrite(Relay1,LOW);
Serial.println("LIGHT ON");
while(digitalRead(Aswitch) == HIGH)
{
Serial.println("door is opened");
digitalWrite(Relay1,HIGH);
}}

else if(t.hour == OffHour && t.min == OffMin){
  digitalWrite(Relay,LOW);
   digitalWrite(Relay1,HIGH);
  Serial.println("LIGHT OFF");
  while(digitalRead(Cswitch) == HIGH) 

{
Serial.println("door is closed");
digitalWrite(Relay,HIGH);
}}

}

Can you post your serial output?

Hello
Describe in simple words what shall happens and this very simple.
Have a nice day and enjoy coding in C++.

practically I set the door opening time the motor starts to turn at the entered time but when I press the limit switch the motor does not stop and continues to turn

This is odd:

    while (digitalRead(Aswitch) == HIGH)
    {
      Serial.println("door is opened");
      digitalWrite(Relay1, HIGH);
    }

I think you're looking for the limit switch to be HIGH so you can stop the motor. But I would expect the limit switch to remain pressed, so that would loop forever.

Also, when it's low, it has to come back on the next iteration of the loop function to check again. If minute has changed, you won't get to the while loop and the motor will stay on.

and now post the sketch in so called code tags "</>" to see what´s going wrong.

How are your limit switches wired? Are there pullup or pulldown resistors?
Please post a schematic how it is wired. That photo is not enough to see if it is corrcet. At least I can see no resistors. Does the motor open or close the limit switch when it reaches its end position.

N.B. You forgot to use codetags. Edit your post with the code, select all the code and press the </> button of the editor.

so what do i need to change as i am not practical someone could please rewrite it right

pullup

Hello
Did you read the washing instruction for this forum already?

yes

ok, I will stay delayed until the sketch is published in code tags.

I do not know what you mean

You are not very chatty.... :wink:

Are these black items the resistors ( I never saw such resistors )?
I think they are not connected to 5V - did you check that?
And what about the limit switches - are they close or open when the motor reaches its limits.

I told you about code tags already in #11:

 #include <DS3231.h>
int Aswitch = 6;
int Cswitch = 7;
int Relay = 4;
int Relay1 = 5;
DS3231  rtc(SDA, SCL);
Time t;

const int OnHour = 16; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 0;
const int OffHour = 8; //SET TIME TO OFF RELAY
const int OffMin = 2;

void setup() {
  Serial.begin(115200);
  rtc.begin();
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, HIGH);
   pinMode(Relay1, OUTPUT);
  digitalWrite(Relay1, HIGH);
   Serial.begin(9600); 
  pinMode(Aswitch, INPUT); 
 pinMode(Cswitch, INPUT); 
}

void loop() {
  t = rtc.getTime();
  Serial.print(t.hour);
  Serial.print(" hour(s), ");
  Serial.print(t.min);
  Serial.print(" minute(s)");
  Serial.println(" ");
  delay (1000);
  
  if(t.hour == OnHour && t.min == OnMin){
    digitalWrite(Relay,HIGH);
     digitalWrite(Relay1,LOW);
    Serial.println("LIGHT ON");
    while(digitalRead(Aswitch) == HIGH)  
  {
    Serial.println("door is opened"); 
digitalWrite(Relay1,HIGH);
  }}
    
    
    
    else if(t.hour == OffHour && t.min == OffMin){
      digitalWrite(Relay,LOW);
       digitalWrite(Relay1,HIGH);
      Serial.println("LIGHT OFF");
      while(digitalRead(Cswitch) == HIGH) 
  {
    Serial.println("door is closed"); 
  digitalWrite(Relay,HIGH);
      }}
      
}
   

It doesn't make sense like that, please answer all our questions.