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)