Go Down

Topic: Controlling 2 DC motors at once [For TV] (Read 360 times) previous topic - next topic

njkl44

Sep 15, 2012, 09:25 pm Last Edit: Sep 16, 2012, 02:38 am by njkl44 Reason: 1
Here is a part of the code from the robotic arm I am making. I have 3 motors (2 shoulder, 1 elbow) with pot feedback, basically I made a giant DC servo. Right now I can only control 1 motor at a time. I need to move 2 of them. Im taking my robotic arm on monday to Hollywood for a Nickelodeon show so I need help ASAP.
Code: [Select]

void Shoulder(int potDestPosition) //*****************************************************//
 {
   while(true)
   {
     int currentPotPos = analogRead(shoulder);
     Serial.print("pot value: ");
     Serial.println(currentPotPos);

     if (currentPotPos - potDestPosition > 5)
     {
       turnDirection(true);
     }
     
     else if (potDestPosition - currentPotPos > 5)
     {
       turnDirection(false);
     }
  }
}

void turnDirection(boolean dir) {
   if (dir)
   {
       analogWrite(D1pin1, 0);
       analogWrite(D1pin2, 20);
   }
   else
   {
       analogWrite(D1pin1, shoulderSpeed);
       analogWrite(D1pin2, 0);
   }
 }
     

johnwasser

Looks like your Sholder function never returns. :(

What I would do:

Global variables for the desired position of each of the motors.

In loop(), check each of the motors to see if it need to move.  For each that needs to move, set it going in the desired direction.

For best motion you should be using a PID control loop for each motor.  This will run the motor fast if it is far from the desired position and slow if it is near the desired position.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

njkl44


Looks like your Sholder function never returns. :(

What I would do:

Global variables for the desired position of each of the motors.

In loop(), check each of the motors to see if it need to move.  For each that needs to move, set it going in the desired direction.

For best motion you should be using a PID control loop for each motor.  This will run the motor fast if it is far from the desired position and slow if it is near the desired position.

I attached my final code. The movement and everything else is not an issue at all. All i need is to have the elbow go down as the shoulder rises. I was thinking about maybe having them move one at a time like how I have it but really fast so it looks fluid.

Go Up