Looking for some help in stopping this motorized potentiometer midway

So I'm controlling this motorized linear pot: (Slide Pot - Motorized (10k Linear Taper) - COM-10976 - SparkFun Electronics)
via TI SN754410 H-Bridge.

I'm using TouchOSC (custom interface made thru TouchOSC editor) running through the oscP5 and serial libraries in Processing to communicate with the Arduino.

I've got the main objective down - - YouTube

What I'm looking for though more specifically is how to get the motor to stop midway based on it's position reading.

Any help's appreciated!

Here's the Arduino code I have so far:

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

int switchPin = 7;    // switch input
int motor1APin = 6;    // H-bridge leg 1 (pin 2, 1A) PWM
int motor2APin = 5;    // H-bridge leg 2 (pin 7, 2A) PWM
int enablePin = 9;    // H-bridge enable pin
int lineTrack = 5;    //line track for position reading

int incomingByte;   // for incoming serial data

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

void setup(){

  Serial.begin(57600);    

  // set the switch pin and line track as inputs:
  pinMode(switchPin, INPUT);
  pinMode(lineTrack, INPUT);

  //set the motor logic pins and enable pin as outputs
  pinMode(motor1APin, OUTPUT);
  pinMode(motor2APin, OUTPUT);
  pinMode(enablePin, OUTPUT);

}

void loop() {

  // set enablePin high so that motor can turn on:
  digitalWrite(enablePin, HIGH);

  if (Serial.available() > 0) {

    // read the incoming byte data from Processing:
    incomingByte = Serial.read();
    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
  }

  //Move motor forward if truth is told
  if (incomingByte == 65) { 
    motorForward();
  }

  //Move motor forward if lie is told
  else if(incomingByte == 66) {
    motorBackward();
  }

  else if(incomingByte == 67){

    //Obtain position reading
    int pos = analogRead(lineTrack);  //read the line positioning
    pos = map(pos, 0, 1023, 0, 110); //remap positioning to scale of 0-100
    //For proper readings, insert 2 to A5, 3 to 5V, 1 to GND

    if (pos >= 100) {
      motorBackward();
      motorStop();
    }

    else if (pos <= 10) {
      motorForward();
      motorStop();
    }
  }
}

void motorForward(){      
  analogWrite(motor1APin, 30);   
  analogWrite(motor2APin, 150); 
}

void motorBackward(){
  analogWrite(motor1APin, 150);  
  analogWrite(motor2APin, 30);  
}

void motorStop(){
    analogWrite(motor1APin, 255);  
    analogWrite(motor2APin, 255);
  }
}

The motorized potentiometer has 2x tapers. Wire one as normal to your audio or whatever and use the other as position feedback to the device controlling the motor. I did not read the datasheet but one taper may be a linear for feedback and the other taper logarithmic for audio.

dear bluesbud
i had been following your post since the very beginning. I am very much interested in your project. Okay I see that you need a 3 position application rite. i cant help up much with TouchOSC but i can help you with the H-bridge and arduino part.
first of all if you don't already have a motor driver i would suggest you to look up on this 2 link i have for you
http://www.modularcircuits.com/blog/articles/old-h-bridge-secrets/part-1/
http://www.societyofrobots.com/member_tutorials/book/export/html/159

disclaimer: I am not in anyway associated with the author of both web page.

now down to the programming part

pos = map(pos, 0, 1023, 0, 110); //remap positioning to scale of 0-100

Well if you comment is right then your program is wrong.
it should be

pos = map(pos, 0, 1023, 0, 100); //remap positioning to scale of 0-100

but how ever i would say that if you need any only 3 location
lets say I'm using 3 button and your slider which your slider have a motor controller and a linear potentiomenter as feedback.
let say in my set up,
1)if i press the first button, the motor will rotate clockwise and stop at the far end
2)if i press the second button, the motor will rotate anticlockwise and stop at the middle end if its at the far end or if its at the near end it will rotate clockwise and stop at the middle
3)if i press the third button, the motor will rotate anticlockwise and stop at the near end

this is only and example for you to see the logic behind this program, i hope you could translate it to your own setup.

if this is my setup i would like to put some kind of safety or interlock.
i would like that if any two or three button are push at the same time will not make it move.
and i would also like that unless the slider have reach it preset position it will not change its course.
i would also like to make the speed of the motor to be variable so that it is capable of a going to top speed but not overshooting.

