DC motor control through micro switches

Im new in this field. I have Pololu High-Power 18V, 15A motor driver, Arduino uno microcontroler and DC worm gear Motor (12V) and two micro switches. Im building a prototype for tracking a solar panel. I would like to ask for a help regarding some sample code, that after tracking the panel with worm gear Motor it returns the panel in their initial position (horizontal). I have two Input PULLUP switches . So the idea is, motor start tracking and rotate 4 sec (defined with const int rotatiOnMillis = 4000) and is off for 2 sec (defined with const int motorBaseInterval = 2000).

So when (say) right-hand switch is pressed it runs the code to stop the motor and then to move the motor back to the left-hand HOME (Sleep) position. Here, in the HOME (Sleep) position the motor should stop and stay off for (let) say
15 sec (defined with const int motorSleepInterval = 15000). The problem of the code is that it stop the motor and bring back when end switch is press but it does not sleep for 15sec. And when its in HOME (Sleep ) position does not waje up and start running after those 15 sec. Any help?

// ----CONSTANTS (won't change)
#define motor_speed 750 //default motor speed @ 0.3rpm

const int motorDirection_Pin = 4; //Motor Direction pin 
const int motorSpeed_Pin = 5; //Motor PWN pin 
const int buttonEnd_Pin = 8; //End Button  pin 
const int buttonSleep_Pin = 9;  //Sleep Button  pin 
const int rotatiOnMillis = 4000; // number of millisecs for motor rotation
const int motorBaseInterval = 2000;  // number of millisecs between motor rotation
const int motorSleepInterval = 15000; // number of millisecs while motor is sleeping
 
//------- VARIABLES (will change)
byte buttonEnd_State = LOW;
byte buttonSleep_State = LOW;
byte motorState = HIGH;
unsigned long currentMillis;    // stores the value of millis() in each iteration of loop()
unsigned long previousMotorMillis;   // will store last time the motor was updated
unsigned long motorInterval = motorBaseInterval;
unsigned long motorOffMillis = motorSleepInterval;
volatile byte button_flag = 0;
volatile byte button_flag1 = 0;
volatile byte sleep_flag = 0;
//========

void setup() {

  Serial.begin(9600);
      // set the Motor and Buttons pins as output, INPUT_PULLUP:
  pinMode(motorDirection_Pin, OUTPUT);
  pinMode(motorSpeed_Pin, OUTPUT);
  pinMode(buttonSleep_Pin, INPUT_PULLUP);
  pinMode(buttonEnd_Pin, INPUT_PULLUP);
  
  }

//=======
void loop() {
  
   currentMillis = millis();
   checkButtons(); 
   execute_motor();
   end_position(); 
   sleep_position();
 }

//========  
  void checkButtons() {
  buttonEnd_State = digitalRead(buttonEnd_Pin);
  buttonSleep_State = digitalRead(buttonSleep_Pin);
}

  void execute_motor() {
    checkButtons();
    // nothing happens unless the interval has expired
    if (currentMillis - previousMotorMillis >= motorInterval) {
      //time is up, its time for another motor rotation
          previousMotorMillis += motorInterval;
          motorState = ! motorState;
          if (motorState == HIGH) {
            digitalWrite(motorDirection_Pin, LOW);  //set direction for hinge motor rotation
            analogWrite(motorSpeed_Pin, motor_speed);
            motorInterval = rotatiOnMillis;
            Serial.println("motor rotation");
          }
          else {
              //nothing happens unless the interval has expired, still motor is not rotating
              motorInterval = motorBaseInterval;
              analogWrite(motorSpeed_Pin, 0);  //set 0 speed for hinge motor
              Serial.println("motor stop, speed is 0");
          }
          digitalWrite(motorDirection_Pin, motorState);
          
      }
     }   
    
   void end_position() {
    
     checkButtons();
     /* Check if End Button is pressed, if yes, stop the motor*/
     if (buttonEnd_State == HIGH) {
    
       analogWrite(motorSpeed_Pin, 0);  //set 0 speed for hinge motor
       Serial.println("end position");
             /* Check if Sleep Button is pressed, run the motor to opposite direction till Sleep Button is pressed */
            while (buttonSleep_State == LOW) {
                  digitalWrite(motorDirection_Pin, LOW);  //set direction for hinge motor
                  analogWrite(motorSpeed_Pin, motor_speed);
                  button_flag=1;
                  checkButtons();
                  Serial.println("end position, going home");
                                             }
        }
   }
   void sleep_position(){
    /* Check if Sleep Button is pressed, if its pressed stop the motor and set the sleep interval */
      if (buttonSleep_State == HIGH && button_flag==1) {
         analogWrite(motorSpeed_Pin, 0);
         //button_flag1=1;
         Serial.println("stop, you are in sleep position now");
         motorInterval = motorSleepInterval;
         Serial.println("sleepinngg bzzzz");
       }
    }

