Problem in servo movement by using an SD card

Hello everyone,
ill need some issues if its possible for a project that i work. i have to make a robotic arm with 6DOF. i have an arduino mega adk, 6 servos that i have plugged in a fourth wire to the potentiometer inside to have the feedback information. An sd card to store the values(angle of servo) an external power supply for the servos and joystick with some buttons to control the motion. The cases that i want to do is 1st work it manually(joystick) and 2nd record the moves of the manual use and play it back automatically. The problem that i met is that when i combine the control program with the sd program the servo moves very slow (problem with his speed) the write on the sd works correctly. ill post my code so you have more specific information. Thank you for your time, i will appreciate any help!!

Code:

#include <SPI.h>
#include <SD.h>
#include <Servo.h>

Servo servo1;
const int chipSelect = 53;
int pin0=A2;
int pin1=A4;
int angle;
int value1;
int value2;
int feedback;
long int forservo1=10000;
Servo servo2;
int pin3=A5;
int angle1;
int value3;
int value4;
int feedback1;
long int forservo2=10000;

const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin2 = 4;

// Variables will change:
int buttonState; // the current reading from the input pin
int lastButtonState = 0; // the previous reading from the input pin
int buttonPushCounter = 0;

// Variables will change:
int buttonState2; // the current reading from the input pin
int lastButtonState2 = 0; // the previous reading from the input pin
int buttonPushCounter2 = 0;

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime2 = 0; // the last time the output pin was toggled
unsigned long debounceDelay2 = 50; // the debounce time; increase if the output flickers

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers

void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);

Serial.begin(9600);

while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

Serial.print(“Initializing SD card…”);

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present”);
// don’t do anything more:
while (1);
}
Serial.println(“card initialized.”);

}

void loop() {

debcount();

if (buttonPushCounter==2){
motion1();
grapse();
} else{

servo1.detach();

}
if (buttonPushCounter==3){
motion2();
} else{

servo2.detach();

}
if (buttonPushCounter==4){
SD.remove(“pos.txt”);
}else{
}
}

void debcount(){
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);

// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you’ve waited long enough
// since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it’s been there for longer than the debounce
// delay, so take it as the actual current state:

// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;

// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {

// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println(“on”);
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println(“off”);
}
// Delay a little bit to avoid bouncing
delay(100);
}
}

// save the reading. Next time through the loop, it’ll be the lastButtonState:
lastButtonState = reading;

if(buttonPushCounter>=6){
buttonPushCounter=0;
}

}

void debcount2(){
// read the state of the switch into a local variable:
int reading2 = digitalRead(buttonPin2);

// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you’ve waited long enough
// since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:
if (reading2 != lastButtonState2) {
// reset the debouncing timer
lastDebounceTime2 = millis();
}

if ((millis() - lastDebounceTime2) > debounceDelay2) {
// whatever the reading is at, it’s been there for longer than the debounce
// delay, so take it as the actual current state:

// if the button state has changed:
if (reading2!= buttonState2) {
buttonState2 = reading2;

// only toggle the LED if the new button state is HIGH
if (buttonState2 == HIGH) {

// if the current state is HIGH then the button went from off to on:
buttonPushCounter2++;
//Serial.println(“on”);
//Serial.print("number of button pushes: ");
//Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
//Serial.println(“off”);
}
// Delay a little bit to avoid bouncing
delay(100);
}
}

// save the reading. Next time through the loop, it’ll be the lastButtonState:
lastButtonState2 = reading2;

if(buttonPushCounter2>=6){
buttonPushCounter2=0;
}

}

void motion1(){
servo1.attach(6);
value1=analogRead(pin0);
value2=analogRead(pin1);
feedback=map(value2,90,405,0,150);
//Serial.println(feedback);
value1=map(value1,0,1023,1,29);
if (value1 <= 17 && value1 >=13){
value1=15;
}
forservo1=forservo1+ (value1-15);

if (forservo1<1){
forservo1=1;}
if (forservo1>20000){
forservo1=20000;}

angle= map(forservo1,1,20000,0,150);

servo1.write(angle);
delay(15);
}
void motion2(){

servo2.attach(5);
value3=analogRead(pin0);
value4=analogRead(pin3);
feedback1=map(value4,50,584,0,180);
//Serial.println(feedback1);
value3=map(value3,0,1023,1,29);

if (value3 <= 17 && value3 >=13){
value3=15;
}
forservo2=forservo2+ (value3-15);

if (forservo2<1){
forservo2=1;}
if (forservo2>20000){
forservo2=20000;}

angle1= map(forservo2,1,20000,0,180);
servo2.write(angle1);
delay(15);
}
void grapse(){
// make a string for assembling the data to log:
String dataString = “”;

// Read pot value and append to the string
// Map to range of 0-180 for servo

dataString += String(feedback);

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open(“pos.txt”, FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn’t open, pop up an error:
else {
Serial.println(“error opening pos.txt”);
}

}