Servo rotation every 5 hours

I am a beginner, know somethings with leds, all kinds of sensors and displays but can not get the servo working.

I would like to have a servo (paralax) rotate half way round and back once every 4 hours. I get the servo to rotate but never close to the same start and end point and not get the 5 hours worked out. I hope someone can help me with that. The code cant be that hard i was thinking but I was wrong. I am using an arduino uno.

Thanks so much in advance.

Regards,

Arjan

Does the servo needs to take 4 hours to do a 180 or does it have to wait and then rotate?

And can you post the code you have now? (Please use the “</>” button for posting your code into…)

So 4 hours or 5 hours?

Either way the secret lies in the Blink Without Delay example code that came with your IDE. Study it until you understand how it works. You'll need to use the same type of concept.

The demo several things at a time is an extended example of BWoD

...R

#include <Servo.h>

Servo servo;
const int ServoPin = 3;

void setup() {
    servo.attach(ServoPin);
}

void loop() {
    servo.write(0);  // Move to zero degrees
    delay(4UL*60UL*60UL*1000UL);  // wait four hours
    servo.write(180);  // Move to 180 degrees
    delay(4UL*60UL*60UL*1000UL);  // wait four hours
}

arjan1970:
I am a beginner, know somethings with leds, all kinds of sensors and displays but can not get the servo working.

I would like to have a servo (paralax) rotate half way round and back once every 4 hours. I get the servo to rotate but never close to the same start and end point and not get the 5 hours worked out. I hope someone can help me with that.

Help with finding out whether you want a half rotation every 4 hours or every 5 hours?

Here is some non-blocking code for a servoTask() in your loop function (code not tested):

#include <Servo.h>

Servo servo;
const int ServoPin = 3;

void setup() {
    servo.attach(ServoPin);
}

#define MILLISPERHOUR 3600000L   // one hour in milliseconds
#define DURATION 5*MILLISPERHOUR // five hours in milliseconds
unsigned long cycle, lastCycleStart;
void servoTask()
{
  if (millis()-lastCycleStart>= DURATION) 
  {
    cycle++;  // count the cycle number
    lastCycleStart+= DURATION; // remember start time of current cycle
  }
  if (cycle%2==0) servo.write(0);  // even cycle number ==> servo.write(0);
  else servo.write(180);  // odd cycle number ==> servo.write(180);
}

void loop() {
  servoTask();
}

Please be aware that the servo position is highly dependent on the servo temperature. So the servo angle with servo.write(180); may be different when the servo is used at different temperatures.