Arduino starts its self

Hello, i have a problem with programm, i guess. I want servo motor to move constant degrees but in wariable times, example first button push makes move servo motor 1 time, second button push make 2 moves, etc, but when i plug in arduino it starts programm without any push.
thanks

sketch_may18a.ino (1.14 KB)

how are your switches connected to the inputs? since it looks like you want to do something when the input is high, do you have a pull-down resistor and the switch connected between the input and 5V?

in two cases you have infinite loops, "while (1);" that will prevent any further operation. why?

Please post your code.

gcjr:
how are your switches connected to the inputs? since it looks like you want to do something when the input is high, do you have a pull-down resistor and the switch connected between the input and 5V?

in two cases you have infinite loops, "while (1);" that will prevent any further operation. why?

so the switch is connected to 5v and without any resistors to input.
and when i don't put in while it servo wont stop working after one cycle.

TheMemberFormerlyKnownAsAWOL:
Please post your code.

#include <Servo.h>
int servoPin = 9;
int buttonPin = 7;
int buttonPin1 = 2;
int buttonPin2 = 4;
int buttonState = 0;
int buttonState1 = 0;
int buttonState2 = 0;

Servo Servo1;
void setup() {
// put your setup code here, to run once:

pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(servoPin,OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
if(buttonState==HIGH){
Servo1.attach(servoPin);
Servo1.write(120);
delay(1500);
Servo1.write(0);
delay(1000);
while(0);
}
buttonState1 = digitalRead(buttonPin1);
if (buttonState1==HIGH){
Servo1.attach(servoPin);
Servo1.write(120);
delay(1500);
Servo1.write(0);
delay(1000);
Servo1.write(120);
delay(1500);
Servo1.write(0);
delay(1000);
while(1);
}
buttonState2 = digitalRead(buttonPin2);
if(buttonState2==HIGH){
Servo1.attach(servoPin);
Servo1.write(120);
delay(1500);
Servo1.write(0);
delay(1000);
Servo1.write(120);
delay(1500);
Servo1.write(0);
delay(1000);
Servo1.write(120);
delay(1500);
Servo1.write(0);
delay(1000);
while(1);
}
}

Please explain the purpose of these lines...

while(0);
while(1);

I think you may have the wrong idea about what "while" does.

The correct way to post code on the forum is inside code tags. They look like this...

your code goes here

And show your circuit.
You have pull down resistors on your buttons, right?
Any particular reason you went with external pull-downs and rejected the internal pull-ups?

razbainieks:
so the switch is connected to 5v and without any resistors to input.

it's conventional to connect the switch between the pin and Gnd, configure the pin as INPUT_PULLUP and test for the pin going LOW.

since you test for the switch being HIGH, your code is always running and will never stop

razbainieks:
and when i don't put in while it servo wont stop working after one cycle.

an infinite loop will stop everything from working.