Looking for some help in stopping this motorized potentiometer midway

I've got it on its way. I made the cases into 0, 1, 2 instead of a, b, c, and am sending those numbers from Processing. Only case 2 (move motor halfway) works, but it shoots all the way forward, and only when I add high output to the enable pin right at the start of void loop(), but when I tried moving it back with my finger it's unable to go past midpoint (b/c I've tried out all the cases on the iPad via TouchOSC). So it's getting the messages but something's up with the enable pin.

const int Pot     = A5;  //Set the Linear Pot middle pin to Arduino A5 port
const int Enable =  9;  //Set the Motor controller pin on pin 9 Arduino  
const int  pin1A      =  6;  //Set the Motor Controller pin A1 to port 6;
const int pin2A      =  5;  //Set the Motor Controller pin A2 to port 5; 
int Target        =  0;  //Target is the position we want the motor to move to 
int Speed         =  0;  //Speed Control for motor
void setup()
{
  pinMode (Pot,INPUT);          //Set Pot as Input  
  pinMode (Enable,OUTPUT);  //Set Enable as Output
  pinMode (6,OUTPUT);       //Set A1 as Output
  pinMode (5,OUTPUT);       //Set A2 as Output
  Serial.begin(57600);             //Initialize Serial Communication at 9600 baud rate
}

void loop() {
 
  analogWrite(Enable, 255);

  if (Serial.available() > 0) 
  {
    int inByte = Serial.read();
    switch (inByte)
    {
    case 0:
      MoveMotor(0);
      break;
    case 1:    
      MoveMotor(512);
      break;     
    case 2:    
      MoveMotor(1023);
      break;

//    default:
//      MotorStop();
//      break;
    }
  }
}

void MoveMotor(int Target)
{
  int PotVal =analogRead (Pot);
  if (PotVal > Target)
  {
    Speed = map (PotVal,Target,PotVal,0,255); 
    analogWrite (Enable, Speed);
    digitalWrite (pin1A, HIGH);
    digitalWrite (pin2A, LOW);
  }
  if (PotVal < Target)
  {
    Speed = map (PotVal,PotVal,Target,0,255); 
    analogWrite (Enable, Speed);
    digitalWrite (pin1A, LOW);
    digitalWrite (pin2A,HIGH);
  }
  else
  {
    digitalWrite (Enable,HIGH);
    digitalWrite (pin1A, LOW);
    digitalWrite (pin2A, LOW);
    return;
  }
} 
void MotorStop()
{
  digitalWrite (Enable,HIGH);
  digitalWrite (pin1A, LOW);
  digitalWrite (pin2A, LOW);
  return;
}

sorry if my code did not gave you any hint of its working but i think maybe there is another part i forget to put in maybe it need a do while statement the code so that i could work properly, btw you cant use 0 as your first case since that will never be true.. urm let me test with my set up b4 i get back to you

Are you sure about case 0? This example was built into Arduino:

/*
  Switch statement
 
 Demonstrates the use of a switch statement.  The switch
 statement allows you to choose from among a set of discrete values
 of a variable.  It's like a series of if statements.
 
 To see this sketch in action, but the board and sensor in a well-lit
 room, open the serial monitor, and and move your hand gradually
 down over the sensor.
 
 The circuit:
 * photoresistor from analog in 0 to +5V
 * 10K resistor from analog in 0 to ground
 
 created 1 Jul 2009
 modified 9 Apr 2012
 by Tom Igoe 
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/SwitchCase
 */

// these constants won't change. They are the
// lowest and highest readings you get from your sensor:
const int sensorMin = 0;      // sensor minimum, discovered through experiment
const int sensorMax = 600;    // sensor maximum, discovered through experiment

void setup() {
  // initialize serial communication:
  Serial.begin(9600);  
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);
  // map the sensor range to a range of four options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

  // do something different depending on the 
  // range value:
  switch (range) {
  case 0:    // your hand is on the sensor
    Serial.println("dark");
    break;
  case 1:    // your hand is close to the sensor
    Serial.println("dim");
    break;
  case 2:    // your hand is a few inches from the sensor
    Serial.println("medium");
    break;
  case 3:    // your hand is nowhere near the sensor
    Serial.println("bright");
    break;
  } 
  delay(1);        // delay in between reads for stability
}

