Problem with code?

Hi i am new here,

i'm doing a robotic hand ,that can be control by using a push button and flex sensor.
The condition is like this

Here is my program for the push button it run perfectly

 #include <Servo.h>

int button1 = A0;
int button2 = A1;
int button3 = A2;
int button4 = A3;
int button5 = A4;
int press1 = 0;
int press2 = 0;
int press3 = 0;
int press4 = 0;
int press5 = 0;
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  pinMode(button5, INPUT);
  myservo1.attach(2);
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
  myservo1.write(179);
  delay(100);
    }
  else{
  myservo1.write(1);
  delay(100);
  }
  {
     press2 = digitalRead(button2);
  if (press2 == LOW)
  {
  myservo2.write(179);
  delay(100);
    }
  else{
  myservo2.write(1);
  delay(100);
}
  {
     press3 = digitalRead(button3);
  if (press3 == LOW)
  {
  myservo3.write(179);
  delay(100);
    }
  else{
  myservo3.write(1);
  delay(100);
}
 {
     press4 = digitalRead(button4);
  if (press4 == LOW)
  {
  myservo4.write(179);
  delay(100);
    }
  else{
  myservo4.write(1);
  delay(100);
}
 {
     press5 = digitalRead(button5);
  if (press5 == LOW)
  {
  myservo5.write(179);
  delay(100);
    }
  else{
  myservo5.write(1);
  delay(100);
}
 }
 }
  }
  }
}

Flex sensor,this also run perfectly

#include <Servo.h> 

Servo myservo1;// create servo object to control a servo 
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int potpin1 = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 

void setup() 
{ 
  Serial.begin(9600);
  myservo1.attach(2);  // attaches the servo on pin 9 to the servo object 
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
} 

void loop() 

{ 
  val = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023) 
  Serial.println(val);  
  val = map(val, 50, 300, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo1.write(val);
myservo2.write(val);
myservo3.write(val);
myservo4.write(val);
myservo5.write(val);
// sets the servo position according to the scaled value  
  delay(15);                           // waits for the servo to get there 
}

The problem is when i combined it like this ,the servo keep moving.

#include <Servo.h>

int button1 = A0;
int button2 = A1;
int button3 = A2;
int button4 = A3;
int button5 = A4;

int potpin1 = A5;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 


int press1 = 0;
int press2 = 0;
int press3 = 0;
int press4 = 0;
int press5 = 0;
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  pinMode(button5, INPUT);
  Serial.begin(9600);
  myservo1.attach(2);
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
  myservo1.write(179);
  delay(100);
    }
  else{
  myservo1.write(1);
  delay(100);
  }
  {
     press2 = digitalRead(button2);
  if (press2 == LOW)
  {
  myservo2.write(179);
  delay(100);
    }
  else{
  myservo2.write(1);
  delay(100);
}
  {
     press3 = digitalRead(button3);
  if (press3 == LOW)
  {
  myservo3.write(179);
  delay(100);
    }
  else{
  myservo3.write(1);
  delay(100);
}
 {
     press4 = digitalRead(button4);
  if (press4 == LOW)
  {
  myservo4.write(179);
  myservo5.write(179);
  delay(100);
    }
  else{
  myservo4.write(1);
  myservo5.write(1);
  delay(100);
}
 {
     press5 = digitalRead(button5);
  if (press5 == LOW)
  {
  val = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023) 
  Serial.println(val);  
  val = map(val, 50, 300, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo1.write(val);
myservo2.write(val);
myservo3.write(val);
myservo4.write(val);
myservo5.write(val);
// sets the servo position according to the scaled value  
  delay(15); 
}
}

 }  
 }
 }
  }

Which servo keeps moving ?

What sort of buttons do you have ?

michinyon:
Which servo keeps moving ?

What sort of buttons do you have ?

.when i'm give a supply to the board and the servo. all the servo are moving randomly.Its like the all the servo recive and input from the flex sensor directly,
i am using push button switch.

How are the buttons wired ? Do you have any pullup resistors attached or are the inputs floating at an uncertain voltage until the button is pressed ? How are the servos powered ? From the Arduino or from an external power supply ?

{
What's
{
with
{
all
{
the
{
useless
{
braces
}
}
}
}
}
}

UKHeliBob:
How are the buttons wired ? Do you have any pullup resistors attached or are the inputs floating at an uncertain voltage until the button is pressed ? How are the servos powered ? From the Arduino or from an external power supply ?

Here is my circuit

PaulS:
{
What's
{
with
{
all
{
the
{
useless
{
braces
}
}
}
}
}
}

because this appear when i want to verify "expected } at end of input" .

because this appear when i want to verify "expected } at end of input" .

The solution is to get rid of the useless {, not to add a useless } to match it.

If your are powering the servos from the 5V output of the Arduino, you should add a good capacitor. 330uF or more should be OK.

I had problems with a servo connected to th5 5V output of the arduino. When I tried to move the servo, the Arduino was resetted (probably due to noise introduced by tye motor). I solved it adding an electrolitic capacitor. The problem was with one of this cheap chinese servos like this:

anv:
If your are powering the servos from the 5V output of the Arduino, you should add a good capacitor. 330uF or more should be OK.

I had problems with a servo connected to th5 5V output of the arduino. When I tried to move the servo, the Arduino was resetted (probably due to noise introduced by tye motor). I solved it adding an electrolitic capacitor. The problem was with one of this cheap chinese servos like this:

I powering the servo using 5V,2A AC/DC adapter ,do i need to add the capacitor too?