programming problem servo motor

I want to control the timing of the servo motor moving, by using the (millis) function every two hours, it moves to the right by 90 degrees after that stop ,and after another two hours it returns to the original place (-90 degres) ; and so it repeats for 30 days if that possible , please help
this is my code:
#include <Servo.h>

Servo myservo;
int pos = 0;

long previousMillis = 0;
long interval = 6000;
int mask=0;
void setup()
{
myservo.attach(9);

}

void loop() {

unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval){
previousMillis = currentMillis;

for(pos = 0; pos < 180; pos += 1)
{
myservo.write(pos);
delay(15);
delay(5000);
}

for(pos = 180; pos>=1; pos-=1)
{
myservo.write(pos);
delay(30);

}
}

}

Yes, that's possible.

if u know that , some help dude :confused:

Are you using a Uno or Mega or Due or STM32 or ESP32?

For now I'd not worry about the servos, they are the easy part.

You do not want to do anything for 120000 milliseconds, right? How precise do you want to be vs which MCU you are going to use.

Me, I'd use an esp32 and set up a hardware timer, count the 120000mS and trigger a freeRTOS task to run the servo motor in the proper direction.


What does 'some help' mean? You want I to write the code for you?

Time related variables should be unsigned long, that is what a call to millis() returns.

This is 6 seconds: long interval = 6000;

You wanted two hours? 1000mS = 1 second, do the math and use a bigger number; or check every minute (60,000) and keep track that 120 minutes passed. There are multiple ways to get there.

Hint 1. Do not not mix millis() timing and delay() timing in the same program unless you have a really good reason and know exactly what you're doing.

Hint 2. If you want the servo to move slowly don't use for loops instead swap Servo.h for VarSpeedServo.h and use the (non-blocking) speed parameter in the write(). If you don't want it to move slowly dump the for loop and use a single write().

Steve