i am pretty sure of that due to this line

if (Serial.available() > 0)

it means that if the arduino will wait until a value more then 0 then it will proceed to the other part of the program.
since your first case is 0 but the program only execute after receiving anything more then 0 i am sure that arduino will never execute for case 0.

i have study the code i made for you and build a mockup of your setup using a couple of resistor and a pot to simulate your setup
so here's a new one
just change the MotorMove Function with this one

 void MotorMove(int Target)
{ 
 int SenseVal=analogRead(Sense);
 if (SenseVal > Target)
 {
  do 
  {
    int Speed=map (SenseVal,SenseVal,Target,255,0);
    analogWrite (Enable,Speed);
    digitalWrite (PinA1,HIGH);
    digitalWrite (PinA2,LOW);
    SenseVal=analogRead (Sense);
  }
  while (SenseVal !=Target);
 }
 if (SenseVal < Target)
 {
   do 
  {
   int Speed=map (SenseVal,SenseVal,Target,255,0);
    analogWrite (Enable,Speed);
    digitalWrite (PinA1,LOW);
    digitalWrite (PinA2,HIGH);
    SenseVal=analogRead (Sense);
  }
  while (SenseVal !=Target);
 }
  
  if (SenseVal == Target){
    digitalWrite (Enable,HIGH);
    digitalWrite (PinA1,HIGH);
    digitalWrite (PinA2,HIGH);
}}

this code you must be able to change a few thing to suit your need.

  1. I use Sense as my pot analogRead. change that.
  2. digitalWrite (PinA1,LOW);digitalWrite (PinA2,HIGH); need to be change to
    digitalWrite (PinA1,HIGH);digitalWrite (PinA2,LOW); if the motor turn to the wrong direction.
    3)change the name of each int to be consistent with the one i post

for number 2 it must be change for both statement.
wow i got to say doing your project is the first time i use 3 type of looping in one program.
if statement,do..while and switch...case... haha thanks doing you project had teach me alot about how to use all this 3 type of loop effectively.

I have tried to improve the program more. this time with speed control so that was it gets nearer to its set point, the motor would slow down and eventually stop at the determine position.

void MotorMove(int Target)
{
 int SenseVal1;
 int SenseVal=analogRead(Sense);
 int ValSense=analogRead(Sense);
 if (SenseVal > Target)
 {
  do 
  {
    int Speed=map (SenseVal1,ValSense,Target,255,0);
    analogWrite (Enable,Speed);
    digitalWrite (PinA1,HIGH);
    digitalWrite (PinA2,LOW);
    SenseVal1=analogRead (Sense);
    SenseVal=SenseVal1;
  }
  while (SenseVal1 !=Target);
 }
 if (SenseVal < Target)
 {
   do 
  {
   int Speed=map (SenseVal1,ValSense,Target,255,0);
    analogWrite (Enable,Speed);
    digitalWrite (PinA1,LOW);
    digitalWrite (PinA2,HIGH);
    SenseVal1=analogRead (Sense);
    SenseVal=SenseVal1;
    
  }
  while (SenseVal1 !=Target);
 }
  
  if (SenseVal == Target){
    digitalWrite (Enable,HIGH);
    digitalWrite (PinA1,HIGH);
    digitalWrite (PinA2,HIGH);
}
}

or this if the motor moving at the wrong direction, you know its the wrong direction if the motor reach its end but still wont stop.

