executing sequence after manual control of servo

I am new to this forum…can anyone help me with this project…We are trying to control four servos with bluetooth module which executes sequence of command given to it using while loop but we keep getting troubles…It would be a great help if anyone could help us with executing sequence given to control motor.

here is the program.

#include <Servo.h>
void aman(int);
int x[10]={};
int count=0;
int k=0;
Servo base;
Servo waist;
Servo elbow;
Servo clipper;
int state=0;
int basePos=0;
int waistPos=0;
int elbowPos=0;
int clipperPos=0;
void setup() {
Serial.begin(9600);
elbow.attach(3);
waist.attach(5);
base.attach(6);
clipper.attach(9);
elbow.write(elbowPos);
waist.write(waistPos);
base.write(basePos);
clipper.write(clipperPos);
// put your setup code here, to run once:

}

void loop() {

if(Serial.available()>0)
{
state=Serial.read();
if(state==‘0’)
{
basePos=basePos+15;
if(basePos>=180)
basePos=180;
base.write(basePos);
Serial.print(basePos);
x[count]=state;
count=count+1;
state=9;
}
if(state==‘1’)
{
basePos=basePos-15;
if(basePos<=0)
basePos=0;
base.write(basePos);
Serial.print(basePos);
x[count]=state;
count=count+1;
state=9;
}

if(state==‘2’)
{
waistPos=waistPos+15;
if(waistPos>=180)
waistPos=180;
waist.write(waistPos);
Serial.print(waistPos);
x[count]=state;
count=count+1;
state=9;
}
if(state==‘3’)
{
waistPos=waistPos-15;
if(waistPos<=0)
waistPos=0;
waist.write(waistPos);
Serial.print(waistPos);
x[count]=state;
count=count+1;
state=9;
}
if(state==‘4’)
{
elbowPos=elbowPos+15;
if(elbowPos>=180)
elbowPos=180;
elbow.write(elbowPos);
Serial.print(elbowPos);
x[count]=state;
count=count+1;

Serial.print(count);

state=9;
}
if(state==‘5’)
{
elbowPos=elbowPos-15;
if(elbowPos<=0)
elbowPos=0;
elbow.write(elbowPos);
Serial.print(elbowPos);
x[count]=state;
count=count+1;
state=9;
}

if(state==‘6’)
{
clipperPos=clipperPos+15;
if(clipperPos>=180)
clipperPos=180;
clipper.write(clipperPos);
Serial.print(clipperPos);
x[count]=state;
count=count+1;
state=9;
}
if(state==‘7’)
{
clipperPos=clipperPos-15;
if(clipperPos<=0)
clipperPos=0;
clipper.write(clipperPos);
Serial.print(clipperPos);
x[count]=state;
count=count+1;
state=9;
}

if(state==‘8’)
{
int jpt=0;
int p=0;

while(1)
{
p=x[jpt];

aman(p);

Serial.print(x[jpt]);

jpt=jpt+1;
if(jpt>count)
break;
}
}
}
}

void aman(int a) {
int state;
state=a;
if(state==‘0’)
{
basePos=basePos+15;
if(basePos>=180)
basePos=180;
base.write(basePos);
Serial.print(basePos);
state=9;
}
if(state==‘1’)
{
basePos=basePos-15;
if(basePos<=0)
basePos=0;
base.write(basePos);
Serial.print(basePos);
state=9;
}

if(state==‘2’)
{
waistPos=waistPos+15;
if(waistPos>=180)
waistPos=180;
waist.write(waistPos);
Serial.print(waistPos);
state=9;
}
if(state==‘3’)
{
waistPos=waistPos-15;
if(waistPos<=0)
waistPos=0;
waist.write(waistPos);
Serial.print(waistPos);
state=9;
}

if(state==‘4’)
{
elbowPos=elbowPos+15;
if(elbowPos>=180)
elbowPos=180;
elbow.write(elbowPos);
Serial.print(elbowPos);
state=9;
}
if(state==‘5’)
{
elbowPos=elbowPos-15;
if(elbowPos<=0)
elbowPos=0;
elbow.write(elbowPos);
Serial.print(elbowPos);
state=9;
}

if(state==‘6’)
{
clipperPos=clipperPos+15;
if(clipperPos>=180)
clipperPos=180;
clipper.write(clipperPos);
Serial.print(clipperPos);
state=9;
}
if(state==‘7’)
{
clipperPos=clipperPos-15;
if(clipperPos<=0)
clipperPos=0;
clipper.write(clipperPos);
Serial.print(clipperPos);
state=9;
}
}

temporary.ino (3.59 KB)

we keep getting troubles

What troubles ?

Please read this before posting a programming question, tidy up the code in the IDE by Auto Formatting it and post it here using [­code] ... [­/code] tags

but we keep getting troubles.

Are you going to tell us what they are?
Are you going to tell us why the bluetooth module is connected to the hardware serial port, preventing using that for trouble-shooting/debugging?
Are you going to read the stickies at the top of the forum, and post your code CORRECTLY?
Are you going to tell us what is on the other end of the bluetooth link?

Without doing ALL of that, we can't help you.

      x[count] = state;
      count = count + 1;

What is the maximum value allowed for count in your program ?
How many elements are there in the x array ?