Hello all. I am new to arduino. Looking forward to do some auto-self calibration for my program upon starting,or when there are at the beginning of the program starts. I am connecting a wiper motor shaft to a potentiometer, and read it's value to determine it's location.
My first question, how to do the self calibration upon start up, do I need to put the code inside the void Setup() function or it doesn't matter?
Secondly, how may I instruct the motor to chase it's home(center location) always, either when no input were taken or when a switch has just been released?
Here is my current method, the motor is moving and try to stop at center (I called it home). For the center(home) I maybe need more brilliant suggestion from you guys. I may draw a diagram if needed, please let me know.
int speedA = 95;
int positionPin = A0;
int switchA = 7; // pin 7 connected to a switch pin
int current_Location ;
int actual_Location;
//int new_init_Location;
int max_Location = 60;
int min_Location = 40;
int init_Location = 50;
int max_Limit = 5;
int min_Limit = 5;
int LPWM_A_State ;
int RPWM_A_State;
//define pin connected to motor
int LPWM_A = 6;
int RPWM_A = 5;
void setup() {
// define pinmode for motor pins
pinMode(LPWM_A,OUTPUT);
pinMode(RPWM_A,OUTPUT);
pinMode(switchA,INPUT);
}
void read()
{
actual_Location = analogRead(positionPin);
current_Location= map(actual_Location,0,1023,0,100);
// No button were pressed
if (digitalRead(switchA) == LOW)
{
analogWrite(LPWM_A,0);
analogWrite(RPWM_A,0);
}
// button A pressed
if (digitalRead(switchA) == HIGH)
{
analogWrite(LPWM_A,90); // move motor forward
analogWrite(RPWM_A,0);
LPWM_A_State = 1;
RPWM_A_State = 0;
}
// button has been released
if (digitalWrite(switchA) == LOW && LPWM_A_State ==1 && RPWM_A_State == 0 )
{
analogWrite(LPWM_A,0);
analogWrite(RPWM_A,90); //reverse the motor backward
LPWM_A_State = 0;
RPWM_A_State = 1;
}
// the motor is reversing after switch has been released, and need to stop at center. Looking for better idea, need improvements/suggestion.
if(LPWM_A_State == 0 && RPWM_A_State == 1 && current_Location == init_Location)
{
analogWrite(LPWM_A,0); // stop motor.
analogWrite(RPWM_A,0);
LPWM_A_State = 2;
RPWM_A_State = 2;
}
}
The code has more, TBH, I am also using a function for the motor move forward,reverse or stop.Current code is working, but without self-calibration. Thus, I'm looking forward for any ideas regarding self-calibration upon start up , and chasing back the motor to it's center location upon releasing switch/ no input received.
Thank you in advance.