void MotorMove(int Target)
{
 int SenseVal1;
 int SenseVal=analogRead(Sense);
 int ValSense=analogRead(Sense);
 if (SenseVal > Target)
 {
  do 
  {
    int Speed=map (SenseVal1,ValSense,Target,255,0);
    analogWrite (Enable,Speed);
    digitalWrite (PinA1,LOW);
    digitalWrite (PinA2,HIGH);
    SenseVal1=analogRead (Sense);
    SenseVal=SenseVal1;
  }
  while (SenseVal1 !=Target);
 }
 if (SenseVal < Target)
 {
   do 
  {
   int Speed=map (SenseVal1,ValSense,Target,255,0);
    analogWrite (Enable,Speed);
    digitalWrite (PinA1,HIGH);
    digitalWrite (PinA2,LOW);
    SenseVal1=analogRead (Sense);
    SenseVal=SenseVal1;
    
  }
  while (SenseVal1 !=Target);
 }
  
  if (SenseVal == Target){
    digitalWrite (Enable,HIGH);
    digitalWrite (PinA1,HIGH);
    digitalWrite (PinA2,HIGH);
}}

change to your program to make it compatible are
A) the linear pot name have been change to Sense
B) Enable is the same;
C) PWM pin 1 Is now PinA1
D) PWM pin 2 is now PinA2

I hope this help you with your program.

Very very very very close!

So I've gotten it to stop in the middle, except it shakes fervently back and forth in the middle.

I think it has to do with conflicting messages in the motor move function.

//=====INITIALIZE PINS========

int PinA1 = 6;    // H-bridge leg 1 (pin 2, 1A) PWM
int PinA2 = 5;    // H-bridge leg 2 (pin 7, 2A) PWM
int enablePin = 9;    // H-bridge enable pin

//For proper lineTrack readings, insert 2 to A5, 3 to 5V, 1 to GND
int lineTrack = A5;    //line track for position reading
int potVal;

int inByte = 0;   // for incoming serial data
//=============================

void setup(){

  Serial.begin(57600);    

  // set the line track as input
  pinMode(lineTrack, INPUT);

  //set the motor logic pins and enable pin as outputs
  pinMode(PinA1, OUTPUT);
  pinMode(PinA2, OUTPUT);
  pinMode(enablePin, OUTPUT);
}

void loop() {

  // set enablePin high so that motor can turn on:
  potVal = analogRead(lineTrack);

  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
    Serial.println(inByte);

    if (inByte == 97) {
      moveToTarget1(0);
    }

    if (inByte == 98) {
      moveToTarget1(1023);
    }

    if (inByte == 99) {
      moveToTarget1(512);
    }
  }
}

void moveToTarget1(int target)
{
  int pos;
  potVal = analogRead(lineTrack);

  if ( potVal > (target) )
  {
    digitalWrite(enablePin, HIGH);
    analogWrite (PinA1, 150);  //Move backwards
    analogWrite (PinA2, 30);
    potVal = analogRead(lineTrack);
    pos = potVal;
  }


  if ( potVal < (target) )
  {
    digitalWrite(enablePin, HIGH);
    analogWrite (PinA1, 30);        //Move forward
    analogWrite (PinA2, 150);
    potVal = analogRead(lineTrack);
    pos = potVal;
  }

  if ( potVal < target+3 && potVal >target-3)
  {
    digitalWrite(enablePin, LOW);  //Stop
    digitalWrite (PinA1, HIGH);
    digitalWrite (PinA2, HIGH);
    potVal = analogRead(lineTrack);
    pos = potVal;
  }
}

Not sure if I submitted a working code, this one's more efficient.

//=====INITIALIZE PINS========

int PinA1 = 6;    // H-bridge leg 1 (pin 2, 1A) PWM
int PinA2 = 5;    // H-bridge leg 2 (pin 7, 2A) PWM
int enablePin = 9;    // H-bridge enable pin

//For proper lineTrack readings, insert 2 to A5, 3 to 5V, 1 to GND
int lineTrack = A5;    //line track for position reading
int potVal;

int inByte = 0;   // for incoming serial data

// These variables will change:
int sensorMin = 1023;  // minimum sensor value
int sensorMax = 0;     // maximum sensor value
int sensorValue = 0;         // the sensor value
//=============================

void setup(){

  Serial.begin(57600);    

  // set the line track as input
  pinMode(lineTrack, INPUT);

  //set the motor logic pins and enable pin as outputs
  pinMode(PinA1, OUTPUT);
  pinMode(PinA2, OUTPUT);
  pinMode(enablePin, OUTPUT);
}

