I want to control DC motor with a TA7291P Driver. I had connected DC motor and Potentiometer such that one complete rotation of DC motor shaft will rotate potentiometer shaft by one complete rotation.
I have written the code so that whatever position the dc shaft will be in it will it should come back to zero position. But I am making some mistake. I am posting my code alongwith. Kindly, suggest me where I am going wrong.
Any help will be appreciated.
Best regards
The code:
const int numReadings = 20;
//pot Variable
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A0;
const int pot_signal = 0;
//servo Variable
const int motor_Pin1 = 7;
const int motor_Pin2 = 8;
int angle_turned;
int data,j=0;
void setup()
{
Serial.begin(9600);
// initialize all pot the readings to 0:
memset(readings, 0, sizeof(int)*numReadings);
//to bring dc shaft to 0 position
initialise();
pinMode(motor_Pin1, OUTPUT);
pinMode(motor_Pin2, OUTPUT);
}
void loop()
{
}
int pot_read(){
for (readIndex = 0; readIndex < numReadings; readIndex++) {
total = total - readings[readIndex]; // subtract the last reading:
readings[readIndex] = analogRead(inputPin); // read from the sensor:
total = total + readings[readIndex]; // add the reading to the total:
}
average = total / numReadings;
Serial.println(average);
return(average);
}
void initialise(){
int pot_val= pot_read();
Serial.println(pot_val,DEC);
while (pot_val =<1023 || pot_val>=1){ //check position of POT
if (pot_val>512){
cw();
}
else
{
ccw();
}
pot_val= pot_read();
}
Brake();
}
void cw() //rotate clockwise
{
digitalWrite(motor_Pin1, HIGH);
digitalWrite(motor_Pin2, LOW);
Serial.println("motor turns left now");
}
void ccw() //rotate counter clockwise
{
digitalWrite(motor_Pin1, LOW);
digitalWrite(motor_Pin2, HIGH);
Serial.println("motor turns right now");
}
void Brake() //Stop turning
{
digitalWrite(motor_Pin1, LOW);
digitalWrite(motor_Pin2, LOW);
Serial.println("motor stopped turning");
}

