dc motor 180 degrees direction control using H-bridge (L293D)

Hi dear friends , i want to control DC motor using H bridge and potentiometer and make it works like the servo knob way , any help i will appreciate it , here i attached my code .

const int analogPin = A0;
int enableA = 11;
int MotorA1 = 10;
int MotorA2 = 9;
int pos = 0 ;
void setup() {
Serial.begin (9600);
//configure pin modes
pinMode (enableA, OUTPUT);
pinMode (MotorA1, OUTPUT);
pinMode (MotorA2, OUTPUT);
}
void loop() {
int analogValue = analogRead(analogPin);
digitalWrite (enableA, HIGH);
delay (1000);

if (analogValue = 0 ) {
for (pos = 0; pos <= 1024; pos += 1) {
delay(15);

digitalWrite (MotorA1, LOW);
digitalWrite (MotorA2, HIGH);
}

} else if (analogValue = 1024 ) {
for (pos = 0; pos <= 1024; pos -= 1) {

digitalWrite (MotorA1,HIGH);
digitalWrite (MotorA2,LOW);
delay(15);
}

}}

To use a DC motor as a servo you have to add position feedback like the servo has. Use the motor to drive a potentiometer. Use an analog input to sense the position of the motor. Use the PID library to control the motor based on the current position (the analog input) and your desired position.

Note: You seem to be using '=' (value assignment) where you mean to use '==' (equality comparison).

if (analogValue = 1024 ) {
    for (pos = 0; pos <= 1024; pos -= 1)

So many oopsies.

AWOL:

if (analogValue = 1024 ) {

for (pos = 0; pos <= 1024; pos -= 1)



So many oopsies.

come on!

All he wants is the loop code to execute once, All of the extra verbiage is to increase his word count, The grade it based on program length!

Chuck.

First, I'd recommend against building your own servo...

But if you are going to do it, I recommend doing it in 3 steps:

  1. Start with the positional feedback as suggested by John. That is, connect a pot to the motor and get to the point where your software can read the motor position when you rotate the un-powered motor by hand.

  2. Add some software where you can program-in an angle and then it drives the motor in the correct direction and stops when it gets to the programmed angle.

  3. Add the software to read the position of a "servo-knob" pot, and rotate the motor to the desired position.

If the motor is not geared-down, the motor will probably move too fast and overshoot the position, then bounce-around a bit until it settles-in.

chucktodd:
come on!

All he wants is the loop code to execute once, All of the extra verbiage is to increase his word count, The grade it based on program length!

Chuck.

OK, the Long Answer.if (analogValue = 1024 That’s an assignment.
If a comparison (==)was the intention, then any eight-bit AVR’s ADC returning eleven bits of result is cause for concern.

for (pos = 0; pos <= 1024; pos -= 1)

Starting from zero, while “pos” is less than or equal to 1024, count down by one per loop iteration.
Could take a while.

Oh yeah, and code tags

Caro amico, questa è una buona idea! defraggler alternative