in my physical connection from arduino to the motor controller, and the potentiometer
ok i will use a 2 pin motor controller using the circuit that is show in the webpage
http://www.societyofrobots.com/member_tutorials/book/export/html/159
to control the motor, one pin is set as Enable and this one need to be pwm, while the other pin will be the direction
Enable pin will be connected to pin 3 at Uno and pin 2 will be my direction.
if direction is High then motor turn clockwise and if direction is low then motor turn anticlockwise.
this i hypothetically speaking. The speed is control by the value of pwm on the enable, the higher the pwm then the higher the speed.

for the pot i will connect the middle leg to port A0 and the other two to ground and 5 volt respectively

for the buttons, i will hook it to pin 4,5,and 6. For the purpose of this example please consider that this button all have an external circuit that have take care the debounce or this button is a perfect button that does not need to be debounce. I then also put a resistor to the ground and one of the leg of the button, and this middle position is put to the pin. This setup is call as a pull down resistor, or just pulling the pot value down to ground
Okay now for the pseudo code,

read the value from pot, 
read value of button one,button two, and button three,
if value of button one high and button two and three are low then motor rotate clockwise until pot full
if value of button two high, button one and three are low and pot higher then middle value then motor rotate anticlockwise until pot middle value
if value of button two high, button one and three are low and pot lower then middle value then motor rotate clockwise until pot middle value
if button three high and button one and two are low then motor rotate anticlockwise until pot 0
if motor rotate, button value for one, two and three is low

for real code that arduino will understand

int Pot           =   A0;
int Button1       =    4;
int Button2       =    5;
int Button3       =    6;
int Direction     =    2;
int Enable        =    3;
int Clockwise     = HIGH;     
int Anticlockwise =  LOW;
int Stop          =  LOW;
int potval              ;

void setup()
{
  pinMode (Pot,INPUT);
  pinMode (Button1,INPUT);
  pinMode (Button2,INPUT);
  pinMode (Button3,INPUT);
  pinMode (Direction,OUTPUT);
  pinMode (Enable,OUTPUT);
}
void loop()
{
 int PotVal = analogRead(Pot);
 int Btn1    = digitalRead(Button1);
 int Btn2    = digitalRead(Button2);
 int Btn3    = digitalRead(Button3);
 if (Btn1 == HIGH && Btn2 ==LOW && Btn3 ==LOW)
 {
   int Speed = map (PotVal,PotVal,1023,255,0);
   do 
   {
     analogWrite (Enable,Speed);
     digitalWrite (Direction, Clockwise);
     potval = analogRead(Pot);
    }
   while (potval <= 1023);
   if (potval == 1023)
   {
    digitalWrite (Enable,Stop);
   }
 }
 if (Btn2 ==HIGH && Btn1 ==LOW && Btn3 == LOW && PotVal >512)
 {
  int Speed = map (PotVal,PotVal,512,255,0);
  do
  {
    analogWrite (Enable,Speed);
    digitalWrite(Direction,Anticlockwise);
    potval = analogRead(Pot);
  }
  while (potval > 511);
  if (potval == 512)
  {
    digitalWrite (Enable,Stop);
  }
 }
 if (Btn2 ==HIGH && Btn1 ==LOW && Btn3 == LOW && PotVal < 512)
 {
  int Speed = map (PotVal,PotVal,512,255,0);
  do
  {
    analogWrite (Enable,Speed);
    digitalWrite(Direction,Clockwise);
    potval = analogRead(Pot);
  }
  while (potval < 513);
  if (potval == 512)
  {
    digitalWrite (Enable,LOW);
  }
 }
 if (Btn3 == HIGH && Btn1 ==LOW && Btn2 ==LOW)
 {
   int Speed = map (PotVal,PotVal,0,255,0);
   do 
   {
     analogWrite (Enable,Speed);
     digitalWrite (Direction,Anticlockwise);
     potval = analogRead(Pot);
    }
   while (potval >=0);
   if (potval == 0)
   {
    digitalWrite (Enable,Stop);
   }
 }
}

I hope you could understand my code bluesbud, this code however just as an example of what you could do.
I hope someone could verify my code,

