power supply

Could you please help me,
I have written a servo/button program, the servos work properly when plugged into the PC USB but as soon as I connect the Arduino to a 7 vdc supply via the barrel plug the servos start to sweep without stopping. I use am LM317 with proper smoothing for the power supply.
Thanks,

Wally.

#include<Servo.h>
const int buttonPin = 2;
const int servoPin = 9;
int buttonState = 0;
int directionState = 0;
Servo servoOne;
Servo servoTwo;
int pos = 0;

void setup() {
  servoOne.attach(9);
  servoTwo.attach(10);
  servoOne.write(directionState);
  servoTwo.write(directionState);
  pinMode(buttonPin, INPUT);

}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (directionState == 0) {
    if (buttonState == HIGH) {
      directionState = 1;
      for (pos = 0; pos < 90; pos = pos + 1) {
        servoOne.write(pos);
        delay(40);
        servoTwo.write(pos);
        delay(40);
      }
    }
  } else if (directionState == 1) {
    if (buttonState == LOW) {
      directionState = 0;
      for (pos = 90; pos > 1; pos = pos - 1) {
        servoOne.write(pos);
        delay(40);
        servoTwo.write(pos);
        delay(40);
      }
    }
  }
}

The servos need a separate power supply - do they have one?

How do the servos get their power ?

If the answer is "from the Arduino 5V and GND pins" then do as Bill suggests but don't forget to make the GNDs common

As suggested, it's possibly the same issue I had with my very first Arduino project.

The Arduino can supply the correct voltage for all kinds of modules and hardware. However, it cannot supply enough current (amperage) for many of them. Especially motors.

One option may be to add one of these (or similar) to your circuit to power your servo(s).

You do not say which Arduino board that you have, but 7V to the power jack may not meet the minimum voltage requirement for the 5V regulator on the Arduino. There is a diode in line between the power jack and the regulator on the Uno and Mega. That diode may drop the 7V to below the drop out voltage of the regulator and the regulator will not function properly. There is no diode between Vin and the regulator so might be a better way to connect.

Thank you for your replies.
I am using an Uno.
I connected the Arduino and the servos from two power sourced.
The Arduino was connected to the PC with the USB cable and the servos connected directly to the 7 vdc supply with the grounds connected together. I also tried adjusting the voltage to 9 vdc
All of this had no change the servos carried on sweeping.
As soon as I only use the PC USB cable for both the UNO and servos everything works fine.
I will try the power modules suggested.

Thanks for the advice, I found the problem. I forgot the 10k resistor from the button to ground.