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

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.

#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); 
}

Use millis in place of delays.

Read thru these examples:

digitalWrite(servoPin, HIGH);
:thinking:
Use servoWrite().

1 Like

Use this

(Hint: it comes with examples)

1 Like

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

#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);
}
1 Like

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

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

Thank you for the math. It helped a lot!

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.

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

#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);
}

What happened when you tried it?

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

Might need to shorten the delays a bit.

Nothing happened. I tried it an hour ago.

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

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


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.

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

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