Thanks for following, I'll be checking this in a bit later this afternoon.

As for the remapping code

pos = map(pos, 0, 1023, 0, 110); //remap positioning to scale of 0-100

I actually did that on purpose because it was the readings I was getting were a bit under. 100% on the far end read 90, so I just added 10.

Hey, this looks great and is really helpful. I'm just a bit confused about the buttons, considering my buttons are on a TouchOSC interface and don't necessarily have a HIGH or LOW state. I just have serial communication coming in.

the code i did is just an example of what you could do, if you have 3 different button. lets say in a Serial communication situation. just use if like if serial read == 1 (meaning the button one) then Button 1 = high. i hope this make sense to you

Ah, yeah, that makes sense. Thanks!

Hrrrmm, something's gone awry. See attached zip file.

I have a feeling it has to do with the position of enablePin, where I analogRead the potVal in the do function or how I'm calling previous & new states.

_10k_Pot_Control.zip (3.07 KB)

what software are you using. i cant read any of your file.

I'm using Arduino...though I have had Processing and Arduino sketches open in each other's platform. Have you checked default program that opens it?

Any luck getting them open? I'll see what's up.

Yeah, they should open just fine.

Here's the code. I've split them up in tabs to keep it a bit organized (thus the zip).

So, I figure it might be best to give you context so you're not confused. I have two puppets, Obama and Romney, with this motor as a Pinocchio nose that moves forward or backwards based on the truthfulness of a selected statement. The statements are selected via TouchOSC on an iPad.

I've attached my processing code below this one.

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

int switchPin = 7;    // switch input
int motor1APin = 6;    // H-bridge leg 1 (pin 2, 1A) PWM
int motor2APin = 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 = 5;    //line track for position reading
int potVal;
int prevState;  

int incomingByte;   // for incoming serial data
//=============================

void setup(){

  Serial.begin(57600);    

  // set the switch pin and line track as inputs:
  pinMode(switchPin, INPUT);
  pinMode(lineTrack, INPUT);

  //set the motor logic pins and enable pin as outputs
  pinMode(motor1APin, OUTPUT);
  pinMode(motor2APin, OUTPUT);
  pinMode(enablePin, OUTPUT);

}

void loop() {

  // set enablePin high so that motor can turn on:
  digitalWrite(enablePin, HIGH);

  readSerial();

  //Cases are named by previous state, followed by new state
  lieTruth();
  truthLie();
  truthHalf();
  lieHalf();
  halfTruth();
  halfLie();
}


//If previous state was lie and new state is a truth, move backwards all the way
void lieTruth (){
  if (prevState == 2 && incomingByte == 65){
    //    //Obtain position reading
    //    int potVal = analogRead(lineTrack);  //read the line positioning
    //    int Speed = map (potVal,potVal,0,255,0);  //Map from 1023-0, HIGH-LOW
    do 
    {
      //      analogWrite (enablePin, Speed);
      motorBackward();
      potVal = analogRead(lineTrack);
    }
    while (analogRead(lineTrack) >= 0);
    if (potVal == 0){
      motorStop();
    }
  }
}


//If previous state was truth and new state is a lie, move forward all the way
void truthLie(){
  if (prevState == 1 && incomingByte == 66){
    //Obtain position reading
    //    int potVal = analogRead(lineTrack);  //read the line positioning
    //    int Speed = map (potVal,potVal,1023,255,0);    //Map from 0-1023, HIGH-LOW
    do 
    {
      //      analogWrite (enablePin, Speed);
      motorForward();
      potVal = analogRead(lineTrack);
    }
    while (analogRead(lineTrack) <= 1023);
    if (potVal == 1023){
      motorStop();
    }
  }
}

//If prevState was truth and new state half truth. Go forwards halfway
void truthHalf(){
  if (prevState == 1 && incomingByte == 67){
    //Obtain position reading
    //    int potVal = analogRead(lineTrack);  //read the line positioning
    //    int Speed = map (potVal,potVal,512,255,0);  //Map from 0-512, HIGH-LOW
    do 
    {
      //      analogWrite (enablePin, Speed);
      motorForward();
      analogRead(lineTrack);
    }
    while (analogRead(lineTrack) >= 513);
    if (potVal == 512){
      motorStop();
    }
  }
}

