problem with servo mg995

hi
i want to connect 8 servo to mega2560 and control them with potentiometer (one potentiometer for control 2 servo)
when i connect one servo there is no problem and it works well but when i connect the second servo, second servo start to shake and doesn’t work
here is the code:

#include <Servo.h> 
 
Servo myservo;  
Servo myservo2; 
int potpin = 0;  
int val;     
 int val2;
void setup() 
{ 
  myservo2.attach(49);   
myservo.attach(48);
} 
 
void loop() 
{ 
  val = analogRead(potpin);           
  val = map(val, 0, 1023, 0, 179);    
  myservo.write(val);                  
 val2 = analogRead(potpin);            
  val2 = map(val2, 0, 1023, 179, 0);      
  myservo2.write(val2);
  delay(15);                          
}

what is the problem that they don’t perform good?

what is the problem that they don't perform good?

How are you powering the servos? Don't power them from the arduino or the arduino will probably crash.

Yes your problem is the power supply and either lack of current or lack of decoupling.

thanks for your answers
I don't use arduino for servo power
i use a 2amp power supply and there are7 servos
you mean i use another power supply ?(with capability of more than 2amp)
what do you mean by lack of decoupling?

You must adapt your circuit to support all those servos that you are working with. The simultaneous activation could cause some problems.

Read this old post:

About the lack of decoupling:
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

thanks for the links.they are very useful
i don't use decoupling in my project and i will use it
in then first link i saw he used 2 separate power supply for arduino and servo.in this case we must connect gnd of 2 power supply to each other.am i right?
in my circuit i connect the usb to laptop and connect the servos to the power supply,it mean i use 2 separate power supply,is it right?
in this case i think if i use decoupling it may get better

in this case we must connect gnd of 2 power supply to each other.am i right?

Yes.

This is why:-
http://www.thebox.myzen.co.uk/Tutorial/Power_Supplies.html