void loop() {

  potVal = analogRead(lineTrack);

  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {

    // get incoming byte:
    inByte = Serial.read();

    if (inByte == 97) {
      moveToTarget1(0);
    }


    if (inByte == 98) {
      moveToTarget1(100);
    }


    if (inByte == 99) {
      moveToTarget1(50);
    }
  }
}

void calibrate() {
   
  // read the sensor
  potVal = analogRead(lineTrack);

  // record the maximum sensor value
  if (sensorValue > sensorMax) {
    sensorMax = sensorValue;
  }

  // record the minimum sensor value
  if (sensorValue < sensorMin) {
    sensorMin = sensorValue;
  }
}

void moveToTarget1(int target)
{

  int pos;
  calibrate();
  potVal = analogRead(lineTrack);
  // apply the calibration to the sensor reading
  potVal = map(potVal, sensorMin, sensorMax, 0, 110);
  // in case the sensor value is outside the range seen during calibration
  potVal = constrain(potVal, 0, 110);


  if (potVal != target || potVal != target+1 || potVal != target-1){
    //Move backwards if potValue less than or equal target
    if ( potVal > target)
    {  
      digitalWrite(enablePin, HIGH);
      analogWrite (PinA1, 150);  //Move backwards
      analogWrite (PinA2, 30);
      potVal = analogRead(lineTrack);
      pos = potVal;
    }
    //Move forward if potValue less than target
    if ( potVal < target )      
    {
      digitalWrite(enablePin, HIGH);
      analogWrite (PinA1, 30);        //Move forward
      analogWrite (PinA2, 150);
      potVal = analogRead(lineTrack);
      pos = potVal;
    }
  }
}

dear bluesbud,
just use try this.

int PinA1 = 6;    // H-bridge leg 1 (pin 2, 1A) PWM
int PinA2 = 5;    // H-bridge leg 2 (pin 7, 2A) PWM
int Enable = 9;    // H-bridge enable pin
int Sense = A5;    //line track for position reading

void setup()
{
  Serial.begin(9600);
  pinMode(Sense, INPUT);
  pinMode(PinA1, OUTPUT);
  pinMode(PinA2, OUTPUT);
  pinMode(Enable, OUTPUT);
}

void loop()
{
  if (Serial.available() > 0) 
  {
   int inByte = Serial.read();
   switch (inByte) {
    case 'a':    
      MotorMove(0);
      break;
    case 'b':    
      MotorMove(512);
      break;
    case 'c':    
      MotorMove(1023);
      break;
      default:
      MotorStop();
      break;
      
   }
  }
} 

void MotorMove(int Target)
{
 int SenseVal1;
 int SenseVal=analogRead(Sense);
 int ValSense=analogRead(Sense);
 if (SenseVal > Target)
 {
  do 
  {
    int Speed=map (SenseVal1,ValSense,Target,255,0);
    analogWrite (Enable,Speed);
    digitalWrite (PinA1,HIGH);
    digitalWrite (PinA2,LOW);
    SenseVal1=analogRead (Sense);
    SenseVal=SenseVal1;
  }
  while (SenseVal1 >Target);
 }
 if (SenseVal < Target)
 {
   do 
  {
   int Speed=map (SenseVal1,ValSense,Target,255,0);
    analogWrite (Enable,Speed);
    digitalWrite (PinA1,LOW);
    digitalWrite (PinA2,HIGH);
    SenseVal1=analogRead (Sense);
    SenseVal=SenseVal1;
    
  }
  while (SenseVal1 <Target);
 }
  
  if (SenseVal == Target){
    digitalWrite (Enable,HIGH);
    digitalWrite (PinA1,HIGH);
    digitalWrite (PinA2,HIGH);
    return;
  }}

void MotorStop()
{
  digitalWrite (Enable,HIGH);
  digitalWrite (PinA1,HIGH);
  digitalWrite (PinA2,HIGH);
  return;
}

or this if its move at the wrong direction

