DC Motor and Potentiometer positioning

hey gays i want to make my dc motor to run with a Precision potentiometer. 10 gears left and right when the max or min position its achieved and from reading the serial the motor should position it self
have written some simple code but don't now how to do it further (didn't make this yet) so heave some understanding and if your could help me further i did it appreciated

int Enable_A=13;
int Enable_B=8;
int Input_1=12;
int Input_2=11;
int Input_3=10;
int Input_4=9;
int Poti=A0;
int val;

void setup()
{
	Serial.begin(115200);
	Serial.println("REEDY");
	pinMode(Enable_A,OUTPUT);
	pinMode(Enable_B,OUTPUT);
	pinMode(Input_1,OUTPUT);
	pinMode(Input_2,OUTPUT);
	pinMode(Input_3,OUTPUT);
	pinMode(Input_4,OUTPUT);
}

void loop()
{
	while(Serial.available() > 0)
	{
		char aChar = Serial.read();
		
		if (aChar=='1')
		{
			Motor_ON();
			Serial.print("Motor_ON and running 1_3 ");
			Motor_1_3();
			Motor_OFF();
		}

		if (aChar=='2')
		{
			Motor_ON();
			Serial.print("Motor_ON and running 2_4 ");
			Motor_2_4();
			Motor_OFF();
		}
		if (aChar=='3')
		{
			Potie();
		}
	}
}

void Motor_ON()
{
	digitalWrite(Enable_A,HIGH);
	digitalWrite(Enable_B,HIGH);
}

void Motor_OFF()
{
	delay(100);
	digitalWrite(Enable_A,LOW);
	digitalWrite(Enable_B,LOW);
	Motor_Coast();
	Serial.println("STOP");
}

void Motor_1_3()
{
	digitalWrite(Input_1,HIGH);
	digitalWrite(Input_2,LOW);
	digitalWrite(Input_3,HIGH);
	digitalWrite(Input_4,LOW);
}

void Motor_2_4()
{
	digitalWrite(Input_1,LOW);
	digitalWrite(Input_2,HIGH);
	digitalWrite(Input_3,LOW);
	digitalWrite(Input_4,HIGH);
}

void Motor_Coast()
{
	digitalWrite(Input_1,LOW);
	digitalWrite(Input_2,LOW);
	digitalWrite(Input_3,LOW);
	digitalWrite(Input_4,LOW);
	Potie();
}

void Potie()
{
	val=analogRead(Poti);
	Serial.print("Poti = ");
	Serial.println(val);
}

i want to make my dc motor to run with a 10 gang poti links and right

I'm assuming "links" means "left", but "10 gang poti" has me stumped.

heh ok edited sorry for that it should be Precision potentiometer. 10 gears

No, sorry still not making a lot of sense (to me anyway).
Are you saying there's a 10:1 gear ratio there?
Or it is a ten turn potentiometer?

Are you saying you're trying to make a DC servo motor?

  1. yes it have 3600° turn so its 10 turn potentiometer
  2. yes over a h-bridge controller

Which bit of the program do you need help with? I can understand your test program.

What is the next step? I think to ask the motor to turn until a specific value is reached on the potentiometer. Initially hardcode the targetvalue in the program, making it dynamic from the serial port (or other source) comes later.

To avoid damaging you potentiometer, you can manually turn the potentiometer and see the motor move or not. Mechanically couple the two together when everything works.

In loop(), when not doing serial (ie when available()==0) check the pot value. If it is less than targetvalue call motorturn one direction. If it is more than targetvalue call motor turn other way.

Check that when you manually turn the knob you can make the motor tun one or other way.

The targetvalue test has to include a dead band, ie the test should be less than magicvalue-5 or greater then targetvalue+5

u help a little now i need to write this code but basic it should look like this arduino read a value from serial.read and then motor turn left or right till my value is true

then motor turn left or right till my value is true

You need to be specific here. Which value? What makes the value "true"? (In C, anything that is not zero is "true" - but you mean something else here, I think)

In fact, the more specific and very clear you are, the easier it is to write the program. In fact that is how you write a program. You start of with a vauge idea. Then write a few sentences describing it in more detail. Then change each of those sentences into several with more excruciating detail. And again. And then suddenly you have written it in so small steps, that it is trivial to translate to C.

value its form 0 to 1023 and if the serial.read input its lets see 500 then this while by my true value if the potentiometer "say so"

That is what I suggested. But take it in small steps. First write a program that calls your motor left/right code if the analogRead() is above/below a fixed value.

Search this forum for "nnnC" - it is a small function I wrote to send decimal values and a command letter using Serial. Good for quick debugging code/prototypes.

One fairly inexpensive setup might be to take a standard servo, mechanically modify it for continous rotation, remove the internal pot and externally connect the 10 turn pot. Connect the pot to the gizmo being rotated and use the servo to rotate the gizmo. The below shows the general idea.

http://www.servocity.com/html/belt_drive_pan_systems.html

om trying to write some code but it dont work have no clue

void Motor()
{
	val1=targetval1;
	val2=targetval2;
	if (targetval1!=targetval2)
	{
		Motor_ON();
		{
			if (targetval1<targetval2)
			{
				Motor_1_3();
			}
			if (targetval1>targetval2)
			{
				Motor_2_4();
			}
		}
	}
	if (targetval1=targetval2)
	{
		Motor_OFF();
	}
}

im using now 2 potentiometer one its mechanically paired with dc motor and the other its for putting my values

Have you looked at the "knob" servo example in the arduino IDE?

yes im trying to "modifying" this code for my purpose

The below code is for servos and different from your motor setup, but it includes a deadband setup that may eliminate position hunting by the motors. In your code you should add printing the generated values and such to the serial monitor so you can see if you are getting what you expect.

//zoomkat dual pot/servo test 12-29-12

#include <Servo.h> 
Servo myservo1;
Servo myservo2;

int potpin1 = 0;  //my pot pin
int potpin2 = 1;

int newval1, oldval1;
int newval2, oldval2;

void setup() 
{
  Serial.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  Serial.println("testing dual pot servo");  
}

void loop() 
{ 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){  
    myservo1.write(newval1);
    Serial.print("1- ");
    Serial.println(newval1);
    oldval1=newval1;
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    myservo2.write(newval2);
    Serial.print("2- ");    
    Serial.println(newval2);
    oldval2=newval2;
  }
  delay(50);
}

dude ur amazing i just looking for the deadband code

void Motor()
{
	val1=analogRead(Poti1);
	val2=analogRead(Poti2);
	if ((val1>val2)&&(val1>minval))
	{
		Motor_1_3();
	}
	if ((val1<val2)&&(val1<maxval))
	{
		Motor_2_4();
	}
	if ((val1=val2)||(val1=minval)||(val1=maxval))
	{
		Motor_OFF();
	}
}

one question can i put val = analogread to a different void or this wouldn't work

one question can i put val = analogread to a different void or this wouldn't work

What happened when you tried it?

nothing my values wear 0 on both potentiometers
now have issue withe the deadband when i use

if (val1 < (targetval1-10) | val1 > (targetval1+10){
if ((val1>val2)&&(val1>minval))
	{
		Motor_1_3();
                         targetval1=val1
	}

my code works one whey

There's a difference between bit wise OR | and (no pun intended) logical OR ||

still no use the same problem