Servo Problems

Not sure if this is a redundant question or what, but I've been messing with this code for half an hour and I just cant seem to get it to work. I have checked examples in the Arduino Projects Book, mainly the ones involving servos and pushbuttons, and they don't seem to be any different than my code (other than the fact that I'm merging the two examples).

The problem is that when I run the code, the servo first jolts about 10-20 degrees (I've heard this is common), then turns all the way to the maximum amount (180), and appears to be continuing to turn, or attempting to turn. Keep in mind that this is all before I even press the button. When I do press the pushbutton, the servo jolts past the 180 degrees by about 10 degrees, then my computer flashes an error message stating that the connected USB device is consuming too much power, and that the computer has cut off its power to the USB port in order to protect its components. Also, when I attempted to turn the servo manually while the code was running, I met heavy resistance from the servo (obviously I stopped trying further).

Heres the code:

#include <Servo.h>

Servo myServo;
int switchState = 0;

void setup()
{
  myServo.attach(3);
  Serial.begin(9600);
  pinMode(13, INPUT);
}

void loop()
{ 
  while(1 == 1)
  {
    myServo.write(0);
    //^added this to make sure that the servo was at 0 when the program started
    delay(50);
    switchState = digitalRead(13);
    if(switchState == LOW)
      {}
    //^also added this to try to fix it
    else if(switchState == HIGH)
    {
      Serial.println("Button Pressed");
      myServo.write(160);
      //^changed from 179 just to try to make it more stable
      delay(50);
    }
  }
}

If you think the problem is in the circuitry- It's really just a servo (yes all three wires are connected to the right places) with a capacitor and a pushbutton with an input wire to port 13- then I can post a more detailed description/image, but I really don't think that's the problem. From my limited perspective, the problem appears to be with the if statement checking for the button press, but I can't see any syntax that is incorrect. I hope someone else who is more experienced can solve this.

Btw, the parts I am using are all part of the Arduino Starter Kit, including the servo, and in case it's not clear, I want the servo to turn from 0 degrees to 180 when the button is pressed. Thanks!

Oh dear, its a mess, but it can be sorted out

while(1 == 1)Why ? Is the loop() function not good enough at repeating code ?
myServo.write(0);Sends the servo fully one way every time through the loop not just when the program starts

    if(switchState == LOW)
    {
    }

This is redundant. Test for the condition you are looking for and act on it.
    else if(switchState == HIGH)Even assuming you needed the else, which you don't, what other value could switchState have anyway ?
You don't say how the switch is wired but from the logic in your program activating takes the input pin HIGH. Have you got a pulldown resistor holding LOW when the switch is not activated so that you know its state at all times ? A better alternative is to use the internal pullup resistor and   pinMode(pinNumber, INPUT_PULLUP);The logic for reading the pin state and the wiring will need to be revised but it is a much tidier solution.

    else if(switchState == HIGH)
    {
      Serial.println("Button Pressed");
      myServo.write(160);
      //^changed from 179 just to try to make it more stable
      delay(50);
    }

Why the delay() ? The servo will not turn 160 degrees in 50 milliseconds and even if it did you want it to stay there while the switch is turned on. As it is, after the delay() the code loops back to     myServo.write(0);and off the servo goes back to 0.

Instead of an if you need to use a while so the servo stays at 160 while the button is pressed, but you must test the switch state inside the while loop so that it can end when the switch is opened.

Now for the big question, but I fear that I already know the answer. How is the servo powered ? The Arduino cannot provide enough power for any reasonable servo so you need to use an external power supply and a common GND

Just to add to UKHB's description of pullup and pulldown resistors, the attached diagram may help.

then my computer flashes an error message stating that the connected USB device is consuming too much power, and that the computer has cut off its power to the USB port in order to protect its components.

If you are powering the servo from the arduino, the servo is probably causing the arduino to reset due to low voltage, as well as overpowering the USB port on the pc.

USB ports standard max current is 500mA, but you know that right?
Try this - comment out ALL the code in loop();
In setup add
myServo.write(0);
and than download your sketch and observe your servo.
If the USB port stayed up ( your servo may not moved at all) , try this just to make sure.

Change your code. still in setup to
myServo.write(180);
and repeat.

Did the PC shut the USB port down?
If it did - rewire you power to the servo, if it did not - fix you loop code or better yet do your servo control in setup.
You can spent time coding and having full control over your code in setup() or you can do some extra coding making sure your code will work correctly in loop(). Your call.
Have fun anyway.

Yes, it is quite a mess, isn't it?
As I stated in a few comments in the code, many of the meaningless things I added were just experiments to see if for some strange reason it fixed the problem. As an afterthought, I probably should have condensed the code to its original before posting it. One of the only intentional things I did was the while(1 == 1) loop; I like to overwrite the original loop statement so that I can easily put an end to the code -without having to tab everything to add the statement- instead of it running infinitely.

Anyways, thanks for the reply! Based on your information, it seems that these are the problems:

1.) Setting the servo to 0 should go in the void setup().

2.) I didn't place a resistor on the switch! For some reason I didn't see this in the project book, and your noticing it was extremely helpful. This explains perfectly why the code kept trying to turn to 180, because it was interpreting a HIGH signal all the time.

Instead of an if you need to use a while so the servo stays at 160 while the button is pressed, but you must test the switch state inside the while loop so that it can end when the switch is opened.

Yes, I can see now how my code was structured to serve that purpose, which would require a while loop, but I was actually only trying to fulfill the simple purpose of the servo turning to 180 degrees from 0 when the button is pressed, and staying there. Does that mean that by continually setting the servo to 180 degrees each time the button is pressed, I am damaging the servo? If so, how should I restructure the code to only set the servo to 180 once?

Finally,

Now for the big question, but I fear that I already know the answer. How is the servo powered ? The Arduino cannot provide enough power for any reasonable servo so you need to use an external power supply and a common GND

No, I am not using an external power supply, as the tiny servo included in the Arduino Starter Kit draws little enough power that the computer can effectively power it (there's a project in the book where you control a servo with a pot, and it is powered by your computer). Lastly, I am only using the tiny servo to move a thin piece of paper, so the servo shouldn't be straining to move it.

@Vaclav and zoomkat

Sorry, didn't see your replies before posting. Yes, the PC shut the USB port down, but I think that it was because the servo was trying to go past the 180 degrees, or the looping statement telling the servo to go to 0 degrees was conflicting with the one telling it to go to 180, causing some kind of internal strain on the servo. As to testing if the servo consumes too much power, I am quite certain that the one I am using is fine with being powered by the computer, as it is the small one included in the starter kit, and one of the projects instructs you to use your PC to power it (although the project has nothing connected to the servo, whereas mine has a piece of paper, but -correct me if I'm wrong- I don't think that it should cause too much of a difference).

Anyways, thanks for the replies!

That is why I made my suggestions, just to check.
Stalled motors want more power than running motors.

One of the only intentional things I did was the while(1 == 1) loop; I like to overwrite the original loop statement so that I can easily put an end to the code -without having to tab everything to add the statement- instead of it running infinitely.

Sorry, but I can't make sense of what you are saying.

If you are saying in some way that you like to keep your code neatly formatted (which is s good thing) but find it a nuisance to do manually when you make changes then try using Tools/Auto Format in the IDE.