5 Servo Control for Robot ARm

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:

#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)

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?

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

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.

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 -

Duane B

rcarduino.blogspot.com

Thanks for the help.
Its looking a lot better.