I have two servos connected to a slave device hc-05 and the master controller is also connected to a Bluetooth hc-05. The controller can correctly tell the slave to turn on and off an LED, control the RGB colors but for some reason I've had the hardest time with some servo motor controls. I know the servos work fine because when the controller is off I can set the initial position of the servos and they move just fine so its not a wiring or hardware issue. The moment the controller is turned on the servos just start shaking and vibrating and dont listen to any commands. As a test I have completely removed any mention of the servos from the loop yet as soon as I turn on the controller regardless they still start to spasm. Can anyone identify what in my code is making them do this?
Slave code
#include <SoftwareSerial.h>
#include <Servo.h>
Servo servo1;
Servo servo2;
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
int servo1_pin = 8;
int servo2_pin = 9;
int servoPosition = 160;
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);
servo2.attach(servo2_pin);
servo1.write(servoPosition);
servo2.write(servoPosition);
}
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
}
}
}
void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}
master code
#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 (buttonState3 == LOW)
{
BTSerial.write('o');
}
else if (buttonState4 == LOW)
{
BTSerial.write('p');
}
}