int PinA1 = 6;    // H-bridge leg 1 (pin 2, 1A) PWM
int PinA2 = 5;    // H-bridge leg 2 (pin 7, 2A) PWM
int Enable = 9;    // H-bridge enable pin
int Sense = A5;    //line track for position reading

void setup()
{
  Serial.begin(9600);
  pinMode(Sense, INPUT);
  pinMode(PinA1, OUTPUT);
  pinMode(PinA2, OUTPUT);
  pinMode(Enable, OUTPUT);
}

void loop()
{
  if (Serial.available() > 0) 
  {
   int inByte = Serial.read();
   switch (inByte) {
    case 'a':    
      MotorMove(0);
      break;
    case 'b':    
      MotorMove(512);
      break;
    case 'c':    
      MotorMove(1023);
      break;
      default:
      MotorStop();
      break;
      
   }
  }
} 

void MotorMove(int Target)
{
 int SenseVal1;
 int SenseVal=analogRead(Sense);
 int ValSense=analogRead(Sense);
 if (SenseVal > Target)
 {
  do 
  {
    int Speed=map (SenseVal1,ValSense,Target,255,0);
    analogWrite (Enable,Speed);
    digitalWrite (PinA1,LOW);
    digitalWrite (PinA2,HIGH);
    SenseVal1=analogRead (Sense);
    SenseVal=SenseVal1;
  }
  while (SenseVal1 >Target);
 }
 if (SenseVal < Target)
 {
   do 
  {
   int Speed=map (SenseVal1,ValSense,Target,255,0);
    analogWrite (Enable,Speed);
    digitalWrite (PinA1,HIGH);
    digitalWrite (PinA2,LOW);
    SenseVal1=analogRead (Sense);
    SenseVal=SenseVal1;
    
  }
  while (SenseVal1 <Target);
 }
  
  if (SenseVal == Target){
    digitalWrite (Enable,HIGH);
    digitalWrite (PinA1,HIGH);
    digitalWrite (PinA2,HIGH);
    return;
  }}

void MotorStop()
{
  digitalWrite (Enable,HIGH);
  digitalWrite (PinA1,HIGH);
  digitalWrite (PinA2,HIGH);
  return;
}

to test this code, copy and paste the code to a new sketch and use the serial monitor to see if its working,
you could send a,b,or c. could you take a video of it so that i know how to amend the code to be more efficient. Its hard to write a code for something that i cant see.

Awesome! So my phone died and you can't really see anything on my webcam but pressing the three states on the iPad now works for 0, 512, and 1023. It jitters in the middle now for maybe a second and then it stops. Thank ya! Thank ya! Thank ya! I touched it up a bit to suit my needs.

//=====INITIALIZE PINS========

const int PinA1 = 6;    // H-bridge leg 1 (pin 2, 1A) PWM
const int PinA2 = 5;    // H-bridge leg 2 (pin 7, 2A) PWM
const int enablePin = 9;    // H-bridge enable pin

//For proper lineTrack readings, insert 2 to A5, 3 to 5V, 1 to GND
int lineTrack = A5;    //line track for position reading

int inByte = 0;   // for incoming serial data

int pos;
int potVal;
int posRead;
//=============================

void setup(){

  Serial.begin(57600);    

  // set the line track as input
  pinMode(lineTrack, INPUT);

  //set the motor logic pins and enable pin as outputs
  pinMode(PinA1, OUTPUT);
  pinMode(PinA2, OUTPUT);
  pinMode(enablePin, OUTPUT);
}

void loop() {

  potVal = analogRead(lineTrack);
  posRead = analogRead(lineTrack);

  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {

    // get incoming byte:
    inByte = Serial.read();

    switch (inByte) {
    case 'a':    
      motorMoveA(0);
      break;
    case 'b':   
      motorMoveA(1023);
      break;
    case 'c':    
      motorMoveA(512);
      break;
    default:
      motorStop();
      break;
    }
  }
} 

