# DC motor Steering control through POT

i am making a robot and i am using DC Motor in the steering and reading the degree by POT i dont have any idea how to do it? any code please

Get a RC servo motor and control it using the PWM.

If you have access to it, by all means power the servo's motor via PWM and an appropriate buffer, but if you're controlling the servo itself, use PPM from the servo library.

I am using DC motor for steering and for Precise Positioning i am using POT secondly i cant use servo because its too expensive......

What is the physical set up? Do you mean you have two motors, and are doing differential drive, or do you have an axle (or stub axles) with a motor to steer it?

I am using DC motor for steering and for Precise Positioning i am using POT secondly i cant use servo because its too expensive......

You read the analog value from the pot and if it is less than a desired value, you run the motor in the desired direction. If the value is greater than the desired value, run the motor in the other direction. stop the motor when the desired value is reached. Servos are now very inexpensive in the US.

I am using the POT as analog val and My steering is attached with the POT.
i have to develop three states LEFT,RIGHT and Center,Each State will have its own Resistance but how to range them

It's pretty difficult if you want to do it right - look up PID.

Of course, you can try to simplify it - just take the value of the pot, subtract where you want to go from it, and then move the motor at a speed proportional to that. (that's a P loop in PID terms)

``````// Set this value by trial and error - if it doesn't get to where you want it to go, increase it; if it oscillates after getting where you want it to go, lower it; if both (or you can't find a happy medium) you'll need to look into using the I and D terms of PID
int Pterm; // 0 to 1000
int dest; // where you want the steering to go
int speed = (((long)pos - (long)dest) * (long)Pterm)/1000;

// Run your motor at the right speed.
``````

mkwired:
Get a RC servo motor and control it using the PWM.

Better yet get an RC servo and control it using the servo library that uses PPM control, not PWM control.

retrolefty:
control it using the servo library that uses PPM control, not PWM control.

If you mean Pulse Position Modulation, I think you’re using the wrong term. Standard RC servos use Pulse Width Modulation, usually a pulse between about 1.5 and 2.5ms repeating at 20ms (50 Hz).

PeterH:

retrolefty: control it using the servo library that uses PPM control, not PWM control.

If you mean Pulse Position Modulation, I think you're using the wrong term. Standard RC servos use Pulse Width Modulation, usually a pulse between about 1.5 and 2.5ms repeating at 20ms (50 Hz).

Yes, this use of terms has come up a lot in the past, ( http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1253149521/all ) but it's never resolved to everyones satisfaction. I use PPM to establish that the standard servo signal is of a R/C standard 1-2msec pulse width at a 40-50hz frequency, which the servo library can generate. PWM in context of the arduino (analogWrite() function) is a not a good fit at all for a standard servo. Even if you perform low level register control to lower the PWM frequency to 50Hz (if defaults to like 900hz) you will end up with very poor resolution control as you cannot utilize the full 8 bit pulse width control.

My point of posting was, if using a servo, use a servo library to drive it. Nothing more, nothing less.

Lefty

If you mean Pulse Position Modulation, I think you're using the wrong term. Standard RC servos use Pulse Width Modulation

Lefty is absolutely right. Digital R/C systems (used to be called "digiprop" for "digital proportional") have used PPM for at least the last 35 years. The difference is subtle, and you could use PWM to generate a PPM-compatible waveform (though you're advised not to) using the Arduino's eight bit PWM, which is at too high a frequency by default (a servo or ESC may not work at all with too high a frame-rate, or may draw excessive current), and has too low resolution. If you take a typical range of an R/C PPM pulse of 1 to 2ms, an eight bit resolution PWM generating the whole 20ms frame will give only about twelve graduations or positions.

I have Built a code for my motor PWM through POT can any body tell me whats the Mistake

``````const int apin =A0;
const int m1 = 9;
const int m2 = 11;
int aval = 0;
int fval = 0;
int cval;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop()
{
// map it to the range of the analog out:
fval = map(aval,0,0,0,1023);
// change the analog out value:
}
void check_input() {

if ( Serial.available()) {

switch(ch) {

case 'l':
Serial.println("wana go left");
if(fval <= 49){
Serial.println("i'm in left");}
else if(fval > 49)
{
analogWrite(m1,255);
analogWrite(m2,0);
delay(1000);
}
digitalWrite(13, HIGH);
break;
case 'c':
Serial.println("Center the Steering");
int a=constrain(fval,470,510);
if (a){
Serial.println("i am in the center");
}
else
{
analogWrite(m1,255);
}
else
{
analogWrite(m2,255);
}
//        //motor_run = false;

//motor_started = false;

//analogWrite(motor_cur_pin, 0);

//digitalWrite(13, LOW);
break;

case 'r':
//motor_cur_pin = MOTOR_LT_PIN;
if (fval>=975)
{
Serial.println("Direction = Right");
}
else
{
analogWrite(m1,255);
}
else
{
analogWrite(m2,255);
}
break;
//case 'r':
//motor_cur_pin = MOTOR_RT_PIN;
//Serial.println("Direction = RIGHT");
//break;

}

}

}
``````

well i have tried a simple code for this but its giving me errors why

``````const int apin =A0;
const int m1 = 9;
const int m2 = 11;
int aval = 0;
int fval = 0;
int cval;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop()
{
// map it to the range of the analog out:
fval = map(aval,0,0,0,1023);
// change the analog out value:
}
void check_input() {

if ( Serial.available()) {

switch(ch) {

case 'l':
Serial.println("wana go left");
if(fval <= 49){
Serial.println("i'm in left");}
else if(fval > 49)
{
analogWrite(m1,255);
analogWrite(m2,0);
delay(1000);
}
digitalWrite(13, HIGH);
break;
case 'c':
Serial.println("Center the Steering");
int a=constrain(fval,470,510);
if (a){
Serial.println("i am in the center");
}
else
{
analogWrite(m1,255);
}
else
{
analogWrite(m2,255);
}
//        //motor_run = false;

//motor_started = false;

//analogWrite(motor_cur_pin, 0);

//digitalWrite(13, LOW);
break;

case 'r':
//motor_cur_pin = MOTOR_LT_PIN;
if (fval>=975)
{
Serial.println("Direction = Right");
}
else
{
analogWrite(m1,255);
}
else
{
analogWrite(m2,255);
}
break;
//case 'r':
//motor_cur_pin = MOTOR_RT_PIN;
//Serial.println("Direction = RIGHT");
//break;

}

}

}
``````

http://arduino.cc/forum/index.php/topic,87167.0.html

but its giving me errors why

That would depend on what the errors are. You tell us.

``````  // map it to the range of the analog out:
fval = map(aval,0,0,0,1023);
``````

Hmm.

Reference: map(value, fromLow, fromHigh, toLow, toHigh)

Also, if you've gone to the bother of defining a function, like "check_input", it is probably a good idea to call it.

Cross-posting corrected.

@OP: DO NOT CROSS-POST.

It wastes time and makes people CROSS.

As to your error, an "if" statement has, by convention, only one "else" clause at most.

this is the following error i am observing on the Above mentioned Code

``````sketch_jan16a.cpp: In function 'void check_input()':
sketch_jan16a:47: error: expected `}' before 'else'
sketch_jan16a:47: error: expected `}' before 'else'
sketch_jan16a:58: error: break statement not within loop or switch
sketch_jan16a:60: error: case label ''r'' not within a switch statement
sketch_jan16a:70: error: 'else' without a previous 'if'
sketch_jan16a:74: error: break statement not within loop or switch
sketch_jan16a.cpp: At global scope:
sketch_jan16a:82: error: expected declaration before '}' token
``````