Strange behavior with multiple servos

Hello. This is my first arduino project and my first post here.
A bit about my project: I’m using the Servo library to control 3 servos with a pushbutton. It’s supposed to turn pages on a book. Two servo arms make contact with a page on each corner, and turn inwards simultaneously, squeezing the page so it bunches up at the center. The third servo flips an arm that grabs underneath that bunched up page, and then flips it over.

It actually works, but there are two peculiarities during the routine that I can’t account for:
1.) During the setup function in the code, during the part where the three servos are moved to their initial positions. The first servo to move will go straight to the position specified, but the second and third will move to the middle position and then go to the specified positions. I’ve played around with it, and whatever servo moves first in the code is the only one that goes straight to the position.
2.) During the loop. When the squeezers squeeze the page, and the arm grabs the page. It will do this fine, but then the squeezers retract, and the flip arm retracts, and they do it again, and then the routine continues to the end. So basically it performs this section twice for some reason.(In the code, the squeezers are supposed to retract so it’s easier for the arm to flip the page. This isn’t what’s happening here, though.)

Here’s my code:

//Book Scanner Control code
#include <Servo.h>

const int buttonPin = 2;
  const int servo1Pin = 4;
  const int servo2Pin = 7;
  const int servo3Pin = 8;
  int buttonState = 0;
  int lastButtonState = 0;
  Servo servo1; 
  Servo servo2;
  Servo servo3;
void setup()
  pinMode(buttonPin, INPUT);
  servo2.write(0); //Whatever servo goes here works fine.
  servo3.write(180); // This one goes to 90 before 180
  servo1.write(0); //This one also

void loop()
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState)
    servo1.write(180); //Brings flip arm to ready position above page
    for (int i=0; i<50; i++)//Squeezes page 
    servo1.write(145); //flip arm grabs page
    for (int i=0; i<50; i++)//Squeezers retract
      servo3.write(130 + i);
    servo1.write(0); //Flip arm flips page
  lastButtonState = buttonState;

As for wiring, I have a 5v wall wart powering the servos. Wall wart and servo grounds are tied to the arduino’s ground as well. Servos are attached at the pins indicated in the code The pushbutton works fine, and the whole thing will actually turn a page, it’s just that I can’t figure out what’s going on with the two things I mentioned. If anyone has any suggestions, they’d be greatly appreciated.

You can use servo.write() to set the initial position BEFORE you do the servo.attach().

The code appears to do a page flip each time the button is pressed AND each time the button is released. Could that be causing the double-action you are seeing?

Your advice solved both my problems! I was working under the assumption that my button worked like a switch (a press and release turns on, another press and release turns off) and not like, well... a button. :stuck_out_tongue:
I also staggered the first part, so each servo writes to its initial position right after it attaches in the code, and they all work fine now.
Thanks a lot!