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);
}