//If prevState was lie and new state half truth. Go backwards halfway
void lieHalf(){
  if (prevState == 2 && incomingByte == 67){
    //Obtain position reading
    //    int potVal = analogRead(lineTrack);  //read the line positioning
    //    int Speed = map (potVal,potVal,512,255,0);  //Map from 1023-512 to HIGH-LOW
    do 
    {
      //      analogWrite (enablePin, Speed);
      motorBackward();
      analogRead(lineTrack);
    }
    while (analogRead(lineTrack) >= 511);
    if (potVal == 512){
      motorStop();
    }
  }
}

//If prevState was half-truth and new state truth. Go backwards halfway
void halfTruth(){
  if (prevState == 3 && incomingByte == 65){
    //Obtain position reading
    //    int potVal = analogRead(lineTrack);  //read the line positioning
    //    int Speed = map (potVal,potVal,0,255,0);    //Map from 512-0, HIGH-LOW
    do 
    {
      //      analogWrite (enablePin, Speed);
      motorBackward();
      analogRead(lineTrack);
    }
    while (analogRead(lineTrack) <= 513);
    if (potVal == 0){
      motorStop();
    }
  }
}


//If prevState was half-truth and new state lie. Go forwards halfway
void halfLie(){
  if (prevState == 3 && incomingByte == 66){
    //Obtain position reading
    //    int potVal = analogRead(lineTrack);  //read the line positioning
    //    int Speed = map (potVal,potVal,1023,255,0);    //Map from 512-1023, HIGH-LOW
    do 
    {
      //      analogWrite (enablePin, Speed);
      motorForward();
      analogRead(lineTrack);
    }
    while (analogRead(lineTrack) >= 511);
    if (potVal == 1023){
      motorStop();
    }
  }
}

void motorForward(){      
  analogWrite(motor1APin, 30);   
  analogWrite(motor2APin, 160); 
}

void motorBackward(){
  analogWrite(motor1APin, 160);  
  analogWrite(motor2APin, 30);  
}


void motorStop(){
   analogWrite(motor1APin, 255);  
   analogWrite(motor2APin, 255);
  }

void readSerial(){

  int prevState;  
  //Allow previous states to be stored here in prevState
  // 1 = truth
  // 2 = lie
  // 3 = half-truth

  // read the incoming byte data from Processing:
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
  }

  //Store data as previous states
  if (incomingByte == 65) {   //Truth
    prevState = 1;
  }
  else if(incomingByte == 66) {  //Lie
    prevState = 2;
  }
  else if(incomingByte == 67) {  //Half-truth
    prevState = 3;
  }

}

Processing code:

//libraries needed for arduino communication
import processing.serial.*;

//libraries needed for osc
import oscP5.*;
import netP5.*;

Serial arduinoPort;        //  Set arduinoPort as serial connection

OscP5 oscP5;
NetAddress myRemoteLocation;

//set/change port numbers here
int incomingPort = 8000;
int outgoingPort = 9000;

//set/change the IP address that the OSC data is being sent to
//127.0.0.1 is the local address (for sending osc to an application on the same computer)
String ipAddress = "127.0.0.1";

int truthVal;  //truthValue

void setup() 
{
  size(100, 100);
  background(0);
  frameRate(25);

  /* start oscP5, listening for incoming messages at port 8000 */
  //for INCOMING osc messages (e.g. from Max/MSP)
  oscP5 = new OscP5(this, incomingPort); //port number set above

  arduinoPort = new Serial(this, Serial.list()[4], 57600);  //port 4 is Arduino to USB
}

// Incoming osc message are forwarded to the following oscEvent method. Write to the arduino here--------
// This method is called for each OSC message recieved
void oscEvent(OscMessage theOscMessage) 
{

  println("### received an osc message.");
  println("addrpattern: "+theOscMessage.addrPattern());

  //Convert the OSC address pattern to a string
  String addr = theOscMessage.addrPattern();

  if (addr.equals("/o/truth") == true) //if the osc message reads obama truth
  {
    truthVal = 0;    //Set truthVal to 0
  }

  else if (addr.equals("/o/lie") == true) //if the osc message reads obama lie
  {  
    truthVal = 1;  //Set truthVal to 1
  }
  else if (addr.equals("/o/halfTruth") == true)
  {
    truthVal = 2;
  }
}


