Servo Motor Code problem

So I am trying to write some code that tells a servo to spin for x amount of time every 24 hours. I am not sure how to do this, do you have any ideas? I am completely new to coding so sorry if this is a dumb question.

@johanwik Installation and Troubleshooting is for Problems with the Arduino itself NOT your project. It says so in the description of the section. Therefore I have moved your post here.

You may want to read this before you proceed:-
how to get the best out of this forum

Generally servos do not spin, they only turn over a limited range normally under 240 degrees. Unless you have a so called "continuous servo", which is not a servo at all but a normal motor.

You need to be a lot more specific about what your project is about before anything meaningful can be discussed.

Few things to say before going into details:

  • Typical servos don't "spin", they wiggle from side to side. So if you really do mean spin, like a wheel, make sure you get what's called a "continuous rotation" servo. (edit... as mentioned by previous poster)

  • Do you need this to happen at a particular time of day, like 5 past 3 every afternoon? If so you will need to get either an RTC (Real time clock) or be online to internet time.

  • If you are just thinking of 24 hours since last time, you could use the internal timer of an Arduino but you will need to look at the accuracy. You'll also need to be aware that the time starts over if Arduino loses power...

Hi, @johanwik

Can you tell us the application?
What do you want your project to accomplish?

Thanks... Tom.. :smiley: :+1: :coffee: :australia:

to answer as unspecific as you:

write a routine that creates the servo-control-signal according to the specs of the datasheet of the servo.

In a regular manner read out the time of a real-time-clock (RTC) and compare actual time with the time to spin the servo.

Does this description help? No not at all.
It is the same situation for us with your vague description
you have to describe your project much more specific

  • what servo ? (RC-servo, "real" servo-motor?
  • is there a requirement how fast the servo should spin?
  • how long in time or in rotations
    in case of an RC-servo what angle? at what speed?

which exact type of microcontroller are you using.
additional information that helps to adapt the answers to your knowledge
how much do you know about electronics?

  • voltage?, current?, resistance?

best regards Stefan

Hi @TomGeorge!
I am building an automatic pet feeder, so I will need my continuous servo to spin an auger-like thing inside of a piece of PVC at 3 o clock daily.

Hi, sorry for the unspecified variables.

I have a continuous servo and there are no requirements about how fast it should spin. I would like it to spin for 10 seconds.

Right now, I am only using a servo and an Arduino Uno and I know a bit about electronics, but not much about coding. I am using 3.3 volts, but i do not know how to measure resistance or current on an arduino.

How fast for how long?

Full speed for 10 seconds

Here is a crude way to do it. Turn it on at 3 PM and it will feed around 3 PM each day. The clock will probably drift a few minutes a day but it should be OK for a while. You can compensate for most of the drift. For example, if the feedings are averaging 3 minutes and 18 seconds earlier each day, add 3 minutes and 18 seconds to the timer interval.

if (currentMillis - lastMillis >= 24 * HOURS + (3 * MINUTES + 18 * SECONDS))

#include <Servo.h>

const unsigned long SECONDS = 1000;
const unsigned long MINUTES = 60 * SECONDS;
const unsigned long HOURS = 60 * MINUTES;

Servo AugerServo;
const byte AugerServoPin = 4;
const int StopAngle = 90;

unsigned long lastMillis = 0;

void Feed()
  AugerServo.write(180); // Full forward  (use 0 for the other direction)
  delay(10 * SECONDS);

void setup()
  Feed();  // Feed once on power up

void loop()
  unsigned long currentMillis = millis();

  // Feed every 24 hours after power up
  if (currentMillis - lastMillis >= 24 * HOURS)
    lastMillis += 24 * HOURS;

Thanks so much, I just got it working! :grinning_face_with_smiling_eyes:

If the servosize is bigger than 4 gram (9gram, 12gram 20 gram etc.) The servo needs its own powersupply. An Arduino can not deliver enough current for a bigger servo under load.

If you can afford to buy a Wemos D1 Mini (ESP8266) or a nodeMCU-board (ESP8266).

These little boards can be programmed with the arduino-IDE just the same way as an arduino-Uno with after installing some additional files.
This means these boards can do the job of the complete arduino.

These boards have WLAN onboard and you can connect them to your home-WLAN.
This gives you always actual time.

If you can afford a little more money an ESP32-board

the ESP32 has WLAN and bluetooth.

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.