Robotic arm, need help with servo movement

zoomkat:

You need to add (say) delay(50); as the last thing in loop(). And you shouldn't use delay() in your real code - just for this demo.

New code should be based on using Millis and not delay. Maybe a little more complex, but probably better control.

I thought I had already covered that point ??

...R

zoomkat:

You need to add (say) delay(50); as the last thing in loop(). And you shouldn't use delay() in your real code - just for this demo.

New code should be based on using Millis and not delay. Maybe a little more complex, but probably better control.

How would that look?
I am learning as I go here, and THANK YOU!!! For the help!

Here is your code Robin2:

Here is a mod to go in reverse:

/*
August 24th 2014 
Version 1.2 Firmware development for the open source "Lite Arm" Project:
http://www.thingiverse.com/thing:407800
Check the end of this code for a list of people who contributed to this project
*/

//Robin2's code modified to move arm forward from a home position, other
//mods are in the delay and steps

// "define global variables"

#include <Servo.h> 
 
Servo servoBase;         
Servo servoShoulder; //assign names to the servos       
Servo servoForearm;
 
int servoBasePos = 1500;
int servoShoulderPos = 1500;// variable to store the servo (start) position 
int servoForearmPos = 1500;

void setup() 
{ 
  servoBase.attach(9, 1000, 2000);  // Set servo to pin 9, min, max
  servoShoulder.attach(10, 1000, 2000);  // Set servo to pin 10, min, max
  servoForearm.attach(11, 1000, 2000); // Set servo to pin (11, min, max); 
  
  moveLimbs(); // to the initial position
  
  delay(1000);  // so we can see the initial position
} 


void loop() {
   updateLimbPositions();
   moveLimbs();
   delay(40);
}

void updateLimbPositions() {
   // run in reverse of last code
   servoBasePos -= 11; // microsecs 
   servoShoulderPos -= 11;
   servoForearmPos -= 11;
   if (servoBasePos < 1100) {
       servoBasePos = 1100;
   }
   if (servoShoulderPos < 1100) {
       servoShoulderPos = 1100;
   }
   if (servoForearmPos < 1100) {
       servoForearmPos = 1100;
   }
}

void moveLimbs() {
   servoBase.writeMicroseconds(servoBasePos);
   servoShoulder.writeMicroseconds(servoShoulderPos);
   servoForearm.writeMicroseconds(servoForearmPos);
}





/*
August 24th 2014 
Version 1.0 Firmware development for the open source "Lite Arm" Project:
http://www.thingiverse.com/thing:407800
Contributors:
Senior hardware design, instigator, and co-founder: Lance Pierson www.armatecgroup.org
Design inspiration from the uArm project:
https://www.kickstarter.com/projects/ufactory/uarm-put-a-miniature-industrial-robot-arm-on-your
The U Factory team:
Frank Zhang, Evan Deng, Aler Gu, Loeve Le, James Wang & Eric Wang.

Hardware 3D printing, testing and ebay parts supply:
Mark Jansen (http://www.thingiverse.com/mjansenoh/about) (http://www.ebay.com/itm/Lite-Arm-Open-Source-Arduino-Robotic-Arm-Structural-Parts-Kit-/221527649285?)
Joel Brown (Hardware printing and testing) (http://www.thingiverse.com/chphead24/about)

Code development contributors
Arduino Forum:
Robin2 (Code for initial servomotor smoothing) (http://forum.arduino.cc/index.php?action=profile;u=174714) 
Zoomcat (insiration for GUI) (http://forum.arduino.cc/index.php?action=profile;u=18664)
*/

And a mod to the original code in video 8.24.14_A changing the start and end positions, to make movement a little smoother, and step in sync.

// "define global variables"
#include <Servo.h> 
 
Servo servoBase;         
Servo servoShoulder;     //I assume that I add this the same way    
Servo servoForearm;
 
int servoBasePos = 1100;
int servoShoulderPos = 1100;// variable to store the servo position 
int servoForearmPos = 1100;//if i dont add the above i get thee error below

void setup() 
{ 
  servoBase.attach(9, 1000, 2000);  // Set servo to digital pin 9, min, max
  servoShoulder.attach(10, 1000, 2000);  // Set servo to digital pin 10, min, max
  servoForearm.attach(11, 1000, 2000); // Set servo to digital pin 11, min, max 
  
  moveLimbs(); // to the initial position
  
  delay(1000);  // so we can see the initial position
} 


void loop() {
   updateLimbPositions();
   moveLimbs();
   delay(40);
}

void updateLimbPositions() {
   // this is just simple demo stuff
   servoBasePos += 15; // microsecs 
   servoShoulderPos += 15;
   servoForearmPos += 15;
   if (servoBasePos > 1500) {
       servoBasePos = 1500;
   }
   if (servoShoulderPos > 1500) {
       servoShoulderPos = 1500;
   }
   if (servoForearmPos > 1500) {
       servoForearmPos = 1500;
   }
}

