Go Down

Topic: Looking for some help in stopping this motorized potentiometer midway (Read 5192 times) previous topic - next topic

ash901226


bluesbud

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: https://www.youtube.com/watch?v=R4vXC0ugHZA&feature=plcp). 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.

bluesbud

#17
Dec 06, 2012, 12:41 am Last Edit: Dec 06, 2012, 01:35 am by bluesbud Reason: 1
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.

Code: [Select]
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;
}

ash901226

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

bluesbud

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

Code: [Select]
/*
  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
}

ash901226

i am pretty sure of that due to this line
Code: [Select]
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.

ash901226

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
Code: [Select]
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)
Code: [Select]
digitalWrite (PinA1,LOW);digitalWrite (PinA2,HIGH); need to be change to
Code: [Select]
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

ash901226

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.

ash901226

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.
Code: [Select]

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.

Code: [Select]
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.

bluesbud

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.
Code: [Select]

//=====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;
  }
}
















bluesbud

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

Code: [Select]
//=====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;
    }
  }
}











ash901226

dear bluesbud,
just use try this.

Code: [Select]
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
Code: [Select]
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;
}




ash901226

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.

bluesbud

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.

Code: [Select]

//=====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;
}


































ash901226

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.

Go Up