So when (say) right-hand switch is pressed

Would the right hand switch be the one attached to the pin called buttonSleep_Pin or the one attached to the pin called buttonEnd_Pin? Use names in your code that reflect how you actually think of the switches, or use words in your description that match the code.

Guessing games are for game-show participants.

Sleep and home are completely different concepts.

buttonEnd_Pin correspond to the (say) left hand switch (means left hand end Position) and buttonSleep_Pin correspond to the (say) wright hand switch and its say(Sleep Position). So what the program should do is the motor is off and on (off for 2 sec and on for 4 sec) till the buttonEnd_Pin (of the left hand switch) is HIGH (means is pressed). That should stop the motor and bring it back to the right hand end position( means to the position when right hand button switch - buttonSleep_Pin is HIGH). Here the motor should stop for 15 sec and than start again the off and on mode.

Its not important the side of the switch . Any of the two switches can be right hand end switch and other left-hand-end switch. Important is that when one of the switches when press means stop the motor and bring it back to the other end , and the other end switch is for stop the motor for a period of ( say) 15 sec and then again the normal on and off mode.

Hope is more clear now, Any help?

Its not important the side of the switch

It is when the name in the code does not match the name you are using. If one switch is the home limit switch and the other is the end limit switch, and one is located on the left and one on the right, and you call then Sam and Mary in the code, and then talk about Eric and Nancy, we have no idea what you are talking about.

In loop(), you call checkButtons(). Then, you call execute_motor(), in which you call checkButtons(). Why?

You need to be consistent in how you name functions. Either camel case, as checkButtons() uses, or underscores, as execute_motor() uses. Do not use both.

So what the program should do is the motor is off and on (off for 2 sec and on for 4 sec) till the buttonEnd_Pin (of the left hand switch) is HIGH (means is pressed).

I don't understand this. Does the motor running cause something to move? Does that moving item press the switches?

If that is the case, let's suppose that when the Arduino starts up, the something is in the home position - the home limit switch is pressed. What, exactly do you want to have happen? The motor should NOT "off and on". What, exactly should it do?

It sounds vaguely like the motor should be turned on, should run until the end limit switch is pressed, and should then stop for some period of time. But only vaguely...

So, to be clear. The system is a part of a solar panel tracking system. And this is the code only for the rotation. So the idea is the panel start rotation in the morning and rotate let we say every 10 min for let we say 2 sec. ( So means, I have defined motorBaseInterval which is the interval between rotation and should be 10 min long when motor stop. then and I have rotatiOnMillis when the motor runs and should be let we say 2 sec long). So this happen till the end limit switch is pressed.

So when left end limit switch is pressed (so normally this happen evening time) the motor should stop and than bring the panel to the Home (Sleep) position (mean motor runs in opposite direction till the right hand button switch - buttonSleep_Pin is HIGH). Here in the Home (Sleep) Position the panel should stay over the night , so means motor stop for time defined with motorSleepInterval. And in the morning start runing again the same procedure.

Hope is clear now

I made modification in the code but still not getting the wright results, as when I make the intervals very big, (for example const int motorBaseInterval = 122000; const int motorSleepInterval = 49320;) the motor never start rotating. I think this is the issue with millis. So by small intervals of motorBaseInterval = 30000; const int motorSleepInterval = 25000;) till 33 sec its works fine. What is the problem

Here the modified code

// ----CONSTANTS (won't change)
#define motor_speed 750 //default motor speed @ 0.3rpm

