Potentiometer analog value is not stable when it is sended via bluetooth

I am doing project to send value of potentiometer by bluetooth to control servo in other arduino board. However, the value of potentiometer is not stable. For instance, when I got 58 as degree, it goes up to 59 and back to 58 and so on. I also notice that after the bluetooth sends data ( led in bluetooth flash ), the value of potentiometer changed.
My wiring.
I use power from arduino 5v ( Uno) to supply bluetooth and one potentiometer.
However, when I disconect power to bluetooth, I realize that the value of potentiometer is eventually stable. I doubt that the power maybe not sufficient, so I supply power to bluetooth by separated another power supply with ground together. Again, the value is unstable.
Can any one guild me to solve the problem.
Here is my code
Sending data

int potpin = A0;

int val,gt;

int val1=0;

void setup()

{

Serial.begin(9600);

BluetoothSerial.begin(9600);

}

void loop()

{

val = analogRead(potpin);

gt = map(val, 0, 1023, 0, 180);

if(gt!=val1){ // when gt is not equal to val1 , data is sent
 
BluetoothSerial.println(gt);
 
val1=gt;

Serial.println(gt);

delay(300);
}
}

If you average multiple readings you're likely to get better stability.

I posted some code which uses a ring buffer to average readings from a joystick. I thought it worked will with my joystick and servos.

The constant "POWER_OF_TWO_TO_AVERAGE" is used to set the size of the averaging buffer.

const int POWER_OF_TWO_TO_AVERAGE = 4;          // User changeable.

In this example the number of samples to be averaged is 16.

IMO, the normal servo "write" command is awful. You can control servos more precisely (with smoother transitions) if you use "writeMicroseconds".

DuaneDegn:
If you average multiple readings you're likely to get better stability.

I posted some code which uses a ring buffer to average readings from a joystick. I thought it worked will with my joystick and servos.

The constant "POWER_OF_TWO_TO_AVERAGE" is used to set the size of the averaging buffer.

const int POWER_OF_TWO_TO_AVERAGE = 4;          // User changeable.

In this example the number of samples to be averaged is 16.

IMO, the normal servo "write" command is awful. You can control servos more precisely (with smoother transitions) if you use "writeMicroseconds".

I would like to ask you one question. Firstly, I would like to describe my wiring
Bluetooth Master with Uno board ( Pin 10= Tx, Pin 11=Rx)
Bluetooth Slaver with Mega board ( Pin 10= Tx Pin 11=Rx)
Servo connect to Pin 9 (PWM) in Mega board
The data received from Slaver is oke. For instance, when I send 139 from Master, Slaver receives 139. One thing is nothing right that the servo vibrates slightly. When I twist the pot the servo also rotates according to degrees, but it stills vibrate. Do you know what the problem is

phamb587:
When I twist the pot the servo also rotates according to degrees, but it stills vibrate. Do you know what the problem is

Most of these sort of problems are caused by not enough power getting to the servo.

How are you powering the servo?

Do you have a link to the servo you're using?

DuaneDegn:
Most of these sort of problems are caused by not enough power getting to the servo.

How are you powering the servo?

Do you have a link to the servo you're using?

I supply servo separately with Power (5V,10A), it still vibrates. However, when I add sample code to test servo from Arduino forum It works normally.

Receive code

#include <Servo.h>

#define BT_SERIAL_TX 10

#define BT_SERIAL_RX 11

SoftwareSerial BluetoothSerial(BT_SERIAL_TX, BT_SERIAL_RX);

Servo myservo;
int i;

int val;

int array[2];

void setup()

{

Serial.begin(9600);

BluetoothSerial.begin(9600);


}

void loop()
{
myservo.attach(7);

int incoming = BluetoothSerial.read();
if (BluetoothSerial.available()>0){
   
 array[0]= BluetoothSerial.parseInt(); // read the data
 array[1]= BluetoothSerial.parseInt();
 Serial.println(array[0]);
 myservo.write(array[0]); // write value to servo
  
}
}