Arduino Servo's with button switch

Hello,

I'm new with Arduino.

i have this code and if i run i, it counts automatically....
Does anyone know how to fix it, I have tried everything but nothing helps:P
I have 3 Servo programms and if i push the button he must go to the next program.

const int motorPin = 9;
const int buttonPin = 2;

int buttonState = 0;
int lastButtonState = 0;
int buttonPushCounter = 0;

void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);

}

void loop()
{
buttonState = digitalRead(buttonPin);
if(buttonState != lastButtonState)
{
if (buttonState == HIGH){
buttonPushCounter++;

switch (buttonPushCounter)
{
case 1:
Serial.println("1");
motorOnThenOff();
delay(15);
break;

case 2:
Serial.println("2");
motorAcceleration();
delay(15);
break;

case 3:
Serial.println("3");
motorOnThenOffWithSpeed();
delay(15);
break;
}
}
}
}

void motorOnThenOff(){
int onTime = 2500;
int offTime = 1000;

digitalWrite(motorPin, HIGH);
delay(onTime);
digitalWrite(motorPin, LOW);
delay(offTime);
}

void motorOnThenOffWithSpeed(){

int onSpeed = 200;
int onTime = 2500;

int offSpeed = 0;
int offTime = 1000;

analogWrite(motorPin, onSpeed);
delay(onTime);
analogWrite(motorPin, offSpeed);
delay(offTime);
}

void motorAcceleration(){
int delayTime = 50;

for(int i = 0; i < 256; i++){
analogWrite(motorPin, i);
delay(delayTime);
}

for(int i = 255; i >= 0; i--){
analogWrite(motorPin, i);
delay(delayTime);
}
}

Go back and modify that post. Select the code and hit the # icon and then save it.

I have tried everything but nothing helps:P

If nothing helps than you have not tried everything have you?

You are doing nothing with the variable lastButtonState, it is never changed!

const int motorPin = 9;
const int buttonPin = 2;

int buttonState = 0;
int lastButtonState = 0;
int buttonPushCounter = 0;

void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);

}

void loop()
{
buttonState = digitalRead(buttonPin);
if(buttonState != lastButtonState)
{
if (buttonState == HIGH){
buttonPushCounter++;

lastButtonState = buttonState;

switch (buttonPushCounter)
{
case 1:
Serial.println("1");
motorOnThenOff();
delay(15);
break;

case 2:
Serial.println("2");
motorAcceleration();
delay(15);
break;

case 3:
Serial.println("3");
motorOnThenOffWithSpeed();
delay(15);
break;
}
}
}
}

void motorOnThenOff(){
int onTime = 2500;
int offTime = 1000;

digitalWrite(motorPin, HIGH);
delay(onTime);
digitalWrite(motorPin, LOW);
delay(offTime);
}

void motorOnThenOffWithSpeed(){

int onSpeed = 200;
int onTime = 2500;

int offSpeed = 0;
int offTime = 1000;

analogWrite(motorPin, onSpeed);
delay(onTime);
analogWrite(motorPin, offSpeed);
delay(offTime);
}

void motorAcceleration(){
int delayTime = 50;

for(int i = 0; i < 256; i++){
analogWrite(motorPin, i);
delay(delayTime);
}

for(int i = 255; i >= 0; i--){
analogWrite(motorPin, i);
delay(delayTime);
}
}

Now i add this line: LastbuttonState = Buttonstate
but now he does only do case 1 once and stops then. Why?
Sorry for my bad english i know its bad:p

A google forum search for servo button which may have info of interest from previous post.

http://www.google.com/search?q=servo+button+site%3Ahttp%3A%2F%2Farduino.cc%2Fforum%2Findex.php&hl=en&num=100&lr=&ft=i&cr=&safe=images

Each time you push the switch, buttonPushCounter is incremented. It starts at 0, so the first switch press increments it to 1 and the code for case 1 is executed. Since the code is only executed when the switch is pressed, and buttonPushCounter is incremented before that happens, buttonPushCounter will only be 1 once. So, case 1 will only be executed once.

Do you want the switch statement outside of the if-switch-is-pressed block?