const int motorDirection_Pin = 4; //Motor Direction pin
const int motorSpeed_Pin = 5; //Motor PWN pin
const int buttonEnd_Pin = 8; //End Button pin
const int buttonSleep_Pin = 9; //Sleep Button pin
const int rotatiOnMillis = 2500; // number of millisecs for motor rotation
const int motorBaseInterval = 122000; // number of millisecs between motor rotation
const int motorSleepInterval = 49320; // number of millisecs while motor is sleeping

//------- VARIABLES (will change)
byte buttonEnd_State = LOW;
byte buttonSleep_State = LOW;
byte motorState = HIGH;
unsigned long currentMillis; // stores the value of millis() in each iteration of loop()
unsigned long previousMotorMillis; // will store last time the motor was updated
unsigned long motorInterval = motorBaseInterval;
unsigned long motorOffMillis = motorSleepInterval;
volatile byte button_flag = 0;
volatile byte button_flag1 = 0;
volatile byte sleep_flag = 0;
//========

void setup() {

Serial.begin(9600);
// set the Motor and Buttons pins as output, INPUT_PULLUP:
pinMode(motorDirection_Pin, OUTPUT);
pinMode(motorSpeed_Pin, OUTPUT);
pinMode(buttonSleep_Pin, INPUT_PULLUP);
pinMode(buttonEnd_Pin, INPUT_PULLUP);

}

//=======
void loop() {

currentMillis = millis();
checkButtons();
executeMotor();
endPosition();
sleepPosition();
}

//========
void checkButtons() {
buttonEnd_State = digitalRead(buttonEnd_Pin);
buttonSleep_State = digitalRead(buttonSleep_Pin);
}

void executeMotor() {
// nothing happens unless the interval has expired
if (currentMillis - previousMotorMillis > motorInterval) {
//time is up, its time for another motor rotation
//previousMotorMillis += motorInterval;
previousMotorMillis = currentMillis;
motorState = ! motorState;
if (motorState == HIGH) {
digitalWrite(motorDirection_Pin, LOW); //set direction for hinge motor rotation
analogWrite(motorSpeed_Pin, motor_speed);
motorInterval = rotatiOnMillis;
Serial.println("MOTOR ROTATIONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN");
}
else {
//nothing happens unless the interval has expired, still motor is not rotating
motorInterval = motorBaseInterval;
analogWrite(motorSpeed_Pin, 0); //set 0 speed for hinge motor
Serial.println("motor stop, speed is 0000000000000");
}
digitalWrite(motorDirection_Pin, motorState);

}
}

void endPosition() {

checkButtons();
/* Check if End Button is pressed, if yes, stop the motor*/
if (buttonEnd_State == HIGH) {

analogWrite(motorSpeed_Pin, 0); //set 0 speed for hinge motor
Serial.println("end position");
/* Check if Sleep Button is pressed, run the motor to opposite direction till Sleep Button is pressed */
while (buttonSleep_State == LOW) {
digitalWrite(motorDirection_Pin, LOW); //set direction for hinge motor
analogWrite(motorSpeed_Pin, motor_speed);
button_flag=1;
checkButtons();
Serial.println("end position, going home");
}
}
}

void sleepPosition(){
/* Check if Sleep Button is pressed, if its pressed stop the motor and set the sleep interval */
if (buttonSleep_State == HIGH ) {
analogWrite(motorSpeed_Pin, 0);
// previousMotorMillis = currentMillis;
Serial.println("sleepinngg bzzzz");
motorInterval = motorSleepInterval;
}
}

Hi Actually I start the thread under DC motor control through micro switches - Programming Questions - Arduino Forum but I think its ok to open a new thread as the problem is with function millis.

So I traying the ma ke a code for solar planel tracking but only for rotation of the panels. So the idea is the panel start rotation in the morning and rotate let we say every 10 min for let we say 2 sec. ( So means, I have defined motorBaseInterval which is the interval between rotation and should be 10 min long while motor stop. Then and I have rotatiOnMillis interval while the motor runs and should be let we say 2 sec long). So this happen till the end limit switch is pressed.

So when left end limit switch is pressed (so normally this happen evening time) the motor should stop and than bring the panel to the Home (Sleep) position (mean motor runs in opposite direction till the right hand button switch - buttonSleep_Pin is HIGH). Here in the Home (Sleep) Position the panel should stay over the night , so means motor stop for time defined with motorSleepInterval. And in the morning start runing again the same procedure.