void draw() 
{
  //write serial data to for the Arduino to read
  if (truthVal == 0) //if the osc message reads obama truth
  {  
    arduinoPort.write(65);  
    println("Obama Truth");
  }
  else if (truthVal == 1) //if the osc message reads obama truth
  {  
    arduinoPort.write(66);
    println("Obama Lie");
  }
  else if (truthVal == 2) //if the osc message reads obama truth
  {  
    arduinoPort.write(67);
    println("Obama Half-Truth");
  }
}

hello there, sorry i cant even open any of your zip file, maybe its my computer.
int lineTrack = 5;    //line track for position reading the 5 need to change to A5. anyway i got an idea, to reduce the programming in arduino part.

lets see maybe in the processing part you could send a serial data maybe A,B and C

if A receive the motor will move to position 0
if B receive the motor will move to position 1
if C receive the motor will move to position 2

and the list could expend as you seem fit.

now in arduino

lets say if arduino receive an A= 0
then it will move to position to where the pot is 0
let say if arduino receive a B = 512
then it will move to position to where the pot is 512
lets say if arduino receive a c = 1023
then it will move to position to where the pot is 1023

lets try to program the arduino

const int Pot     = A0;  //Set the Linear Pot middle pin to Arduino A0 port
const int Enable =  2;  //Set the Motor controller pin on pin 2 Arduino  
const int A1      =  3;  //Set the Motor Controller pin A1 to port 3;
const int A2      =  4;  //Set the Motor Controller pin A2 to port 4; 
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 (A1,OUTPUT);       //Set A1 as Output
 pinMode (A2,OUTPUT);       //Set A2 as Output
 Serial.begin(9600);             //Initialize Serial Communication at 9600 baud rate
}

void loop() 
{
  if (Serial.available() > 0) 
  {
    int inByte = Serial.read();
    switch (inByte)
   {
     case 'A':
                MoveMotor(0);
                break;
     case 'B':    
                MoveMotor(512);
                break;     
     case 'C':    
                MoveMotor(1023);
                break;
   
     default:
   }
  }
}

void MoveMotor(Target)
{
  int PotVal =AnalogRead (Pot);
  if (PotVal > Target)
  {
   Speed = map (PotVal,Target,PotVal,0,255); 
   analogWrite (Enable, Speed);
   digitalWrite (A1, HIGH);
   digitalWrite (A2,LOW);
  }
  if (PotVal < Target)
  {
   Speed = map (PotVal,PotVal,Target,0,255); 
   analogWrite (Enable, Speed);
   digitalWrite (A1, LOW);
   digitalWrite (A2,HIGH);
  }
  else
 {
  digitalWrite (Enable,HIGH);
  digitalWrite (A1, LOW);
  digitalWrite (A2, LOW);
  return;
 }
}

ok. please try the program with your serial sending only A B or C no need to change to ASCII value. this way if you increase the number of stop position you dont have to change the program instead just add more stop along the way.

sorry the program i gave b4 will not compile. use this one instead

const int Pot     = A0;  //Set the Linear Pot middle pin to Arduino A0 port
const int Enable =  2;  //Set the Motor controller pin on pin 2 Arduino  
const int  pin1A      =  3;  //Set the Motor Controller pin A1 to port 3;
const int pin2A      =  4;  //Set the Motor Controller pin A2 to port 4; 
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 (A1,OUTPUT);       //Set A1 as Output
 pinMode (A2,OUTPUT);       //Set A2 as Output
 Serial.begin(9600);             //Initialize Serial Communication at 9600 baud rate
}

void loop() 
{
  if (Serial.available() > 0) 
  {
    int inByte = Serial.read();
    switch (inByte)
   {
     case 'A':
                MoveMotor(0);
                break;
     case 'B':    
                MoveMotor(512);
                break;     
     case 'C':    
                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;
}

Hrmm, no luck :confused:

could you give me more information then just no luck?

So, for my code in the first post in this thread, once I open Processing and run the program (thus sending serial info) I instantly hear the motor, it's quite noisy when it's on (you can hear it in the video: - YouTube). All the subsequent codes that I've worked on since, the motor won't even turn on...which is usually the first sign of good news.

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
}