Control Servo Motor With tow different Mehodes (Bluetooth and Joystick)

Hello Every one;
I am new in arduino and i have project to make an robotic arm;
we have created every thing but we have an problem with the programming , we wnnt to controol our arm using blutooth or with joystick at the same time
what i mean , that if blutooth send a value we will control it with Blutooth , if not it will be controled using joystick moudle
but i does not work just the blutooth is working , the code is below if can any one help.
I am Using arduino Uno with KY-023 and HC-06 and 4 x Servo , 1 potenmater to set the speed.
Thank you.

//    @Author TAU:Muhammed El Ibrhaim;
// Die liberiry, die wie es benuzen werden

#include <Arduino.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include <math.h>

// PIN-Definition

#define S1PIN 10
#define S2PIN 11
#define S3PIN 12
#define S4PIN 13
#define Joy_Y A0
#define Joy_X A1
#define Joy_B A2
#define PotM  A3
#define L1    200 // Test
#define W1    150 // Test
#define bthRX 3
#define bthTX 2
/*
#define MAX_SAngle    90
#define MAX_Speed    10
#define MIN_Speed    2000
#define MAX_Distance 350
*/

// Varibalen und device definition

SoftwareSerial bth( bthTX , bthRX ); //  Arduino RX  , TX
Servo S1 , S2 , S3 , S4;
String com;
int S1POS , S2POS , S3POS , S4POS;
int S1PPOS , S2PPOS , S3PPOS , S4PPOS;
float angle1 , angle2 , angle3;
int Joy_X_Val , Joy_Y_Val , Joy_B_Val ;
int delay_val = 20;
float Distance = 0.00;
int joy = 0;

boolean states = false;

// wenn Atduion ist zu erste dingen die gemacht werden

void setup() {
  
  S1.attach(S1PIN);
  S1PPOS = S1.read();
  S2.attach(S2PIN);
  S2PPOS = S2.read();
  S3.attach(S3PIN);
  S3PPOS = S3.read();
  S4.attach(S4PIN);
  S4PPOS = S4.read();

  pinMode (Joy_X, INPUT);
  pinMode (Joy_Y, INPUT);
  pinMode (Joy_B, INPUT);
  pinMode (PotM , INPUT);

  digitalWrite(Joy_B , HIGH);

  bth.begin(38400);  

}

//Unser Robot funktonen

void loop() {

  Joy_X_Val = analogRead(Joy_X);
  Joy_Y_Val = analogRead(Joy_Y);
  Joy_B_Val = analogRead(Joy_B);
  delay_val = analogRead(PotM);
  com = bth.readString();
  
  if(bth.available() >= 0){

    

    if(com.startsWith("s1")){                   // Z servo
      String CMD = com.substring(2 ,com.length());
      S1POS = CMD.toInt();
      if(S1POS > S1PPOS){
        for(int i = S1PPOS ; i < S1POS ; i++){
          S1.write(i);
          delay(delay_val);
        }
      }
      if(S1POS < S1PPOS){
        for(int i = S1PPOS ; i > S1POS ; i--){
          S1.write(i);
          delay(delay_val);
        }
      S1PPOS = S1POS;
    }
  }

    if(com.startsWith("s2")){                   // X servo
      String CMD = com.substring(2 ,com.length());
      S2POS = CMD.toInt();
      if(S2POS > S2PPOS){
        for(int i = S2PPOS ; i < S2POS ; i++){
          S2.write(i);
          delay(delay_val);
        }
      }
      if(S2POS < S2PPOS){
        for(int i = S2PPOS ; i > S2POS ; i--){
          S2.write(i);
          delay(delay_val);
        }
      S2PPOS = S2POS;
    }
  }

    if(com.startsWith("s3")){                     // Rotation Servo , Y servo
      String CMD = com.substring(2 ,com.length());
      S3POS = CMD.toInt();
      if(S3POS > S3PPOS){
        for(int i = S3PPOS ; i < S3POS ; i++){
          S2.write(i);
          delay(delay_val);
        }
      }
      if(S3POS < S3PPOS){
        for(int i = S3PPOS ; i > S3POS ; i--){
          S3.write(i);
          delay(delay_val);
        }
      S3PPOS = S3POS;
    }
  }

    if(com.startsWith("s4")){                         // Gripper Servo
      String CMD = com.substring(2 ,com.length());
      S1POS = CMD.toInt();
      if(S4POS > S4PPOS){
        for(int i = S4PPOS ; i < S4POS ; i++){
          S4.write(i);
          delay(delay_val);
        }
      }
      if(S4POS < S4PPOS){
        for(int i = S4PPOS ; i > S4POS ; i--){
          S2.write(i);
          delay(delay_val);
        }
      S4PPOS = S4POS;
    }
  }

    if(com.startsWith("speed")){                         // Die Geschwindeigkeit des Servo
      delay_val = com.substring(5 , com.length()).toInt();
    }
    
  }
 
  // Control with Joystick KY-04
  if(bth.available() <= 0){
    angle1 = ((90 - S1POS ) * PI) / 180;
    angle2 = ((90 - S2POS ) * PI) / 180;
    Joy_X_Val = map(Joy_X_Val , 0 , 1023 , 180 , 0);
    Joy_Y_Val = map(Joy_Y_Val , 0 , 1023 , 180 , 0);
    Joy_B_Val = map(Joy_B_Val , 0 , 1023 , 0 , 500);
    delay_val = map(delay_val, 0 , 1023 , 20 , 70);

    if(Joy_X_Val > 95  ){

        delay(delay_val);
    }

    if(Joy_Y_Val > 95 && S3PPOS < 180){
        S3PPOS++;
        S3.write(S3POS);
        delay(delay_val);
    }

    if(Joy_X_Val < 95){
      
      delay(delay_val);
    }

    if(Joy_Y_Val < 95 && S3PPOS > 0 ) {
      S3PPOS--;
      S3.write(S3POS);
      delay(delay_val);
    }

  if(Joy_B_Val < 10 && S4PPOS > 90){
    S4PPOS--;
    S4.write(S4PPOS);
    delay(20);
   }else if(S4PPOS < 90) {
     S4PPOS++;
     S4.write(S4PPOS);
     delay(20);
   }

  }

}

