# Help getting a servo to activate on time for a specific amount of time

**URL:** <https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737>\
**Category:** General Guidance\
**Created:** [December 31, 2022, 10:47pm UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737 "2022-12-31T22:47:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![legobatman\_321](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@legobatman\_321](https://forum.arduino.cc/u/legobatman_321)\
**Post date:** [December 31, 2022, 10:47pm UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/1 "2022-12-31T22:47:04Z")

</div>

I'm very new at C++ and need to finish this project soon but I'm having difficulty with the code. I'm not sure how to do this. This is what I have so far. I'm trying to get it to turn on for maybe a minute every few hours. If there is a better way to do this please do tell.

```cpp
#include <Servo.h>
#include <unistd.h>
#define servoPin 9

void setup() {
pinMode(servoPin, HIGH);
}

void loop() {
delayMicroseconds(1000);
digitalWrite(servoPin, HIGH);
delayMicroseconds(2000);
digitalWrite(servoPin, LOW);
delayMicroseconds(18550); 
}

```

---

<div class="post-metadata">

**Author:** ![bluejets](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bluejets/32/1013044_2.png) [@bluejets](https://forum.arduino.cc/u/bluejets)\
**Post date:** [December 31, 2022, 10:49pm UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/2 "2022-12-31T22:49:04Z")

</div>

> [@legobatman\_321](#):
>
> If there is a better way to do this please do tell.

Use millis in place of delays.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [December 31, 2022, 10:54pm UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/3 "2022-12-31T22:54:14Z")

</div>

Read thru these examples:

> [@Flashing multiple LEDs at the same time](https://forum.arduino.cc/t/flashing-multiple-leds-at-the-same-time/1065564):
>
> Introduction A common question is to ask how to flash 2 (or more) LEDs at the same time. Typically the person asking will be using delay and will be experiencing the main problem with delay: it blocks and stops anything else from happening. This tutorial contains several different approaches to the problem of flashing 2 or more LEDs at the same time with independent timing for each LED. The basic principles for each can all be adapted for other situations requiring multiple timers working inde…

digitalWrite(servoPin, HIGH);  
🤔  
Use servoWrite().

---

<div class="post-metadata">

**Author:** ![anon56112670](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@anon56112670](https://forum.arduino.cc/u/anon56112670)\
**Post date:** [December 31, 2022, 10:54pm UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/4 "2022-12-31T22:54:59Z")

</div>

> [@legobatman\_321](#):
>
> If there is a better way to do this please do tell.

Use this

> [@legobatman\_321](#):
>
> `#include <Servo.h>`

(Hint: it comes with examples)

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [December 31, 2022, 10:55pm UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/5 "2022-12-31T22:55:17Z")

</div>

Use some math.  
1,000,000 uS = 1 S  
1 minute = 60S  
1 hour = 60 minutes  
a few hours = (60 x a few minutes)

delayMicroseconds() won't time such long intervals, read the documentation on that.

delay() will work

```cpp
#include <Servo.h>
#include <unistd.h>
#define servoPin 9

const long mS_per_S = 1000;
const long mS_per_min = 60 * mS_per_S;
const long min_per_hr = 60;

const long timeOff = 2 * mS_per_min + 5 * mS_per_S // 2 minutes 5 seconds off time
const long timeOn = (3 * min_per_hr * mS_per_min) - timeOff; // almost 3 hours on

void setup() {
pinMode(servoPin, HIGH);
}

void loop() {
digitalWrite(servoPin, HIGH);
delay(timeOn);
digitalWrite(servoPin, LOW);
delay(timeOff);
}

```

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [December 31, 2022, 11:08pm UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/6 "2022-12-31T23:08:32Z")

</div>

I hope you are not trying to power a servo from an I/O pin. It sort of looks that way...

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [December 31, 2022, 11:12pm UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/7 "2022-12-31T23:12:09Z")

</div>

Don't try to power the servo from the Arduino 5V output, either.

---

<div class="post-metadata">

**Author:** ![legobatman\_321](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@legobatman\_321](https://forum.arduino.cc/u/legobatman_321)\
**Post date:** [December 31, 2022, 11:33pm UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/8 "2022-12-31T23:33:57Z")

</div>

Thank you for the math. It helped a lot!

---

<div class="post-metadata">

**Author:** ![lastchancename](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lastchancename/32/215426_2.png) [@lastchancename](https://forum.arduino.cc/u/lastchancename)\
**Post date:** [December 31, 2022, 11:52pm UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/9 "2022-12-31T23:52:58Z")

</div>

If you’re talking about ‘time of day’ for the action, you’ll need to go out and buy an RTC (real time clock) module, or an internet connection for NTP time.

There are examples, and it will track much netter time than delays.

---

<div class="post-metadata">

**Author:** ![legobatman\_321](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@legobatman\_321](https://forum.arduino.cc/u/legobatman_321)\
**Post date:** [January 1, 2023, 12:11am UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/10 "2023-01-01T00:11:23Z")

</div>

I have tried to improve this a bit more. Would this work?

```cpp
#include <Servo.h>
#include <unistd.h>
Servo myservo;
const long mS_per_S = 1000;
const long mS_per_min = 60 * mS_per_S;
const long min_per_hr = 60;
const long timeOff = 2 * mS_per_min + 5 * mS_per_S; // 2 minutes 5 seconds off 
const long timeOn = (3 * min_per_hr * mS_per_min) - timeOff; // almost 3 hours on

void setup() {
myservo.attach(9);
}

void loop() {
myservo.write(180);
delay(timeOn);
myservo.write(0);
delay(timeOff);
}

```

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [January 1, 2023, 12:31am UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/11 "2023-01-01T00:31:33Z")

</div>

What happened when you tried it?

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [January 1, 2023, 12:42am UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/12 "2023-01-01T00:42:09Z")

</div>

@anon94801460 , the three hours isn't elapsed yet. How would legobatman know?

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [January 1, 2023, 12:44am UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/13 "2023-01-01T00:44:12Z")

</div>

> [@legobatman\_321](#):
>
> need to finish this project soon

Might need to shorten the delays a bit.

---

<div class="post-metadata">

**Author:** ![legobatman\_321](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@legobatman\_321](https://forum.arduino.cc/u/legobatman_321)\
**Post date:** [January 1, 2023, 12:52am UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/14 "2023-01-01T00:52:56Z")

</div>

Nothing happened. I tried it an hour ago.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [January 1, 2023, 12:54am UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/15 "2023-01-01T00:54:24Z")

</div>

How are you powering the servo? Please post a focused, close up pic of your setup.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [January 1, 2023, 12:57am UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/16 "2023-01-01T00:57:06Z")

</div>

What happens when you run a simple test example from the servo library you used?

---

<div class="post-metadata">

**Author:** ![legobatman\_321](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@legobatman\_321](https://forum.arduino.cc/u/legobatman_321)\
**Post date:** [January 1, 2023, 1:11am UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/17 "2023-01-01T01:11:23Z")

</div>

![IMG_2617](https://europe1.discourse-cdn.com/arduino/original/4X/8/b/5/8b5a4417c17351c2953eb455ce57760bc15c1b71.jpeg)  
9v battery

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [January 1, 2023, 1:36am UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/18 "2023-01-01T01:36:17Z")

</div>

> [@legobatman\_321](#):
>
> 9v battery

== failure

Also the ground wire from your Arduino goes into an unconnected strip on the proto board. So, it's not connected to anything.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [January 1, 2023, 1:48am UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/19 "2023-01-01T01:48:05Z")

</div>

Use a 4xAA battery pack for good results, and connect battery negative to Arduino GND.

---

<div class="post-metadata">

**Author:** ![legobatman\_321](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@legobatman\_321](https://forum.arduino.cc/u/legobatman_321)\
**Post date:** [January 1, 2023, 3:20am UTC](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737/20 "2023-01-01T03:20:22Z")

</div>

I just fixed it. But I need the servo to spin faster. How do I do that?

[Next page](https://forum.arduino.cc/t/help-getting-a-servo-to-activate-on-time-for-a-specific-amount-of-time/1071737.md?page=2)
