Weird servo twitching

I am trying to control my servo to go from 0 degrees to 90 degrees by pressing a button and switch back and forth by pressing the button. But once I press the button to make the servo go to 90 degrees, the servo starts twitching and does not stay in the position. But it will stay put and work fine when it goes to 0 degrees. Any ideas on why this is happening? This is also not my original code. I have little to no experience coding and found this online. Here is the video of the servo.

//Button Toggle Servo

#include <Servo.h> 
Servo servo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards
const int buttonPin = 2;
boolean lastState = LOW;//storage for last button state
boolean pos = true;
void setup()
{
  servo.attach(8);  // attaches the servo on pin 9 to the servo object
  pinMode(buttonPin, INPUT_PULLUP);//this time we will set the pin as INPUT
  Serial.begin(9600);//initialize Serial connection 
}
void loop()
{
  boolean currentState = digitalRead(buttonPin);
  if (currentState == LOW && lastState == HIGH)
  {
    Serial.print(pos ? "up" : "down");
    servo.writeMicroseconds(pos ? 1900 : 900);
    delay(150);
    pos = !pos;
  }
  lastState = currentState;
}

The most common error is trying to power a servo from the arduino, which often causes the arduino to reset.

Are you getting the correct messages printed on the Serial Monitor ?

If you add Serial.println("Starting"); as the last line in setup() you will know if the Arduino is resetting when it should not.

...R

Robin2: Are you getting the correct messages printed on the Serial Monitor ?

I am getting correct messages on the Serial Monitor. It just says Starting in the very beginning and doesnt again. But I am still getting the same twitching with my servo.

But I am still getting the same twitching with my servo.

And are you still trying to power the servo from the Arduino?

jremington: And are you still trying to power the servo from the Arduino?

No. At least I dont think so. I'm not using the 5v pin on the board. I have 4 AA batteries that I have wired to power the servo. Here is a picture of my wiring. Hopefully I didn't mess something up here or else I'm going to feel dumb.

Sorry, impossible to make much sense of the photo. A hand drawn wiring diagram would be much better.

Do you have a common ground for the servo power and the Arduino? It is required.

http://imgur.com/fq0zWy6

Here is a diagram of my wiring. The only difference is I have 4 AA batteries instead of the one shown on the diagram

That looks OK to me. Are the batteries fresh?

They should be. After doing some research it seems that the servos need a few Amps to work properly. Do 4 AA batteries supply enough Amps to run up to 6 servos? Could that be the problem?

Do 4 AA batteries supply enough Amps to run up to 6 servos? Could that be the problem?

So now there are six servos you are trying to power from four AA batteries? Yes, that could be a problem. :roll_eyes:

Get one servo working first.

AA batteries will power two servos at most, and only if those two are not straining at the same time.

Well I want to eventually power 6 servos for the project. What kind of power system do I need to eventually power 6 servos together? Any suggestions on what I should get?

Budget one ampere per straining servo, so for six straining servos you need a power supply capable of 5-6 V at 6 amperes.

Hexapod bots often have 18 servos, so you might look to see what type they use.

https://www.google.com/search?as_q=hexapod+battery&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=&as_qdr=all&as_sitesearch=&as_occt=any&safe=images&as_filetype=&as_rights=&gws_rd=ssl

http://www.ebay.com/itm/AC110-220V-to-DC5V-10A-50W-Driver-Switch-Power-Supply-Transformer-for-LED-Light/201195896777?_trksid=p2047675.c100005.m1851&_trkparms=aid%3D222007%26algo%3DSIC.MBE%26ao%3D1%26asc%3D32933%26meid%3D087b2ac46ed9427a903a181a3060a393%26pid%3D100005%26rk%3D4%26rkt%3D6%26mehot%3Dag%26sd%3D201123931405

Would something like this work?

Also if I want a more portable option would this work? What would be the difference between a 2S and 3S Lipo battery?

The power supply is overkill, but it will work.

With LiPo batteries you have to worry about over-discharge, which destroys them, and improper charging, which can cause fires. You need a LiPo charger and the battery should have a protection PCB, plus you will need a voltage regulator to provide 5-6 V at 6 amps for the servos.

2S = 2 cells (7.4 V) and 3S = 3 cells (11.2 V)

What makes the power supply over kill? How many Amps should i be looking for?

See reply #13

Oh gotcha. Sorry. But having more amps than necessarily isnt a bad thing? It wont damage or do anything weird?