Proper use of code tags on your first post. YAY! Karma++

What is sending bluetooth commands? How often is it sending those commands?

I would want to determine if the code is running through the bluetooth section or the joystick section.
If you can use the serial monitor, do some serial prints to confirm that it is switching to joystick when you expect it to switch.
If you cannot use the serial monitor, use one of your servos as an indicator.
Write 30 degrees to S4 when it is inside the bluetooth section, and write 150 degrees to S4 when it is in the joystick section.

vinceherman:
Proper use of code tags on your first post. YAY! Karma++

What is sending bluetooth commands? How often is it sending those commands?

I would want to determine if the code is running through the bluetooth section or the joystick section.
If you can use the serial monitor, do some serial prints to confirm that it is switching to joystick when you expect it to switch.
If you cannot use the serial monitor, use one of your servos as an indicator.
Write 30 degrees to S4 when it is inside the bluetooth section, and write 150 degrees to S4 when it is in the joystick section.

Yeah Thank you!
Ok the comands is sending unsang andriod app that bluid with mit app invertor
i used the serial monitor to print the valuse and all valuse were correct wheen is test it
and i tried to delet bluetooth control codes and keep the joystick codes its work well
but when i add the blutooth codes the joystick or the servo woulld not move when i send valuse form jouystick just if we send value form Bluetooth
and i am sorry for may bad English and thank you form your time to help me.

Thanks for every one how tried to help me or readed the Topic i have sloved the issue and will explain it , and i will publish the code and the other componentes to creat this Robotic arm in my GitHub.

The Problem was when the program is trying to read String from blutooth moodle and the defalut for it is about 2 secondes if there is no avaliable bites to read.

It can be sloved by add if(availabl()>) funcktion to code the cheak if ther any data to read and then put readstirng in it but using this methode it will have some data loss when it cheking for available data and this so we have new problem then its sloved by adding timeout(1) to the connected blutoth module where we it is no data loss.

We can slove the problem in another way, in blutooth module there is a State or En pin and this pin it return HIHG when module is connected to a device and LOW if is not connected so we can declare an input mode and read it (digitalRead) then we can make logic if(state == HIGH ){ Control with Bluetooth} if(state == LOW){control with joystick} . so i finde this is better than cheack if ther are an available data to read because we can directly and easly make the control to the arm.

I am sorry for my bad English :slight_smile: , i triying to make it better.

OK , so If any one have a qustion or did not andrestand what i wirte because of my bad English :slight_smile: make your salfe comfortable to ask me in any time.