void moveLimbs() {
   servoBase.writeMicroseconds(servoBasePos);
   servoShoulder.writeMicroseconds(servoShoulderPos);
   servoForearm.writeMicroseconds(servoForearmPos);
}

How would I combine these two versions of code to make the arm move as shown in the second video without me uploading two sets of code?

I'm trying to figure out how to get the arm to execute a second move, but I have not gotten anywhere all day except is with this code. The problem is after it slowly reaches out it just jerks to the second position without smooth action like the first.

// "define global variables"

#include <Servo.h> 
 
Servo servoBase;         
Servo servoShoulder; //assign names to the servos       
Servo servoForearm;
 
int servoBasePos = 1500;
int servoShoulderPos = 1500;// variable to store the servo (start) position 
int servoForearmPos = 1500;

void setup() 


{ 
  

  servoBase.attach(9, 1000, 2000);  // Set servo to pin 9, min, max
  servoShoulder.attach(10, 1000, 2000);  // Set servo to pin 10, min, max
  servoForearm.attach(11, 1000, 2000); // Set servo to pin (11, min, max); 
  
  moveLimbs(); // to the initial position
  
  delay(1000);  // so we can see the initial position
  
  
} 


void loop() {
   updateLimbPositions();
   
   moveLimbs();
   
   updatenexpos();
   
   nexpos();
   delay(40);
}

void updateLimbPositions() {
   // run in reverse of last code
   servoBasePos -= 11; // microsecs 
   servoShoulderPos -= 11;
   servoForearmPos -= 11;
   if (servoBasePos < 1100) {
       servoBasePos = 1100;
   }
   if (servoShoulderPos < 1100) {
       servoShoulderPos = 1100;
   }
   if (servoForearmPos < 1100) {
       servoForearmPos = 1100;
   }
}

void moveLimbs() {
   servoBase.writeMicroseconds(servoBasePos);
   servoShoulder.writeMicroseconds(servoShoulderPos);
   servoForearm.writeMicroseconds(servoForearmPos);
}

void updatenexpos() {
   // run in reverse of last code
   servoBasePos +- 11; // microsecs 
   servoShoulderPos +- 11;
   servoForearmPos +- 11;
   if (servoBasePos <= 1100) {
       servoBasePos = 1500;
       
   }
   if (servoShoulderPos <= 1100) {
       servoShoulderPos = 1500;
       
   }
   if (servoForearmPos <= 1100) {
       servoForearmPos = 1500;
       
   }
}

void nexpos() {
   servoBase.writeMicroseconds(servoBasePos);
   servoShoulder.writeMicroseconds(servoShoulderPos);
   servoForearm.writeMicroseconds(servoForearmPos);
}

Thanks for the new video - that does what I expected.

Regarding other moves, you have to realize that the function updateLimbPositions() is only a bodge to demonstrate how the function moveLimbs() interleaves the movement of all 3 servos.

For your real project you need to write code that changes the values of servoBasePos, servoShoulderPos and servoForearmPos as required to get the device to move as you want. Each change in the value should be small to minimize jerks and moveLimbs() needs to be called at a regular interval. In my example it is every 50 msecs - but you will need to experiment to find what combination of move size and move interval suits your device.

In the video of my code the whole thing rushes to its start position. This is not a simple problem to avoid. If you can arrange for the device to be stopped at the "start" position before the Arduino is turned off or reset then there will be no high-speed move and things will look a lot better.

Look at the Blink Without Delay example and this demo several things at a time to see how to use millis() to avoid the use of delay(). The Arduino can do an awful lot of stuff in 50 msecs so you don't want it sitting waiting for delay(50) to elapse. Also it would be good to change to using millis() before you do any serious development of your project because, while millis() is easy to use, changing complex code from delay() to millis() is not simple.

...R

@Robin2 Thank you for the advice. I purchased a copy of Getting Started With Arduino in PDF format, I will study that to get a better understanding of exactly what you are saying. Then I will ask more questions when I can understand better the syntax.
Best,
Lance-

Getting Started With Arduino
https://thingiverse-production.s3.amazonaws.com/assets/a5/06/33/1b/8a/Getting_Started_with_Arduino_Second_Edition.pdf

You may also be interested in this Thread Planning and Implemeting an Arduino Program

Have fun.

...R

Just looking at some saved links I came across the below. Might be worth a look for controlling servo movement speed.

Can someone please fill me in on something here? I thought Servos went from 0-180 degrees, but all of the code here puts 1000-2000 as the range? I'm also trying to slow move my robot arm

Leigh:
Can someone please fill me in on something here? I thought Servos went from 0-180 degrees, but all of the code here puts 1000-2000 as the range? I'm also trying to slow move my robot arm

1000 to 2000 is the range for Servo.writeMicroseconds(). That range of microseconds corresponds roughly with 0 - 180 degrees. You will find lots of web pages that explain how servos are controlled.

...R