I have 4 servos running on an arduino as a robotic arm. They are being powered by the usb, with only 1 servo operating at a time.
At certain points two of the servos start convulsing and shaking and the usb/com port connects and disconnect every second, then it requires unplugging/resetting the arduino several times just to get it back to normal.
I'm not sure if the servos can't handle the weight of the arm, if it's a power issue, or perhaps the serial connection with something in my code.
(note: the serial connection is with a C# program giving the 1-8 commands).
Anyone have any ideas?
Thanks in advance.
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
// a maximum of eight servo objects can be created
Servo myservo2;
Servo myservo3;
Servo myservo4;
byte incomingByte; // a variable to read incoming serial data into
uint8_t commandIn;
int spd = 15;
int servo1_pos = 0;
int servo2_pos = 0;
int servo3_pos = 0;
int servo4_pos = 0;
int pos = 0; // variable to store the servo position
void setup()
{
Serial.begin(57600);
myservo1.attach(9); // attaches the servo on pin 8 to the servo object
myservo2.attach(8); // attaches the servo on pin 9 to the servo object
myservo3.attach(10); // attaches the servo on pin 9 to the servo object
myservo4.attach(11); // attaches the servo on pin 9 to the servo object
// delay(500);
// myservo1.write(90);
// delay(500);
// myservo2.write(90);
// delay(500);
// myservo3.write(90);
// delay(500);
// myservo4.write(90);
// delay(500);
}
void loop()
{
if (Serial.available() > 0)
{
commandIn = Serial.read();
switch (commandIn)
{
case '1':
{
for (pos = servo1_pos; pos < servo1_pos + 5; pos++)
{
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(spd);
}
servo1_pos += 5;
break;
}
case '2':
{
for (pos = servo1_pos; pos > servo1_pos - 5; pos--)
{
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(spd);
}
servo1_pos -= 5;
break;
}
case '3':
{
for (pos = servo2_pos; pos < servo2_pos + 5; pos++)
{
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(spd);
}
servo2_pos += 5;
break;
}
case '4':
{
for (pos = servo2_pos; pos > servo2_pos - 5; pos--)
{
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(spd);
}
servo2_pos -= 5;
break;
}
case '5':
{
for (pos = servo3_pos; pos < servo3_pos + 10; pos++)
{
myservo3.write(pos); // tell servo to go to position in variable 'pos'
delay(spd);
}
servo3_pos += 10;
break;
}
case '6':
{
for (pos = servo3_pos; pos > servo3_pos - 10; pos--)
{
myservo3.write(pos); // tell servo to go to position in variable 'pos'
delay(spd);
}
servo3_pos -= 10;
break;
}
case '7':
{
for (pos = servo4_pos; pos < servo4_pos + 10; pos++)
{
myservo4.write(pos); // tell servo to go to position in variable 'pos'
delay(spd);
}
servo4_pos += 10;
break;
}
case '8':
{
for (pos = servo4_pos; pos > servo4_pos - 10; pos--)
{
myservo4.write(pos); // tell servo to go to position in variable 'pos'
delay(spd);
}
servo4_pos -= 10;
break;
}
}
}
}