Stopping a DC motor

Hello everyone,

I am working on a project that controls the position of a car's side mirror, and I want to measure the angles (up, down, left, right) that my mirror makes using different delays, or after a certain amount of time for example, because I need the angle that each delay makes so I can do my calculations.

The problem is that the motor of the mirror does not stop after the delay even with the use of a switch. I need it to move to a certain angle according to the delay then stop so I can measure it. So is there a method like millis() or flags that could be of use? I'm relatively new to this so I would really appreciate the help.

Initial code idea

#define SWT 2
#define MOTEN1 11
#define MOTEN2 3
#define IN1 8 //right direction
#define IN2 10 // left direction
#define IN3 6 //Up
#define IN4 7 //down

void setup()
{
    pinMode(IN1,OUTPUT);
    pinMode(IN2, OUTPUT);
    pinMode(MOTEN1, OUTPUT);
    pinMode(MOTEN2, OUTPUT);
    pinMode(SWT, INPUT);
    pinMode(IN3,OUTPUT);
    pinMode(IN4, OUTPUT);
    Serial.begin(9600);
}

  
void loop()
  
{    // to get the right angle
           
    boolean swVal=digitalRead(SWT);

    if (swVal==HIGH)
  
    {      digitalWrite(IN1,LOW);
           digitalWrite(IN2,HIGH);
           analogWrite(MOTEN2,200);
           digitalWrite(IN3,LOW);
           digitalWrite(IN4,LOW);
           analogWrite(MOTEN1,0);
           Serial.println("Mirror move to right");
    }
      
      else 
      {
           digitalWrite(IN1,LOW);
           digitalWrite(IN2,LOW);
           analogWrite(MOTEN2,0);
           digitalWrite(IN3,LOW);
           digitalWrite(IN4,LOW);
           analogWrite(MOTEN1,0);
           Serial.println("stop mirror");
        
      }
  
       delay(1000);
 
}

Never gonna happen.
You probably meant swVal

How are you powering and controlling the motors ?

Perhaps more importantly provide a link to the mortors used AND how they connect to the mirrors.
EG a gear drive with a PM motor will not hold its position. You need a worm drive or screw mechanism to do that.

yes you’re right I meant to put swVal

I use a 12Volts power supply

This is all doable if you can identify a base line for each angle. Otherwise you will not have ANY reference to measure against. Also , you will need to begin by setting each motor to a starting or zero point. Only then can you consider knowing how far a motor moves the mirror.
Consider also the movement may not be linear and that relates to the mechanical arrangement connecting the motor and the mirror.

its a 2016 corolla full option side mirror couldn’t find detailed specifications

I don't fully agree. It depends of the motor axle -> outgoing axle ratio. A high ratio will be quite safe as I feel.

How do You intend to do that? By using the controller or conventional scales, rollers, angle devices?

Did you try (test) this idea? Then show us how you did your motor testing. I don't believe that the motor should fail to stop. My mirrors certainly stop as soon as I release the control button. It seems to me, it does that by just turning off the motor, not any special tricks.

Do you have a 10k pulldown resistor from pin 2 to GND?