Automatic Chicken Feeder

Our project is about Automatic chicken feeder where our goal is to set the time of feeding the chickens in 4 divided patterns (5am, 10am, 1pm and 4pm) each day. We are using RTC DS3231 and Servo Motor Mg995. When the certain time were reached, the servo motor will rotate at 180 degress then stop at 5 seconds. Its like opening a door at 180 degrees for 5 seconds and then close. Can someone help me with proggraming this? This is my example program but its not running when I upload it.

#include <TimeLib.h>
#include <DS3231.h>
#include <Servo.h> 

Servo servo1;
Servo servo2;
Time t;
int pos = 0;

// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);

void setup()
{
  // Setup Serial connection
  Serial.begin(9600);
  // Uncomment the next line if you are using an Arduino Leonardo
  //while (!Serial) {}
  
  // Initialize the rtc object
  rtc.begin();
  
  // The following lines can be uncommented to set the date and time
  rtc.setDOW(MONDAY);     
   rtc.setTime(5, 0, 00);
  rtc.setDate(03, 05, 2021);   
  
servo1.attach(6);
  servo2.attach(9);
}

void loop()
{
  t = rtc.getTime();
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getTimeStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getDateStr());
  
  // Wait one second before repeating 
  delay (1000);
  {
 if ((rtc.getTimeStr() >= 5)) { 
  { for (pos = 0; pos <= 180; pos += 1) { 
    servo1.write(pos);              
    delay(5000);                       
  }
  
for (pos = 180; pos >= 0; pos -= 1) { 
    servo1.write(pos);              
    delay(2000);                       
  }
}
else if ((rtc.getTimeStr() >= 10)) { 
for (pos = 0; pos <= 180; pos += 1) { 
    
    servo1.write(pos);              
    delay(5000);                       
  }
  
for (pos = 180; pos >= 0; pos -= 1) { 
    servo1.write(pos);              
    delay(2000);                       
  }
}

else if ((rtc.getTimeStr() >= 13)) { for (pos = 0; pos <= 180; pos += 1) { 
    
    servo1.write(pos);              
    delay(5000);                       
  }
  
for (pos = 180; pos >= 0; pos -= 1) { 
    servo1.write(pos);              
    delay(2000);                       
  }
}
 else if ((rtc.getTimeStr() >= 16)) {
 for (pos = 0; pos <= 180; pos += 1) { 
    
    servo1.write(pos);              
    delay(5000);                       
  }
  
for (pos = 180; pos >= 0; pos -= 1) { 
    servo1.write(pos);              
    delay(2000);                       
  }
}


 if ((rtc.getTimeStr() >= 5)) { 
for (pos = 0; pos <= 180; pos += 1) { 
    
    servo2.write(pos);              
    delay(5000);                       
  }
  
for (pos = 180; pos >= 0; pos -= 1) { 
    servo1.write(pos);              
    delay(2000);                       
  }
}

else if ((rtc.getTimeStr() >= 10)) { 
for (pos = 0; pos <= 180; pos += 1) { 
    
    servo2.write(pos);              
    delay(5000);                       
  }
  
for (pos = 180; pos >= 0; pos -= 1) { 
    servo2.write(pos);              
    delay(2000);                       
  }
}

else if ((rtc.getTimeStr() >= 13)) { for (pos = 0; pos <= 180; pos += 1) { 
    
    servo2.write(pos);              
    delay(5000);                       
  }
  
for (pos = 180; pos >= 0; pos -= 1) { 
    servo2.write(pos);              
    delay(2000);                       
  }
}


else if ((rtc.getTimeStr() >= 16)) { for (pos = 0; pos <= 180; pos += 1) { 
    
    servo2.write(pos);              
    delay(5000);                       
  }
  
for (pos = 180; pos >= 0; pos -= 1) { 
    servo2.write(pos);              
    delay(2000);                       
  }
}
  

  }  }

someone help me because we really need this to run and present it next week :((

Who is WE ?

Tell us what you think this does ?


  {
 if ((rtc.getTimeStr() >= 5)) { 
  { for (pos = 0; pos <= 180; pos += 1) { 
}
    servo1.write(pos);              
    delay(5000);                       
  }
  
for (pos = 180; pos >= 0; pos -= 1) { 
    servo1.write(pos);              
    delay(2000);                       
  }
}


What’s not working ?


Do you know what a State Machine FSM is ?


You should not use delay() in your sketches, Google Arduino Blink Without Delay.

when the time is 5am the 2 servo motor will run for that declared degrees

Let’s try again, what does this do ?

  { for (pos = 0; pos <= 180; pos += 1) { 
}

it will open the door for 180 degrees

pos += 1) { 
}

Is there anything between { and } ?

none

for (pos = 0; pos <= 180; pos += 1) {
}

Since there is nothing between the { and } the for() does nothing other than loop.

what do you mean?

ahh i see i just put it wrong but my main problem is dat the servo motor is not running at time declared time how will i do that?

Not saying this is correct, but is this what you wanted ?

Old

for (pos = 0; pos <= 180; pos += 1) { 
}
    servo1.write(pos);              
    delay(5000);                       
  }

New

for (pos = 0; pos <= 180; pos += 1) 
{ 
    servo1.write(pos);              
    delay(5000);                       
}

yes i already changed it im sorry

Show us your new changes.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.


if ((rtc.getTimeStr() >= 5)) { 
  { for (pos = 0; pos <= 180; pos += 1) { 
    servo1.write(pos);              
    delay(5000);                       
  }
  

What does this do ?

rtc.getTimeStr() >= 5

it tells that when its 5 am the servo motors will run

What happens at 6, 7, 8, 9, 10, 11, etc. ?

Hello! Can someone help me with programming my project? I will use RTC DS3231 and Servo Motor MG995. The feeding time schedule will be at 5am, 10am, 1pm and 4pm each day and once it reached the time, the servo motor will run for 180 degress and stop for 5seconds, else it will close and open again for the next time declared. Thank You!

What have you tried so far ?