The problem is in the intervals. As when I make the intervals very big, (for example const int motorBaseInterval = 122000; const int motorSleepInterval = 49320;) the motor never start rotating. I think this is the issue with millis. So by small intervals of motorBaseInterval = 30000; const int motorSleepInterval = 25000;) till 33 sec its works fine. What is the problem

Here the code

// ----CONSTANTS (won't change)
#define motor_speed 750 //default motor speed @ 0.3rpm

const int motorDirection_Pin = 4; //Motor Direction pin
const int motorSpeed_Pin = 5; //Motor PWN pin
const int buttonEnd_Pin = 8; //End Button pin
const int buttonSleep_Pin = 9; //Sleep Button pin
const int rotatiOnMillis = 2500; // number of millisecs for motor rotation
const int motorBaseInterval = 122000; // number of millisecs between motor rotation
const int motorSleepInterval = 49320; // number of millisecs while motor is sleeping

//------- VARIABLES (will change)
byte buttonEnd_State = LOW;
byte buttonSleep_State = LOW;
byte motorState = HIGH;
unsigned long currentMillis; // stores the value of millis() in each iteration of loop()
unsigned long previousMotorMillis; // will store last time the motor was updated
unsigned long motorInterval = motorBaseInterval;
unsigned long motorOffMillis = motorSleepInterval;
volatile byte button_flag = 0;
volatile byte button_flag1 = 0;
volatile byte sleep_flag = 0;
//========

void setup() {

      Serial.begin(9600);
      // set the Motor and Buttons pins as output, INPUT_PULLUP:
      pinMode(motorDirection_Pin, OUTPUT);
      pinMode(motorSpeed_Pin, OUTPUT);
      pinMode(buttonSleep_Pin, INPUT_PULLUP);
      pinMode(buttonEnd_Pin, INPUT_PULLUP);

        }

//=======
void loop() {

        currentMillis = millis();
        checkButtons();
        executeMotor();
        endPosition();
        sleepPosition();
}

//========
   void checkButtons() {
        buttonEnd_State = digitalRead(buttonEnd_Pin);
        buttonSleep_State = digitalRead(buttonSleep_Pin);
      }

        void executeMotor() {
                // nothing happens unless the interval has expired
                if (currentMillis - previousMotorMillis > motorInterval) {
                //time is up, its time for another motor rotation
                //previousMotorMillis += motorInterval;
                previousMotorMillis = currentMillis; 
                motorState = ! motorState;
                    if (motorState == HIGH) {
                    digitalWrite(motorDirection_Pin, LOW); //set direction for hinge motor rotation
                    analogWrite(motorSpeed_Pin, motor_speed);
                    motorInterval = rotatiOnMillis;
                    Serial.println("MOTOR ROTATIONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN");
                    }
                        else {
                        //nothing happens unless the interval has expired, still motor is not rotating
                        motorInterval = motorBaseInterval;
                        analogWrite(motorSpeed_Pin, 0); //set 0 speed for hinge motor
                        Serial.println("motor stop, speed is 0000000000000");
                        }
                digitalWrite(motorDirection_Pin, motorState);
        
        }
        } 

    void endPosition() {

      checkButtons();
      /* Check if End Button is pressed, if yes, stop the motor*/
      if (buttonEnd_State == HIGH) {
      
            analogWrite(motorSpeed_Pin, 0); //set 0 speed for hinge motor
            Serial.println("end position");
           /* Check if Sleep Button is pressed, run the motor to opposite direction till Sleep Button is pressed */
                while (buttonSleep_State == LOW) {
                digitalWrite(motorDirection_Pin, LOW); //set direction for hinge motor
                analogWrite(motorSpeed_Pin, motor_speed);
                button_flag=1;
                checkButtons();
                Serial.println("end position, going home");
          }
      }
 }


   void sleepPosition(){
          /* Check if Sleep Button is pressed, if its pressed stop the motor and set the sleep interval */
                if (buttonSleep_State == HIGH ) {
                      analogWrite(motorSpeed_Pin, 0);
                      // previousMotorMillis = currentMillis; 
                      Serial.println("sleepinngg bzzzz");
                      motorInterval = motorSleepInterval;
                                                                }
           }

