using leap motion to control servo in arduino

Hello everyone, I am a Arduino beginner.

I uses Leap Motion to simulate real persons finger action to control Arduino Mega 2560 microcontrollers manipulator, and I'm using Processing 3.2.3 and import de.voidplus.leapmotion for Leap Motion sensor.

Leap Motion will detect my finger and send messages to Arduino constantly. My question is, Arduino sends massive messages to the servomotor to process, causing the manipulator can't synchronize with my hand. Although it will save the datas in Serial.read and extract one character at a time to choose for my case statement, but the delay setting in the statement will make the synchronize between the electronic signal and the manipulator can not be reached.
If I set the delay too short or simply not set the delay, the motor will keep getting the same message and will not be able to move smoothly. In this aspect, should i do a communication management? or use solutions which there will be no delays such as using Millis()? but how should I execute it?

[/my code]
#include <Servo.h>

int thumb = 4; //define the pin of the five fingers
int index = 5;
int middle = 6;
int ring = 7;
int pinky = 8;
int finger=Serial.available(); // variable to store the servo position

Servo myservo; // create Servo objects to control the servo motor

void setup() {
myservo.attach(4);
myservo.attach(5);
myservo.attach(6);
myservo.attach(7);
myservo.attach(8);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
Serial.begin(9600);
delay(500);
}

void loop()
{
finger = 0;
if (Serial.available() > 0)//check
{
finger = Serial.read(); //load and Save the variable

switch (finger) {
case 84:
//ASCII code 84('T')Thumb
//Serial.println("Thumb");
myservo.attach(4); // Connect the servo motor to the servo object on pin 4
myservo.write(5); // Tell the servo motor to rotate the position
delay(20);

break;
case 116: // close_Thumb
//ASCII code 116('t')
myservo.attach(4); //Connect the servo motor to the servo object on pin 4
myservo.write(175);
delay(20);

break;

case 73:
//ASCII code 73('I')Index
myservo.attach(5); // Connect the servo motor to the servo object on pin 5
myservo.write(5);
delay(20);

break;
case 105: // close_Index
//ASCII code 105('i')
myservo.attach(5); // Connect the servo motor to the servo object on pin 5
myservo.write(175);
delay(20);

break;

case 77:
//ASCII code 77('M')Middle
myservo.attach(6); // Connect the servo motor to the servo object on pin 6
myservo.write(5); /
delay(20);

break;
case 109: // close_Middle
//ASCII code 109('m')
myservo.attach(6); // Connect the servo motor to the servo object on pin 6
myservo.write(175);
delay(20);

break;

case 82:
//ASCII code 82('R')Ring
myservo.attach(7); // Connect the servo motor to the servo object on pin 7
myservo.write(5);
delay(20);

break;
case 114: // close_Ring
//ASCII code 114('r')
myservo.attach(7); // Connect the servo motor to the servo object on pin 7
myservo.write(175);
delay(20);

break;

case 80:
//ASCII code 80('P')Pinky
myservo.attach(8); // Connect the servo motor to the servo object on pin 8
myservo.write(5);
delay(20);

break;
case 112: // close_Pinky
//ASCII code 112('p')
myservo.attach(8); // Connect the servo motor to the servo object on pin 8
myservo.write(180);
delay(20);

break;

}//end of switch
}//end of if
}//end of loop

You are only creating 1 servo object and then attaching it to various pins to talk to your various physical servos.
This is not how I did it when I talked to multiple servos.

I created one servo object per servo, attached them in setup and then never attached again.
Also, you went to the trouble to declare variables for the various pins and then never use them.
Also, you are using delays in the loop. Why? Get rid of them. Read blink without delay or various do many things at once tutorials to help with understanding why.

Here is an uncompiled attempt at some of these.

#include <Servo.h>

int thumb = 4;             //define the pin of the five fingers
int index = 5;
int middle = 6;
int ring = 7;
int pinky = 8;
int finger=Serial.available();    // variable to store the servo position