void motorMoveA(int target)
{
  pos;
  potVal = analogRead(lineTrack);
  posRead = analogRead(lineTrack);

  //Move backwards if potValue less than or equal target
  if ( potVal > target)
  {  
    do 
    {
      posRead = analogRead(lineTrack);
      int Speed=map (pos,posRead, target, 255,0);
      analogWrite (enablePin,Speed);
      analogWrite (PinA1, 180);        
      analogWrite (PinA2, 30);
      potVal = analogRead(lineTrack);
      pos = potVal;
    }
    while (pos > target);
  }


  //Move forward if potValue less than target
  if (potVal < target)      
  {
    do
    {
      posRead = analogRead(lineTrack);
      int Speed=map (pos,posRead, target,255,0);
      analogWrite (enablePin,Speed);
      analogWrite (PinA1, 30);        
      analogWrite (PinA2, 180);
      potVal = analogRead(lineTrack);
      pos = potVal;
    }
    while
      (pos < target);
  }

  if (potVal == target){
    digitalWrite (enablePin,HIGH);
    digitalWrite (PinA1,HIGH);
    digitalWrite (PinA2,HIGH);
    return;
  }
}

void motorStop()
{
  digitalWrite (enablePin,HIGH);
  digitalWrite (PinA1,HIGH);
  digitalWrite (PinA2,HIGH);
  return;
}

well then cool... Good to know, i been spending much time thinking about your project. haha anyway this code make it easier for your to expend to other position and you could use all the alphabetical from a to z and A to Z that means you could have more or less 52 position if you could code it all.

Yeah, I learned a ridiculous amount working on this project. Thanks for all the help, seriously.

Art show for it is later today. I'll probably put up some nice HQ videos later.

dear bluesbud
if your motor controller is the same as a L298 Motor controller ic that have 3 pin to control the motor speed and direction i would like to point out some important info
this is the data sheet for L298
http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/CD00000240.pdf

if you go to page 6/13 and go to Figure 6 : Bidirectional DC Motor Control. there is a Table on how to give signal to the motor controller and what actually it does
ok in the table there are Ven which mean Enable, C and D which is PinA1 and PinA2 respectively.
Base on the Table
Enable = HIGH,C=HIGH,D=LOW then Motor will run in full speed at one direction
Enable=HIGH,C=LOW,D+HIGH then the motor will run at full speed on the other direction
Enable=HIGH,C=D, motor will hard stop. Mean that you have a hard time turning the motor
Enable=LOW,C&D Dont care(dont mean a thing if left high or low) Motor Flywheel, Essentially the motor is disconnected from the circuit. you can easily turn the motor shaft.

ok lets say driving the motor is the same as driving a car. If we were to pulse only the enable pin, and C and D would be at different state,its an equal to push fully the gas pedal, then release, do this a couple dozen of time and you essentially controlling the speed of the motor,
let say that your pulsing the C or D instead of Enable, its like your both your feet is at the brake and the pedal, first your push the gas to the max, then you push the brake to the max. yes you are controlling the motor but, you are wasting allot of energy in that kind of setup.

in your code

int Speed=map (pos,posRead, target,255,0);
      analogWrite (enablePin,Speed);
      analogWrite (PinA1, 30);        
      analogWrite (PinA2, 180);

what does this means.Your actually pulsing all the enable,PinA1 and PinA2. yes the motor will rotate, but there will be time that all the pin would be high and making the motor want to stop. there will also be time that the Enable is low, which make the motor Free wheeling . and there will be time that the motor will rotate to a certain direction. but all and all, i hope you would consider my explanation so you could make the motor run at its optimum speed.

untitled.bmp (406 KB)

yes, i actually learn allot from this project. Usually i would charge you for the service that i gave to you. haha but for you this time it on me, just hope you post that vid you promise as my payment. haha

Ah, okay that makes sense. I was actually using analogWrite initially for the logic pins and digitalWrite for the enablePin in order to slow the motor down, since it was going so fast.

Here's a little video from the other day. More documentation to come in a bit. Might've been my soldering or something, but it kept breaking down during the show. I kept having to restart the programs. Additionally, something happened with the midway feature again.

can you explain to me more about this problem and the program that you use?