There is nothing wrong with millis(). However there is something wrong here:

const int motorBaseInterval = 122000; // number of millisecs between motor rotation

The maximum an int can hold is 32767. Do you see the problem?

Ditto for:

const int motorSleepInterval = 49320; // number of millisecs while motor is sleeping

Please use code tags when posting.

I'm going to merge these threads, you asked the same question in both of them. Please don't do that.

ok. Sorry. So what should I do?

Change int to unsigned long.

volatile byte button_flag = 0;
volatile byte button_flag1 = 0;
volatile byte sleep_flag = 0

Why are these declared volatile?

ok I change to byte.

byte button_flag = 0;
byte button_flag1 = 0;
byte sleep_flag = 0;

But its still not working. Any help?

so now the modified code is

// ----CONSTANTS (won't change)
#define motor_speed 750 //default motor speed @ 0.3rpm

const int motorDirection_Pin = 4; //Motor Direction pin
const int motorSpeed_Pin = 5; //Motor PWN pin
const int buttonEnd_Pin = 8; //End Button pin
const int buttonSleep_Pin = 9; //Sleep Button pin
const unsigned long rotatiOnMillis = 2000; // number of millisecs for motor rotation
const unsigned long motorBaseInterval = 4000; // number of millisecs between motor rotation
const unsigned long motorSleepInterval =15000; // number of millisecs while motor is sleeping

//------- VARIABLES (will change)
byte buttonEnd_State = LOW;
byte buttonSleep_State = LOW;
byte motorState = LOW;
unsigned long currentMillis; // stores the value of millis() in each iteration of loop()
unsigned long currentMillis1; // stores the value of millis() in each iteration of loop()
unsigned long previousMotorMillis=0; // will store last time the motor was updated
unsigned long previousMotorMillis1=0; // will store last time the motor was updated
unsigned long motorInterval = motorBaseInterval;
unsigned long motorOffMillis = motorSleepInterval;
byte button_flag = 0;
byte button_flag1 = 0;
byte sleep_flag = 0;
//========

void setup() {

Serial.begin(9600);
// set the Motor and Buttons pins as output, INPUT_PULLUP:
pinMode(motorDirection_Pin, OUTPUT);
pinMode(motorSpeed_Pin, OUTPUT);
pinMode(buttonSleep_Pin, INPUT_PULLUP);
pinMode(buttonEnd_Pin, INPUT_PULLUP);

}

//=======
void loop() {

currentMillis = millis();
//currentMillis1 = millis();
checkButtons();
execute_motor();

end_position();
delay (200);
sleep_position();
}

//========
      void checkButtons() {
            buttonEnd_State = digitalRead(buttonEnd_Pin);
            buttonSleep_State = digitalRead(buttonSleep_Pin);
          }

        void execute_motor() {
                //checkButtons();
                // nothing happens unless the interval has expired
                if ((unsigned long)(currentMillis - previousMotorMillis) >= motorInterval) {
                //time is up, its time for another motor rotation
                //previousMotorMillis += motorInterval;
                    previousMotorMillis = currentMillis; 
                    motorState = ! motorState;
                    if (motorState == HIGH) {
                    digitalWrite(motorDirection_Pin, LOW); //set direction for hinge motor rotation
                    analogWrite(motorSpeed_Pin, motor_speed);
                    motorInterval = rotatiOnMillis;
                    //previousMotorMillis = currentMillis;
                    Serial.println("MOTOR ROTATIONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN");
                    }
                        else {
                        //nothing happens unless the interval has expired, still motor is not rotating
                        motorInterval = motorBaseInterval;
                        analogWrite(motorSpeed_Pin, 0); //set 0 speed for hinge motor
                        Serial.println("motor stop, speed is 0000000000000");
                        }
                digitalWrite(motorDirection_Pin, motorState);
        
        }
        } 

    void end_position() {

      checkButtons();
      /* Check if End Button is pressed, if yes, stop the motor*/
      if (buttonEnd_State == HIGH) {
      
            analogWrite(motorSpeed_Pin, 0); //set 0 speed for hinge motor
            Serial.println("end position");
           /* Check if Sleep Button is pressed, run the motor to opposite direction till Sleep Button is pressed */
                while (buttonSleep_State == LOW) {
                digitalWrite(motorDirection_Pin, LOW); //set direction for hinge motor
                analogWrite(motorSpeed_Pin, motor_speed);
                button_flag=1;
                checkButtons();
                Serial.println("end position, going home");
          }
      }
 }


   void sleep_position(){
    currentMillis1 = millis();
          /* Check if Sleep Button is pressed, if its pressed stop the motor and set the sleep interval */
                if (buttonSleep_State == HIGH && button_flag1==0) {

                  if ((unsigned long)(currentMillis1 - previousMotorMillis1) >= motorOffMillis) {
                  //if (currentMillis1 - previousMotorMillis1 >= motorOffMillis) {

                     //previousMotorMillis1 = currentMillis1; 
                     digitalWrite(motorDirection_Pin, LOW); //set direction for hinge motor rotation
                     analogWrite(motorSpeed_Pin, motor_speed);
                     motorOffMillis = rotatiOnMillis;
                     //previousMotorMillis = currentMillis;
                     Serial.println("wake uppppppppppppppppppppppppppppp ");
                     button_flag1=1;
                                                                              }
                   
                    else {
                      analogWrite(motorSpeed_Pin, 0);
                     // previousMotorMillis1 = currentMillis1; 
                      Serial.println("sleepinngg bzzzz");
                      motorOffMillis = motorSleepInterval;
                           }
                                  //previousMotorMillis = currentMillis;
                                  checkButtons();
                                  }
      }
