Pages: [1]   Go Down
Author Topic: Controlling 2 DC motors at once [For TV]  (Read 289 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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);
    }
  }
      

* arm_nickelodeon_without_btn.ino (0 KB - downloaded 3 times.)
« Last Edit: September 15, 2012, 07:38:54 pm by njkl44 » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8070
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like your Sholder function never returns. smiley-sad

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

0
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like your Sholder function never returns. smiley-sad

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.
Logged

Pages: [1]   Go Up
Jump to: