Servos shake while waiting to move

Good evening good people the arduino forum. I have two arduinos set in master and slave mode using two hc-05 chips and the connection between them works as I can succesfully control an LED and RGB LED using the master remote. I know the value is being read but when I try to control servos like I normally do, they shake and rattle at all times. I know the code is very simple and would love advice on what needs to change. please do not just comment see serial examples or something similar

#include <SoftwareSerial.h>
#include <Servo.h>
Servo servo1;
SoftwareSerial BTSerial(10, 11);

//OP LED
const int OP_led = 2;


//RGB LED
const int redPin = 5;
const int greenPin = 6;
const int bluePin = 7;

//Servo Pins
const int servo1_pin = 12;
int initial_position = 90;
int servoIncrement = 10;

void setup()
{
  BTSerial.begin(9600);

  //Op and RGB
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(OP_led, OUTPUT);
  digitalWrite(OP_led, LOW);

  //Servos
  servo1.attach(servo1_pin);
  servo1.write(initial_position);
}
void loop()
{
  while (BTSerial.available() > 0)
  {
    char value = BTSerial.read(); // Reads the data from the serial port

    if (value == 'q')
    {
      digitalWrite(OP_led, HIGH); // LED Off
    }
    else if (value == 'w')
    {
      digitalWrite(OP_led, LOW); // LED On
    }
    else if (value == 'e')
    {
      setColor(0, 0, 0);  // none
    }
    else if (value == 'r')
    {
      setColor(255, 0, 0);  // red
    }
    else if (value == 't')
    {
      setColor(0, 255, 0);  // blue
    }
    else if (value == 'y')
    {
      setColor(0, 0, 255);  // green
    }
    else if (value == 'u')
    {
      setColor(255, 255, 255);  // white
    }
    else if (value == 'i')
    {
      setColor(255, 255, 0);  // purple
    }
    else if (value == 'o')
    {
      servo1.write(0);
      delay(100);
    }
    else if (value == 'p')
    {
      servo1.write(180);
      delay(100);
    }

  }
}
void setColor(int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
}

How do you normally control servos? Are you using the same power supply as what previously worked?

Shaking servos are almost always the result of an inadequate servo power supply, like the 5V Arduino output.

Use a separate supply (4xAA will power one or two small servos). Don't forget to connect the grounds.

I have both servos connected to an external 6V battery, the GND from the 6v is connected to the two GNDs from of the servos and the 6V to VCC of Servos, both servo 3rd wire connected to digital pin with pwm on arduino mega

Is the 6V ground connected to the Arduino board ground? Your description is unclear on that.

through the servo, does it need a resistor to the GND too?

Nevermind, I do have a 1k ohm resistor between the 5V and the GND, just couldnt see it

A resistor? Can you show a schematic that includes th servos, battery, resistor and Arduino connections. I don't know what a resistor is doing in the circuit.

colinjalbert1:
through the servo, does it need a resistor to the GND too?

It shouldn't be connected through the servo just through a piece of wire. Battery negative > wire > Arduino GND.

Steve

So I have tried with a resistor, a capacitor, noting and a piece of wire between the voltage and GND of the battery. The strange thing is when I set the initial position the motors move where I want without a fuss but the moment I turn on the controller the motors start to spasm. However, the master code for the controller is just sending an 'o' or a 'p' which shouldn't be causing all this trouble. Below is the master code and wiring info.

#include <SoftwareSerial.h>

//Assign the Tx and Rx pins of the bluetooth
SoftwareSerial BTSerial(2, 3);

//Assign the 4 Buttons
const int buttonPin1 = 12;
const int buttonPin2 = 11;
const int buttonPin3 = 9;
const int buttonPin4 = 8;


//Assign the Joyticks with Buttons
const int SW_pin1 = 5; // digital pin connected to switch output
const int X_pin1 = A0; // analog pin connected to X output
const int Y_pin1 = A1; // analog pin connected to Y output

const int SW_pin2 = 6; // digital pin connected to switch output
const int X_pin2 = A2; // analog pin connected to X output
const int Y_pin2 = A3; // analog pin connected to Y output

//Define the initial states
int state = 0;
int buttonState = 1;

//Define the potentiometer
int const potPin = A5;
int potValue;
int angle;

void setup()
{
  BTSerial.begin(9600);
  Serial.begin(9600);

  //Define pushButtons as INPUTS
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);

  //Define Joystick as INPUTS and inital setting (HIGH)
  pinMode(SW_pin1, INPUT);
  digitalWrite(SW_pin1, HIGH);
  pinMode(SW_pin2, INPUT);
  digitalWrite(SW_pin2, HIGH);


}

void loop()
{
  if (BTSerial.available() > 0)
  {
    // Checks whether data is comming from the serial port
    state = BTSerial.read(); // Reads the data from the serial port
  }

  //Buttons Code
  int buttonState1 = digitalRead(buttonPin1);      // read the input pin:
  int buttonState2 = digitalRead(buttonPin2);      // read the input pin:
  int buttonState3 = digitalRead(buttonPin3);      // read the input pin:
  int buttonState4 = digitalRead(buttonPin4);      // read the input pin:
  int buttonState5 = digitalRead(SW_pin1);      // read the input pin:
  int buttonState6 = digitalRead(SW_pin2);      // read the input pin:


  //Reading the potentiometer
  potValue = analogRead(potPin);
  angle = map(potValue, 0, 1023, 0, 120);

  //Reading the Joysticks
  int X_pos1 = analogRead(X_pin1);
  int X_pos2 = analogRead(X_pin2);
  int Y_pos1 = analogRead(Y_pin1);
  int Y_pos2 = analogRead(Y_pin2);


  //Communication
  //Op
  if (buttonState6 == LOW)
  {
    BTSerial.write('q');
  }
  else if (buttonState6 == HIGH)
  {
    BTSerial.write('w');
  }

  //RGB LED
  if (angle <= 20)
  {
    BTSerial.write('e');
  }
  else if ((angle >= 21) && (angle <= 40))
  {
    BTSerial.write('r');
  }
  else if ((angle >= 41) && (angle <= 60))
  {
    BTSerial.write('t');
  }
  else if ((angle >= 61) && (angle <= 80))
  {
    BTSerial.write('y');
  }
  else if ((angle >= 81) && (angle <= 100))
  {
    BTSerial.write('u');
  }
  else if ((angle >= 101) && (angle <= 120))
  {
    BTSerial.write('i');
  }
  if (X_pos2 <= 300)
  {
    BTSerial.write('o');
  }
  else if (X_pos2 >= 700)
  {
    BTSerial.write('p');
  }


}

wiring.PNG

Write a simple program (no Bluetooth) to test just the servo functions, and get that working perfectly before adding anything else.

If you have trouble with the first part, post again.

Servos need periodic pulses to make them hold still. Those pulses are interrupt driven.

SoftwareSerial disables interrupts for relatively long periods of time, while it processes each incoming serial byte.

Disconnect the bluetooth device, and comment out the code that reads from it. Do the servos still shake?