Here is my code:
#include <SoftwareSerial.h>
#include <Servo.h> // servo library
Servo servo1; // servo control object
Servo servo2;
Servo servo3;
SoftwareSerial bluetooth(5, 6); // RX, TX
int servo1Pin = 11;
int servo2Pin = 10;
int servo3Pin = 9;
int motor1pin = 8;
int flag = 0; //Sets integer for HIGH or LOW LED case
String bdata = "";
char c = ' ';
void setup()
{
bluetooth.begin(9600);
pinMode(motor1pin, OUTPUT);
servo1.attach(servo1Pin);
servo2.attach(servo2Pin);
servo3.attach(servo3Pin);
}
void loop()
{
int position;
while (bluetooth.available()) {
c = bluetooth.read();
bdata += c;
//~~~~~~Servo 1~~~~~~~~
if (bdata == "open1")
{
//Opens Door
for (position = 0; position < 90; position += 1)
{
servo1.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
if (bdata == "close1")
{
//Closes Door
for (position = 90; position >= 0; position -= 1)
{
servo1.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
//~~~~~~Servo 2~~~~~~~~
if (bdata == "open2")
{
//Opens Door
for (position = 0; position < 90; position += 1)
{
servo2.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
if (bdata == "close2")
{
//Closes Door
for (position = 90; position >= 0; position -= 1)
{
servo2.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
//~~~~~~Servo 3~~~~~~~~
if (bdata == "open3")
{
//Opens Door
for (position = 0; position < 90; position += 1)
{
servo3.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
if (bdata == "close3")
{
//Closes Door
for (position = 90; position >= 0; position -= 1)
{
servo3.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
//~~~~~~Motor 1~~~~~~
if (bdata == "on")
{
flag = 1; //Turns LEDs on using a high digital signal
}
if (bdata == "off")
{
flag = 0; //Turns LEDs off using a low digital signal
}
}
digitalWrite(motor1pin, flag);
delay(1000);
bdata = "";
}
It is uploaded to an Arduino Uno where I have the 3 servos and DC Motor connected via breadboard. It is meant to power a 3d printed mechanical arm I have made. when read you can see it is also controlled via Bluetooth using a HC-05 module and phone. I am having trouble with it as the commands for servo 1 are working fine so is 3, but when command for servo 2 is sent from phone servo 3 moves instead and servo 3 command also moves servo 3. I have already tried looking at the variable and the commands on my phone by nothing is sticking out to me. so here I am. Can anyone help?