Servo myservoThumb;   // create Servo objects to control the servo motors
Servo myservoIndex;
Servo myservoMiddle;
Servo myservoRing;
Servo myservoPinky;

void setup() {
  myservoThumb.attach(thumb);
  myservoIndex.attach(index);
  myservoMiddle.attach(middle);
  myservoRing.attach(ring);
  myservoPinky.attach(pinky);
  pinMode(thum, OUTPUT);
  pinMode(index, OUTPUT);
  pinMode(middle, OUTPUT);
  pinMode(ring, OUTPUT);
  pinMode(pinky, OUTPUT);
  Serial.begin(9600);
  delay(500);
}

void loop()
{
finger = 0;
  if (Serial.available() > 0)//check
  {
    finger = Serial.read(); //load and Save the variable
         
switch (finger) {
      case 84:
        //ASCII code 84('T')Thumb
        //Serial.println("Thumb");
        myservoThumb.write(5);             // Tell the servo motor to rotate the position                             

        break;
     case 116:  // close_Thumb
        //ASCII code 116('t')
        myservoThumb.write(175);                                 

        break;

      case 73:
        //ASCII code 73('I')Index
        myservoIndex.write(5);                                     

        break;
      case 105:  // close_Index
        //ASCII code 105('i')
        myservoIndex.write(175);             

        break;
       
      case 77:
        //ASCII code 77('M')Middle 
        myservoMiddle.write(5);            

        break;
      case 109:  // close_Middle
        //ASCII code 109('m')
        myservoMiddle.write(175);           

        break;
       
      case 82:
        //ASCII code 82('R')Ring
        myservoRing.write(5);                                     

        break;
      case 114:  // close_Ring
        //ASCII code 114('r')
        myservoRing.write(175);           

        break;
       
      case 80:
        //ASCII code 80('P')Pinky  
        myservoPinky.write(5);                                   

        break;
      case 112:  // close_Pinky
        //ASCII code 112('p')
        myservoPinky.write(180);             
 
        break;
       
      }//end of switch
    }//end of if
  }//end of loop

Vinceherman thank you for your answer and help,
I tried to read the tutorials of blink without delay or various do many things and found out in "Demonstration code for several things at the same time - Project Guidance - Arduino Forum" mentioned "The Arduino is not capable of supplying enough 5v power to operate a servo", currently I'm trying to use an External power supply to power the servo motor.Though i do not really understand currentMillis, previousServoMills, servoInterval ..... , but I've tried to add them to my code, Is there anything I can improve?

here is my new code

#include <Servo.h>

const int thumb = 4;             // the pin numbers for the Servos
const int index = 5;
const int middle = 6;
const int ring = 7;
const int pinky = 8;
int finger;
const int movementDuration = 500;  //// number of millisecs that Servo's are on - all five servos use this
const int servoMinDegrees = 5;  // the limits to servo movement
const int servoMaxDegrees = 175;

Servo myservoThumb;   // create Servo objects to control the servo motors
Servo myservoIndex;
Servo myservoMiddle;
Servo myservoRing;
Servo myservoPinky;

int servoPosition = 90;     // the current angle of the servo - starting at 90.
int servoSlowInterval = 80; // millisecs between servo moves
int servoFastInterval = 10;
int servoInterval = servoSlowInterval; // initial millisecs between servo moves

int servoDegrees = 2;       // amount servo moves at each step 
                           //    will be changed to negative value for movement in the other direction
unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()
unsigned long previousServoMillis = 0; // the time when the servo was last moved                           

void setup() {
  myservoThumb.attach(thumb);
  myservoIndex.attach(index);
  myservoMiddle.attach(middle);
  myservoRing.attach(ring);
  myservoPinky.attach(pinky);

  Serial.begin(9600);
  delay(500);
}

void loop() {

 currentMillis = millis();   // capture the latest value of millis()
                             //   this is equivalent to noting the time from a clock
                             //   use the same time for all LED flashes to keep them synchronized
 servoSweep();

}


