2

I am trying to use map command which is used to power servo 180 degrees as the potentiometer moves...

in fact what i want to do is just when i adjust my potentiometer to zero; stop the dc motor then after turn the pot to 512 analog value turn the motor to the left (digitalWrite(I1, HIGH); digitalWrite(I2, LOW);)and then stop when it reach 512 then when adjust the pot back to zero turn the dc motor to the right (digitalWrite(I1, HIGH); digitalWrite(I2, LOW);) but the think is what i am trying to do is exactly what we can do with the servo motor in the knob http://www.arduino.cc/en/Tutorial/Knob

I'm using a normal DC motor and l293D IC but I want to control the DC as knob method like the 200 degrees.

Code

#define E1 11 // Enable Pin for motor 
#define I1 10 // Control pin a for motor 
#define I2 9 // Control pin b for motor 
const int analogPin = A0; 
int analogValue ; 
void setup() {
 pinMode(E1, OUTPUT);
 pinMode(I1, OUTPUT);
 pinMode(I2, OUTPUT);
}
void loop() {
 int analogValue = analogRead(analogPin);
 if(analogValue > 0 && analogValue < 512) {
 analogWrite(E1, 255); 
 digitalWrite(I1, HIGH);
 digitalWrite(I2, LOW);
 delay(100); 
} else 
 analogWrite(E1, 255); 
 digitalWrite(I1, LOW);
 digitalWrite(I2, HIGH);
 }
asked Apr 1, 2016 at 9:47
3
  • Any chance to have a circuit diagram? Commented Apr 1, 2016 at 10:03
  • The map function doesn't seem to be included in your second section of code. Did you want to adjust the 255 value to a new value relative to the "analogValue"? Commented Apr 1, 2016 at 10:44
  • "and then stop when it reach 512" is meaningless for a DC motor unless you have a position feedback device connected to it. Hobby type servos include an internal potentiometer for feedback. If you are building a position control with an ordinary DC motor, you will have to mechanical link something such as an encoder or potentiometer to its shaft and implement a position control loop. Commented Apr 3, 2016 at 3:06

1 Answer 1

1

It looks like you want to use the "analogValue" to control the direction and speed of your DC motor. If this is correct consider using this:

 //This method assumes: The center position of the pot is the motor's 0 speed point, 
 //(because this is an ideal speed to actually reverse the motor's direction).
 //A PWM control range of 0-255, and the ADC input range of 0-1023. 
 //First create a relative speed value from "analogValue", 
 //with the pot mid point at 0 speed, and pot high/low points being full speed. 
 int motorSpeed = 0; 
 if (analogValue < 512 ) 
 { 
 motorSpeed = map(analogValue, 0, 511, 255, 0); 
 } //Note that mapped range is reversed to give 0 speed at mid point. 
 else 
 { 
 motorSpeed = map(analogValue, 512, 1023, 0, 255); 
 }
 //then replace each analogWrite(E1, 255) with: 
 analogWrite(E1, speedValue); 
 //you should also change " if (analogValue > 2 && analogValue < 500) { " to: 
 if (analogValue < 512) { 
answered Apr 1, 2016 at 11:49
3
  • thanks for this kind answer, in fact what i want to do is just when i adjust my potentiometer to zero; stop the dc motor then after turn the pot to 512 analog value turn the motor to the left (digitalWrite(I1, HIGH); digitalWrite(I2, LOW);)and then stop when it reach 512 then when adjust the pot back to zero turn the dc motor to the right (digitalWrite(I1, HIGH); digitalWrite(I2, LOW);) but the think is what i am trying to do is exactly what we can do with the servo motor in the knob arduino.cc/en/Tutorial/Knob Commented Apr 2, 2016 at 18:17
  • youtube.com/watch?v=3mw6H3461Y0 Commented Apr 2, 2016 at 18:30
  • IT IS AN EXAMPLE Commented Apr 2, 2016 at 18:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.