const int motorDirection_Pin = 4; //Motor Direction pin
const int motorSpeed_Pin = 5; //Motor PWN pin
const int buttonEnd_Pin = 8; //End Button pin
const int buttonSleep_Pin = 9; //Sleep Button pin

These are still ints because you got the top secret Arduino with 20,000 pins?

      void checkButtons() {
            buttonEnd_State = digitalRead(buttonEnd_Pin);
            buttonSleep_State = digitalRead(buttonSleep_Pin);
          }

That code doesn't check anything. It reads the state of some pins.

                    Serial.println("MOTOR ROTATIONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN");

Does that reallllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllly convey more information?

                        Serial.println("motor stop, speed is 0000000000000");

That's somehow different from 0?

The code, poorly indented as it is, does something. You have, again, not bothered to say what it actually does.

ok. I clean the code this the clean code

// ----CONSTANTS (won't change)
#define motor_speed 750 //default motor speed @ 0.3rpm

const unsigned long  rotatiOnMillis = 2000; // number of millisecs for motor rotation
const unsigned long  motorBaseInterval = 4000; // number of millisecs between motor rotation
const unsigned long  motorSleepInterval = 10000; // number of millisecs while motor is sleeping

//------- VARIABLES (will change)
byte buttonEnd_State;
byte buttonSleep_State;
byte motorState = LOW;
unsigned long currentMillis; // stores the value of millis() in each iteration of loop()
unsigned long previousMotorMillis=0; // will store last time the motor was updated
unsigned long motorInterval = motorBaseInterval;
unsigned long motorOffMillis = motorSleepInterval;
//========

void setup() {

      Serial.begin(9600);
      // set the Motor and Buttons pins as output, INPUT_PULLUP:
      pinMode(4, OUTPUT);
      pinMode(5, OUTPUT);
      pinMode(9, INPUT_PULLUP);
      pinMode(8, INPUT_PULLUP);

        }

