Button press only working once (Have to reset after button press)

Hi,

I am working on a Pinball machine for a school project and I am making the paddles at the moment. The problem is that when i press the button to move the servo I have to reset it before the button will work again. Here is my code:

#include <Servo.h>
Servo myservo1;

const int servoPin = 9;
const int button1 = 8;

int pos = 0;
int directionState = 0;
int buttonState = 0;

void setup() {
myservo1.attach(9);
pinMode(button1, INPUT);
}

void loop() {
moveServo();
}

void moveServo() {
while(1) {
pos = 0;
myservo1.write(pos);
buttonState = digitalRead(button1);
if (buttonState == HIGH) {
while (directionState == 0) {
directionState += 1;
pos = 90;
myservo1.write(pos);
delay(300);
pos -= 90;
myservo1.write(pos);
digitalWrite(buttonState, LOW);
}
}
}
}

Thanks

Do you have a pull up or pull down resistor on the pin that the button is attached to? You did not specify pullup in the pinmode statement. If you don't have a resistor pulling button1 up or down then it is floating when the button is not pressed. Not good! Also, what are you trying to accomplish with the last statement?

digitalWrite(buttonState, LOW);

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :)

void moveServo() {
  while(1) {

Yes, while(1) means keep repeating this code forever. So it won't ever exit that function until you reset the board.

What is the purpose of putting a while(1) there?

   while (directionState == 0) {
        directionState += 1;
        pos = 90;
        myservo1.write(pos);
        delay(300);
        pos -= 90;
        myservo1.write(pos);
        digitalWrite(buttonState, LOW);
      }

What's the purpose of the while loop here as well. This is another bug. You start with ditrectionState set to 0. You run this while loop once and it gets set to 1. Nowhere does the code ever set it back to zero, so what's in that while loop can never execute again until you reset the board.

Not only that, but unless you are planning to set the value of directionState to something less than 0, this while loop will only ever iterate once. So it is kind of pointless to make a while loop for something you only want to do one time.

Using an infinite loop, the while(1) bit, is poor practice, that is what the loop function is for.

digitalWrite(buttonState, LOW);

Will set a logic zero on pin 0 or pin 1 depending on what state you read from pin 8. Pins 0 & 1 are used to talk to the computer over the serial line so you don't want to be messing with those. What ever you thought that did - it doesn't - and nothing needs doing here anyway.

The directionState variable is only ever incremented so after the first time the while ( directionState == 0 ) loop is entered for the first time it will never enter it again.