ROBOTIC HAND - HELP FIND SERVO LAG! HELP! (add karma for all)

I am making a robotic hand for my “science” project, 11th grade, im in the finishing stages, I already have the whole system of a hand installed, with strings that go though each finger, i have 5 servos that pull on these strings to flex the fingers.my goal was that i have created and arduino program to pull or flex everytime i hold a button and quickly return back to degree 0 after release, since i have 5 buttons and 5 servos my arduino board is filled with wires crossing each other.

so i have 5 push-buttons connected from 8-12 and 5 servos connected from 1-5 and each button is connected tot he consecutive servo, for example button 8 is designated for servo1 and button 9 is designated for servo2.

Anyways, Iam new to Arduino and I have small experience, Whenever i run this program, some of my servos start lagging, iam not sure if the servos are cheap(tower pro 9g sg90) or my program is too much for the arduino to catch up with. The lagging occurs when i press the button the servo spins for 5 degres, stops the computer makes a duh duh noise (the sound when you disconnect a flash drive)and the spins for another 5 degrees and repeats.The problem may be that i have A LOT of wiring on my small bread board because when i disconnect the 4 servos, the one servo that i have works fine.

Please help me compress the program or tell me what is wrong with my project! Here it is
Thank you soo much
(file attached below)

#include <Servo.h>
int press = 1;

/*Finger 1 */
int button1 = 8; //button 1
Servo servo1;// servo 1

/*Finger 2 */
int button2 = 9; //button 2
Servo servo2;// servo 2

/*Finger 3 */
int button3 = 10; // button 3 and ETC...
Servo servo3;

/*Finger 4 */
int button4 = 11; 
Servo servo4;

/*Finger 5 */
int button5 = 12; 
Servo servo5;


void setup()
{
 /*Finger 1 */
 pinMode(button1, INPUT); //arduino monitor pin state
 servo1.attach(1); // I want it to hold the string in position (attached)
 digitalWrite(8, HIGH); //enable pullups to make pin high

/*Finger 2 */
 pinMode(button2, INPUT); button 2 etc...
 servo2.attach(2);
 digitalWrite(9, HIGH); 

/*Finger 3 */
 pinMode(button3, INPUT); //arduino monitor pin state
 servo3.attach(3); //pin for servo control signal
 digitalWrite(10, HIGH); //enable pullups to make pin high

/*Finger 4 */
 pinMode(button4, INPUT); 
 servo4.attach(4);
 digitalWrite(11, HIGH); 

/*Finger 5 */
 pinMode(button5, INPUT);
 servo5.attach(5); 
 digitalWrite(12, HIGH); 
}

void loop()
{
// If Then logic statement Translation = If press spin 180* if not press spin to 0*

 /*Finger 1 */
 press = digitalRead(button1);
 if (press == LOW)
 {
     servo1.write(180);
   }
   else
   {
     servo1.write(0);
   }

/*Finger 2 */
press = digitalRead(button2);
 if (press == LOW)
 {
     servo2.write(180);
   }
   else
   {
     servo2.write(0);
   }

/*Finger 3 */
press = digitalRead(button3);
 if (press == LOW)
 {
     servo3.write(180);
   }
   else
   {
     servo3.write(0);
   }

/*Finger 4 */
press = digitalRead(button4);
 if (press == LOW)
 {
     servo4.write(180);
   }
   else
   {
     servo4.write(0);
   }

/*Finger 5 */
press = digitalRead(button5);
 if (press == LOW)
 {
     servo5.write(180);
   }
   else
   {
     servo5.write(0);
   }

}

PBL_Final_Servo.ino (2.1 KB)

The computer goes du-duh? You are powering this from a computer's USB port? The servos are probably taking too much current and the computer is disconnecting the Arduino because it's not allowed to take that amount of current from a USB.

Please tell us you have an external power supply or battery to run the servos.

You can compact the code by simply using a few arrays.

Thus

#include <Servo.h>
int button[5] = {1,2,3,4,5};
int servoPin[5] = {8,9,10,11,12}; 
Servo servo[5];// servo objects

void setup()
{
for (int n=0;n<5;n++)
 {
  pinMode(button[n], INPUT); 
  servo[n].attach(servoPin[n]);
  digitalWrite(servoPin[n], HIGH); 
  }
}

void loop()
{
for(int n=0;n<5;n++)
  {
    if(digitalRead(button[n])==LOW)
     {
      servo[n].write(180);
     }
   else
     {
     servo[n].write(0);
     }  
  }
}

