Looking for some help in stopping this motorized potentiometer midway

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
}

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.