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
const int buttonPin = 2;
const int servo1Pin = 4;
const int servo2Pin = 7;
const int servo3Pin = 8;
int buttonState = 0;
int lastButtonState = 0;
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
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.