Go Down

### Topic: 5 Servo Control for Robot ARm (Read 5538 times)previous topic - next topic

#### ehunck

##### Jan 27, 2012, 10:24 pmLast Edit: Feb 02, 2012, 02:00 pm by AWOL Reason: 1
I made a robot arm using 5 servo motors.  I'm looking for a program to run the arm.
I need to be able to control the arm.
I have 5 spst switches and one 10k potentiometer.

What I had in mind was to turn on one switch and control one servo at a time.

I am using an Arduino Uno, an 8-position DIP switch (Radio Shack), 5x 10k resistors, 5x HKSCM16-5 Single Chip Digital Servo (5V) Mini servos, and 1 10k potentiometer.

-The setup is each switch connected to the positive 5v and then to a 10 k resistor then to the ground.  In between the switch and resistor is a wire that runs to a digital input pin.
-The potentiometer is connected to the positive 5v, the ground, and an analog in.
-The servos are each connected to the positive 5v, the ground, and a pwm digital pin.

I am a beginner at writing the code so I tried to use different programs that I had seen before.

The code I used was:

Code: [Select]
`#include <Servo.h>Servo servo1;Servo servo2;Servo servo3;Servo servo4;Servo servo5;int potpin = 0;int switch1 = 1;int switch2 = 2;int switch3 = 4;int switch4 = 7;int switch5 = 8;int valp;int val1 = 0;int val2 = 0;int val3 = 0;int val4 = 0;int val5 = 0;void setup(){  servo1.attach(5);  servo2.attach(6);  servo3.attach(9);  servo4.attach(10);  servo5.attach(11);}void loop(){  val1 = digitalRead(switch1);  if (val1 == HIGH){    valp = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)    valp = map(valp, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)    servo1.write(valp);                  // sets the servo position according to the scaled value    delay(15);  }  val2 = digitalRead(switch2);  if (val2 == HIGH){    valp = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)    valp = map(valp, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)    servo2.write(valp);                  // sets the servo position according to the scaled value    delay(15);  }  val3 = digitalRead(switch3);  if (val3 == HIGH){    valp = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)    valp = map(valp, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)    servo3.write(valp);                  // sets the servo position according to the scaled value    delay(15);  }  val4 = digitalRead(switch4);  if (val4 == HIGH){    valp = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)    valp = map(valp, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)    servo4.write(valp);                  // sets the servo position according to the scaled value    delay(15);  }  val5 = digitalRead(switch5);  if (val5 == HIGH){    valp = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)    valp = map(valp, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)    servo5.write(valp);                  // sets the servo position according to the scaled value    delay(15);  }}  `
Any help in the code or wiring would be great.

#### AWOL

#1
##### Jan 27, 2012, 10:36 pm
Does it work?
If yes, no worries.
Otherwise, what does it do that it shouldn't, or what doesn't it do that it should?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### zoomkat

#2
##### Jan 27, 2012, 11:28 pm
Quote
-The servos are each connected to the positive 5v, the ground, and a pwm digital pin.

If you are trying to power the servos from the arduino 5v, you may have problems. Best to use a seperate power supply.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

#### AWOL

#3
##### Jan 27, 2012, 11:52 pm
The servo library doesn't require you to use PWM pins.
In fact, you should read the documentation for the library regarding the use of the library and PWM on pins 9 and 10 on boards other than the Mega.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### DuaneB

#4
##### Feb 02, 2012, 01:48 pm
Hi,
I See a problem with your plan. When you push a servo button, that servo will immediately move to the position you set for the last servo. This could easily unintentionally break your robot.

I would suggest using 10 buttons, they will give more control than a pot and should stop you from accidentally driving the arm into the table.

Maybe something like this

Base rotation +
Base rotation -

Arm elevation +
Arm elevation -

and so on for your other servos

From what I can see you have most of the servo library grasped, but just incase you need to free up some of the PWM pins for analogWrite at a later stage, you can -

http://rcarduino.blogspot.com/2012/01/can-i-control-more-than-x-servos-with.html

Duane B

rcarduino.blogspot.com
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

#### ehunck

#5
##### Feb 07, 2012, 12:24 am
Thanks for the help.
Its looking a lot better.

Go Up

Please enter a valid email to subscribe