//=======
void loop() {

        byte buttonEnd_State = digitalRead(8);
        byte buttonSleep_State = digitalRead(9);

        currentMillis = millis();
        executeMotor();
        endPosition();
        delay (100);
        sleepPosition();
}


        void executeMotor() {
                // nothing happens unless the interval has expired
                 Serial.print("time difference: ");                     // Print string label
                 Serial.println(currentMillis - previousMotorMillis);   // Print value and new line character
                 delay (500);
                 Serial.print("motorInterval: ");
                 Serial.println(motorInterval);
                 delay (500);
                if ((unsigned long)(currentMillis - previousMotorMillis) >= motorInterval) {
                //time is up, its time for another motor rotation
                   previousMotorMillis = currentMillis; 
                   motorState = ! motorState;
                    if (motorState == HIGH) {
                    digitalWrite(4, LOW); //set direction for hinge motor rotation
                    analogWrite(5, motor_speed);
              
                    motorInterval = rotatiOnMillis;
                
                    Serial.println("MOTOR ROTATION");
                    }
                        else {
                        //nothing happens unless the interval has expired, still motor is not rotating
                        motorInterval = motorBaseInterval;
                        analogWrite(5, 0); //set 0 speed for hinge motor
                        Serial.println("motor stop, speed is o");
                        }
                digitalWrite(4, motorState);

        }
        } 


    void endPosition() {
      buttonEnd_State = digitalRead(8);
      buttonSleep_State = digitalRead(9);
      /* Check if End Button is pressed, if yes, stop the motor*/
      if (buttonEnd_State == HIGH) {
      
            analogWrite(5, 0); //set 0 speed for hinge motor
            Serial.println("end position");
           /* Check if Sleep Button is pressed, run the motor to opposite direction till Sleep Button is pressed */
                while (buttonSleep_State == LOW) {
                digitalWrite(4, LOW); //set direction for hinge motor
                analogWrite(5, motor_speed);
                buttonSleep_State = digitalRead(8);
                buttonSleep_State = digitalRead(9);
                Serial.println("end position, going home");
          }
      }
 }


   void sleepPosition(){
          /* Check if Sleep Button is pressed, if its pressed stop the motor and set the sleep interval */
                if (buttonSleep_State == HIGH ) {
                      analogWrite(5, 0);
                      Serial.println("sleepinngg bzzzz");
                      motorInterval = motorSleepInterval;
                      Serial.print("motorInterval Sleeping: ");
                      Serial.println(motorInterval);
                      
                     
                                                                }
           }

I got the output WHEN the Sleep Button pressed means HIGH.

motorInterval Sleeping: 10000
time difference: 4326
motorInterval: 10000
sleepinngg bzzzz
motorInterval Sleeping: 10000
time difference: 5927
motorInterval: 10000
sleepinngg bzzzz
motorInterval Sleeping: 10000
time difference: 7529
motorInterval: 10000
sleepinngg bzzzz
motorInterval Sleeping: 10000
time difference: 9130
motorInterval: 10000
sleepinngg bzzzz
motorInterval Sleeping: 10000
time difference: 10732
motorInterval: 10000
motor stop, speed is 0

sleepinngg bzzzz
motorInterval Sleeping: 10000
time difference: 1601
motorInterval: 10000
sleepinngg bzzzz
motorInterval Sleeping: 10000
time difference: 3203
motorInterval: 10000
sleepinngg bzzzz
motorInterval Sleeping: 10000
time difference: 4804
motorInterval: 10000
sleepinngg bzzzz
motorInterval Sleeping: 10000
time difference: 6406
motorInterval: 10000
sleepinngg bzzzz
motorInterval Sleeping: 10000
time difference: 8008
motorInterval: 10000
sleepinngg bzzzz
motorInterval Sleeping: 10000
time difference: 9609
motorInterval: 10000
sleepinngg bzzzz
motorInterval Sleeping: 10000
time difference: 11211
motorInterval: 10000
MOTOR ROTATION

sleepinngg bzzzz
motorInterval Sleeping: 10000
time difference: 1601
motorInterval: 10000
time difference: 2702
motorInterval: 10000
time difference: 3803
motorInterval: 10000
time difference: 4904
motorInterval: 10000
time difference: 6005
motorInterval: 10000
time difference: 7106
motorInterval: 10000
time difference: 8207
motorInterval: 10000
time difference: 9308
motorInterval: 10000
time difference: 10409
motorInterval: 10000
motor stop, speed is 0
time difference: 1100
motorInterval: 4000

The problem is in the motor interval. WHEN the Sleep Button is pressed which is in the void sleepPosition() with if (buttonSleep_State == HIGH ) the motor should not runs means analogWrite(5, 0); for 10 sec ( defined with motorInterval = motorSleepInterval; ). And after that (after 10 sec) should do the mode which is in void executeMotor() (means runs for two sec ( with this command defined

digitalWrite(4, LOW); //set direction for hinge motor rotation
analogWrite(5, motor_speed);
motorInterval = rotatiOnMillis;

) and stop for 4 sec which is defined with

motorInterval = motorBaseInterval;
analogWrite(5, 0); //set 0 speed for hinge motor

is it ok now?