MorganS: The computer goes du-duh? You are powering this from a computer's USB port? The servos are probably taking too much current and the computer is disconnecting the Arduino because it's not allowed to take that amount of current from a USB.

Please tell us you have an external power supply or battery to run the servos.

Alright i tried a 9volt and it still does the same thing, and it is even less powerful. some how one of the servos

Is there a possibility that the servos are cheap and that is why they do this. thought it had its own mind and spun by itslef.

Is there a possibility that the servos are cheap and that is why they do this.

No, servos take a lot of power to operate, more than the USB port or a small 9v battery can supply. At a minimum you probably need four AA alkaline batteries, or five rechargable AA batterys. A 6v wall wart would also probably work.

Yes i have connected a 9V with a usb, i seem to get a whole lot of less lag on the servos

Thank you soo much! But the problem is i still cant seem to hold two buttons and flex two servos at the same time.

Make a drawing of how things are wired up and post a photo of it.

Why have you anything connected to pin 1? Pins 0 and 1 are used by Serial for communication with your PC via the USB cable.

You can use the analog pins as normal digital pins.

The way your code is written it is impossible to move two fingers at once because the movement for one finger has to finish before the movement for another can start.

You need to separate reading the buttons from moving the servos. Read all the buttons and save the values.
Then move all the servos according to the saved numbers.

Something like

byte buttonState[5];

void loop() {
   readButtons();
   moveServos();
   delay(100); // to allow servos time to move - may need adjusting
}

void readButtons() {
   for (n = 0; n < 5; n++) {
       buttonState[n] = digitalRead(button[n]);
   }
}

void moveServos() {
    for (n = 0; n < 5; n++) {
      if (buttonState[n] == HIGH) {
         servo[n].write(180);
      }
      else {
        servo[n].write(0);
      }
   }
}

…R

Yes i have connected a 9V with a usb,

That doesn't make any sense to me. If one servo works, but trying two doesn't, then you may still have insufficient power. I assume the servos are under significant load when they try to bend the fingers. You may also have wiring issues, such as insufficient grounding between the servo power supplies and the arduino.

Alright i tried a 9volt and it still does the same thing, and it is even less powerful. some how one of the servos

What was the 9v connected to ? (the ext power barrel jack ?) Forget using 9V smoke alarm batteries. Don't ever use those for anything again unless it is just 50 mA or less if you want to avoid problems. Use AA battery packs for most projects and C or D battery packs for motor projects or anything that draws a lot of current. If you can't afford to replace batteries all the time you can switch to RC lipo batteries . A 2S 1350mAh lipo outputs 8.4V dc fully charged. You should not run it below 7.8Vdc before recharging it or you will damage it. I agree you servos are pulling your USB port down . Post a schematic of how you wired them.

Robin2: Make a drawing of how things are wired up and post a photo of it.

Here you go

Here you go

You are still trying to power your servos thru the arduino, which will not work. You need a separate power supply for the servos. also a small 9v battery is not for powering servos. At a minimum you need a strong 6v wall wart or four AA or larger batteries.

Referring to Reply #9.

What you have posted is NOT a drawing. I asked for a photo of drawing.

A drawing (however crude) is much more informative than fancy pictures. For example we have no way to know how the connections are made inside your breadboard image and all breadboards are not the same. And there is such a mess of crossed-over lines in some places that it is impossible to see some stuff.

As @Zoomkat said you MUST give the servos a separate power supply. I'm sure that was said previously also.

Did you read the rest of Reply #6 ?

...R

What you seem to be missing, Ahmet, is that providing an extra power supply to the Arduino, ie both USB and say 9V to the jack, does not provide any more current to your servos.

You need to give them 6V of their own, as shown below for only 3 servos.

servo power.png

:sunglasses:

Finally finished the project.

My competition is online so here is the video I made explaining the hand

https://www.youtube.com/watch?v=cX-7AlCGVUU&feature=youtu.be

Please please like the videos because it gains me points for my competition. and please comment or reply to me with suggestions on what to improve on my video.

(connected the usb power to a higher current, yes you were all right i had a miss -use of power)

ahmet446: i had a miss -use of power)

And history is full of example of what happens then..... 8)

Well done, glad you got it sorted out.

By the way, what brings the fingers back? I don't see reverse strings: is it just the "springiness" of the material you made the hand from? (What is that stuff, anyway?)

I like the whole package: the clear plastic box with the hand on front, hiding the wires and stuff yet still accessible to the camera and presumably those attending the event. Nice video. That's a good project: I think you should post your YouTube link here.