Servo Not Stopping After Desired Time – Arduino Uno Cat Feeder Project

Hi, I’m working on an automated cat feeder using an Arduino Uno and a servo mechanism for dispensing food. The goal is for the servo to rotate for a certain amount of time, then stop. However, the servo keeps running longer than expected and doesn’t stop after the designated time.

Here’s the relevant portion of my code:

#include <Servo.h>




Servo myServo;
byte servoPin = 9;




void setup() {
  myServo.attach(servoPin);
}


void loop() {
   myServo.writeMicroseconds(700);
   delay(5000);
   myServo.write(91);
   delay(1000);


}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination

Does it slow down and maybe change direction when you write(90) ?

Loop() loops. That's what it does.

Yeth.

Assuming you have a continuous rotation servo, your code looks like 5 seconds of feeding, and one second of not feeding, repeated forever.

I'm sure your cat is quite happy with the current functioning.

You can do better, but delaying longer than 1000 milliseconds between feeding spins might be a version zero of this thing.

Every hour?

  delay(60ul * 60 * 1000):  // 3,600,000 milliseconds

a7