Pages: [1]   Go Down
Author Topic: 5 Servo Control for Robot ARm  (Read 2905 times)
0 Members and 1 Guest are viewing this topic.
US
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.

Moderator: Code tags added.

* DIP_switch_5_servo_pot_control.ino (2.29 KB - downloaded 28 times.)
« Last Edit: February 02, 2012, 08:00:06 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24317
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8908
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24317
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


US
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the help.
Its looking a lot better.
Logged

Pages: [1]   Go Up
Jump to: