help me! button for servo continuous motor

I made that code and does ton works. anyway if you push button or not it rotates and you cants stop it. please tell what is wrong or give me another code.
I just want one pushbutton and motor. and it should rotate when i press button, else-stop. it simple but I cant:((

thanks!!!

that is code

#include <Servo.h> 
const int buttonPin = 2; 
Servo myservo;

int buttonState = 0; 

void setup() {
  myservo.attach(9);
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    myservo.write(45);  
  } 
  else {
    // turn LED off:
    myservo.write(90); 
  }
}

choyina@gmail.com

The servo may not stop at exactly 90 “degrees”; you may have to experiment to find the exact stop point.
zoomkat often posts code to help you to do this.

no, it stops on 90 degree, that is not problem. I think that it should not rotate until i press button but it does:/

thaks!

How is your button wired up? Sounds like you have a floating input. Search the forum for pull up or pull down resistors.

no, there is something wrong harder. it rotates even i press button or not. button and its kind does not means. anyway my button is some kind of wires.

Some debug prints would be useful, but I agree it could be a floating input.

Simple servo test code for two buttons controlling a servo. If your servo is continous, you would use a stopped value like ~90 (or ~1500 us for better control) to stop the servo, and some other value for the speed/direction of the servo movement.

//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
  
  /*else {
    servo1.write(90);
  }*/
}

thanks! youre grate!!