void servoSweep(){
      // this is similar to the servo sweep example except that it uses millis() rather than delay()
     // nothing happens unless the interval has expired
     // the value of currentMillis was set in loop()
     
  if (currentMillis - previousServoMillis >= servoInterval) {     // its time for another move
       
       
   previousServoMillis += servoInterval;
   servoPosition = servoPosition + servoDegrees; // servoDegrees might be negative
   if (servoPosition <= servoMinDegrees) {      // when the servo gets to its minimum position change the interval to change the speed
         
      if (servoInterval == servoSlowInterval) {
        servoInterval = servoFastInterval;
      }
         else {
       servoInterval = servoSlowInterval;
      }
   }
   if ((servoPosition >= servoMaxDegrees) || (servoPosition <= servoMinDegrees))  {  // if the servo is at either extreme change the sign of the degrees to make it move the other way
         
     servoDegrees = - servoDegrees; 
     
          // reverse direction
         // and update the position to ensure it is within range
         
     servoPosition = servoPosition + servoDegrees; 
   }
       // make the servo move to the next position
       // and record the time when the move happened
  finger = 0;
  if (Serial.available() > 0)//check
  {
    finger = Serial.read(); //load and Save the variable
         
switch (finger) {
      case 84:
        //ASCII code 84('T')Thumb
        //Serial.println("Thumb");
        myservoThumb.write(servoPosition);             // Tell the servo motor to rotate the position                             

        break;
     case 116:  // close_Thumb
        //ASCII code 116('t')
        myservoThumb.write(servoPosition);                                 

        break;

      case 73:
        //ASCII code 73('I')Index
        myservoIndex.write(servoPosition);                                     

        break;
      case 105:  // close_Index
        //ASCII code 105('i')
        myservoIndex.write(servoPosition);             

        break;
       
      case 77:
        //ASCII code 77('M')Middle 
        myservoMiddle.write(servoPosition);            

        break;
      case 109:  // close_Middle
        //ASCII code 109('m')
        myservoMiddle.write(servoPosition);           

        break;
       
      case 82:
        //ASCII code 82('R')Ring
        myservoRing.write(servoPosition);                                     

        break;
      case 114:  // close_Ring
        //ASCII code 114('r')
        myservoRing.write(servoPosition);           

        break;
       
      case 80:
        //ASCII code 80('P')Pinky  
        myservoPinky.write(servoPosition);                                   

        break;
      case 112:  // close_Pinky
        //ASCII code 112('p')
        myservoPinky.write(servoPosition);             
 
        break;
       
    }
   }
  }
 }

I have just tried to rewrite the code, maybe both are wrong, but I still posted out my code because I hope I can find a way to improve the error.

Servo myservoThumb;   // create Servo objects to control the servo motors
Servo myservoIndex;
Servo myservoMiddle;
Servo myservoRing;
Servo myservoPinky;

long elapsed_time = 0;
long previousMillis = 0;
long previousMillis2 = 0;
long servointerval = 15;
long interval = 100;

void setup() {
  myservoThumb.attach(thumb);
  myservoIndex.attach(index);
  myservoMiddle.attach(middle);
  myservoRing.attach(ring);
  myservoPinky.attach(pinky);

  Serial.begin(9600);
  delay(500);
}

