Servo sweep on a schedule

Hello, I could use some assistance with my first Arduino project.

The goal of my project is to make a servo sweep back and forth once an hour for a halloween prop.

The rod at the end of the servo arm will have a bloody finger that presses a button.

I got the servo sweeping continously with the attached code but I am having trouble finding the best way sweep once at the interval desired.

Any assitance would be apprciated.



Sweep.txt (1.08 KB)

Forgot to mention I am using an Arduino Micro if that matters.

Have a look at the code in Several Things at a Time. It should not be too difficult to adapt it for what you want.

I presume it won’t matter if the Arduino does not measure an hour accurately. If it does you will need a Real Time Clock (RTC) module.


Thank you for your response. You are correct, I do not need to measure the hour accurately.

I took you code and murdered it by removing all references to LED’s and buttons.

I change the pin from 13 to 9. Verified the code then loaded. The servo sweeps back an forth continuoisely.
I was able to speed it up and slow it down by modifying int servoInterval setting but I do not see where I can delay or schedule the sweep to happen at a specified time interval. Can you point to the section I need to modify?

Thank again.

Here is the modified code.

// SeveralThingsAtTheSameTime.ino

// An expansion of the BlinkWithoutDelay concept to illustrate how a script
// can appear to do several things at the same time

// this sketch does the following
// it blinks the onboard LED (as in the blinkWithoutDelay sketch)
// it blinks two external LEDs (LedA and LedB) that are connected to pins 12 and 11.
// it turns another Led (buttonLed connected to pin 10) on or off whenever a button
// connected to pin 7 is pressed
// it sweeps a servo (connected to pin 5) back and forth at different speeds

// One leg of each LED should be connected to the relevant pin and the other leg should be connected to a
// resistor of 470 ohms or more and the other end of the resistor to the Arduino GND.
// If the LED doesn’t light its probably connected the wrong way round.

// On my Uno and Mega the “button” is just a piece of wire inserted into pin 7.
// Touching the end of the wire with a moist finger is sufficient to cause the switching action
// Of course a proper press-on-release-off button switch could also be used!

// The Arduino is not capable of supplying enough 5v power to operate a servo
// The servo should have it’s own power supply and the power supply Ground should
// be connected to the Arduino Ground.

// The sketch is written to illustrate a few different programming features.
// The use of many functions with short pieces of code.
// Short pieces of code are much easier to follow and debug
// The use of variables to record the state of something (e.g. onBoardLedState) as a means to
// enable the different functions to determine what to do.
// The use of millis() to manage the timing of activities
// The definition of all numbers used by the program at the top of the sketch where
// they can easily be found if they need to be changed


// ----------LIBRARIES--------------

#include <Servo.h>

// --------CONSTANTS (won’t change)---------------

const int servoPin = 9; // the pin number for the servo signal

const int servoMinDegrees = 20; // the limits to servo movement
const int servoMaxDegrees = 150;

//------------ VARIABLES (will change)---------------------

Servo myservo; // create servo object to control a servo

int servoPosition = 90; // the current angle of the servo - starting at 90.
int servoSlowInterval = 15; // millisecs between servo moves
int servoFastInterval = 15;
int servoInterval = servoSlowInterval; // initial millisecs between servo moves
int servoDegrees = 2; // amount servo moves at each step
// will be changed to negative value for movement in the other direction

unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousOnBoardLedMillis = 0; // will store last time the LED was updated
unsigned long previousLed_A_Millis = 0;
unsigned long previousLed_B_Millis = 0;

unsigned long previousButtonMillis = 0; // time when button press last checked

unsigned long previousServoMillis = 0; // the time when the servo was last moved


void setup() {

Serial.println(“Starting SeveralThingsAtTheSameTimeRev1.ino”); // so we know what sketch is running

myservo.write(servoPosition); // sets the initial position



void loop() {

// Notice that none of the action happens in loop() apart from reading millis()
// it just calls the functions that have the action code

currentMillis = millis(); // capture the latest value of millis()
// this is equivalent to noting the time from a clock
// use the same time for all LED flashes to keep them synchronized





void servoSweep() {

// this is similar to the servo sweep example except that it uses millis() rather than delay()

// nothing happens unless the interval has expired
// the value of currentMillis was set in loop()

if (currentMillis - previousServoMillis >= servoInterval) {
// its time for another move
previousServoMillis += servoInterval;

servoPosition = servoPosition + servoDegrees; // servoDegrees might be negative

if (servoPosition <= servoMinDegrees) {
// when the servo gets to its minimum position change the interval to change the speed
if (servoInterval == servoSlowInterval) {
servoInterval = servoFastInterval;
else {
servoInterval = servoSlowInterval;

if ((servoPosition >= servoMaxDegrees) || (servoPosition <= servoMinDegrees)) {
// if the servo is at either extreme change the sign of the degrees to make it move the other way
servoDegrees = - servoDegrees; // reverse direction
// and update the position to ensure it is within range
servoPosition = servoPosition + servoDegrees;

// make the servo move to the next position
// and record the time when the move happened




Please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

You need a variable to keep track of whether the servo is okToSweep and that gets set to true or false at the appropriate time.

In the servoSweep() function you can check that variable - something like this

void servoSweep() {
  if (okToSweep == false) {
        return; // i.e. do nothing
   // rest of the code

You can use the code from one of the LED functions, with changed times obviously, to change the value in okToSweep.