void loop() {
 
  finger = 0;
  if (Serial.available() > 0)//check
  {
      unsigned long Starttimer=millis();      //starts one second delay timer?
         do {
            unsigned long current_time = millis();
            elapsed_time = current_time - Starttimer;
            }
            while(elapsed_time < interval);      //leaves loop when 1 second has passed
    finger = Serial.read(); //load and Save the variable
         
switch (finger) {
      case 84:
        //ASCII code 84('T')Thumb
        //Serial.println("Thumb");
        for(pos = 175; pos >=175; pos-=175) { 
          unsigned long servoMillis = millis();      
        if(servoMillis - previousMillis > servointerval)  //created to delay 15 milliseconds before next pulse
        {
         previousMillis = servoMillis;
         myservoThumb.write(pos);             // Tell the servo motor to rotate the position      
        }
        break;
        }
     case 116:  // close_Thumb
        //ASCII code 116('t')
        for(pos = 0; pos <= 175; pos+=175){
          unsigned long servoMillis2 = millis();
          if(servoMillis2 - previousMillis2 > servointerval)
          {
            previousMillis2 = servoMillis2;
            myservoThumb.write(pos);                                
          }
          break;
        }

        
      case 73:
        //ASCII code 73('I')Index
        for(pos = 175; pos >=175; pos-=175) {  
          unsigned long servoMillis = millis();     
        if(servoMillis - previousMillis > servointerval)  //created to delay 15 milliseconds before next pulse
        {
         previousMillis = servoMillis;
         myservoIndex.write(pos);             // Tell the servo motor to rotate the position      
        }
        break;
        }
                                                         

        
      case 105:  // close_Index
        //ASCII code 105('i')
        for(pos = 0; pos <= 175; pos+=175){
          unsigned long servoMillis2 = millis();
          if(servoMillis2 - previousMillis2 > servointerval)
          {
            previousMillis2 = servoMillis2;
            myservoIndex.write(pos);                                
          }
           break;
        }            

       
       
      case 77:
        //ASCII code 77('M')Middle 
        for(pos = 175; pos >=175; pos-=175) {   
          unsigned long servoMillis = millis();    
        if(servoMillis - previousMillis > servointerval)  //created to delay 15 milliseconds before next pulse
        {
         previousMillis = servoMillis;
         myservoMiddle.write(pos);             // Tell the servo motor to rotate the position      
        }
         break;
        }            
 
      case 109:  // close_Middle
        //ASCII code 109('m')
        for(pos = 0; pos <= 175; pos+=175){
          unsigned long servoMillis2 = millis();
          if(servoMillis2 - previousMillis2 > servointerval)
          {
            previousMillis2 = servoMillis2;
            myservoMiddle.write(pos);                                
          }
          break;
        }         

        
       
      case 82:
        //ASCII code 82('R')Ring
        for(pos = 175; pos >=175; pos-=175) {  
          unsigned long servoMillis = millis();     
        if(servoMillis - previousMillis > servointerval)  //created to delay 15 milliseconds before next pulse
        {
         previousMillis = servoMillis;
         myservoRing.write(pos);             // Tell the servo motor to rotate the position      
        }
         break;
       }                                       
  
      case 114:  // close_Ring
        //ASCII code 114('r')
        for(pos = 0; pos <= 175; pos+=175){
          unsigned long servoMillis2 = millis();
          if(servoMillis2 - previousMillis2 > servointerval)
          {
            previousMillis2 = servoMillis2;
            myservoRing.write(pos);                                
          }
           break;
        }           

       
       
      case 80:
        //ASCII code 80('P')Pinky  
       for(pos = 175; pos >=175; pos-=175) {   
          unsigned long servoMillis = millis();   
        if(servoMillis - previousMillis > servointerval)  //created to delay 15 milliseconds before next pulse
        {
         previousMillis = servoMillis;
         myservoPinky.write(pos);             // Tell the servo motor to rotate the position      
        }
         break;
       }                                      

        
      case 112:  // close_Pinky
        //ASCII code 112('p')
        for(pos = 0; pos <= 175; pos+=175){
          unsigned long servoMillis2 = millis();
          if(servoMillis2 - previousMillis2 > servointerval)
          {
            previousMillis2 = servoMillis2;
            myservoPinky.write(pos);                                
          }
          break;
        }             
 
        
       
    }
   }
  }

I hope I can find a way to improve the error.

What is the error? Do you want to improve it or eliminate it?

         do {
            unsigned long current_time = millis();
            elapsed_time = current_time - Starttimer;
            }
            while(elapsed_time < interval);      //leaves loop when 1 second has passed

There is already a function to do that. It's called delay(). Just because you wrote your own blocking function, but didn't give it a name does not make it better than using delay().

      case 84:
        //ASCII code 84('T')Thumb

or

    case 'T':
    // No useless comment needed
        for(pos = 175; pos >=175; pos-=175) {

WTF? How many times is that loop going to iterate? Why do you need a for loop that will iterate once?