Control motor simultaneously using bluetooth module and joystick

Hi, i have a project that control motor using bluetooth module and also joystick. Both of them work fine if seperately. But i want to combine both of the program together so that the robot can be controlled by joystick or Bluetooth module. Here is my code:

int RPWMR = 5;
int LPWMR = 6;
int RPWML = 9;
int LPWML = 10;
int enA = 2;
int in1 = 3;
int in2 = 4;
int data;

int JOY_XL = A2;

int motorSpeedA = 0;
int motorSpeedB = 0;

void setup() {
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT); 
}

void loop() {

  if(Serial.available() > 0)
  {
    data = Serial.read();
    Serial.println(data);    //Uncomment this line to print the incomming data

    if(data == 'F')
    {
      digitalWrite(5,HIGH);
      digitalWrite(6,LOW);

      digitalWrite(9,LOW);
      digitalWrite(10,HIGH);
    }
    if(data == 'B')
    {
      digitalWrite(5,LOW);
      digitalWrite(6,HIGH);

      digitalWrite(9,HIGH);
      digitalWrite(10,LOW);
    }
    if(data == 'R')
    {
      digitalWrite(5,LOW);
      digitalWrite(6,HIGH);

      digitalWrite(9,LOW);
      digitalWrite(10,HIGH);
    }
    if(data == 'L')
    {
      digitalWrite(5,HIGH);
      digitalWrite(6,LOW);

      digitalWrite(9,HIGH);
      digitalWrite(10,LOW);
    }
    if(data == 'S')
    {
      digitalWrite(5,LOW);
      digitalWrite(6,LOW);

      digitalWrite(9,LOW);
      digitalWrite(10,LOW);
    }  
  }

  int xAxis = analogRead(A0); // Read Joysticks X-axis
  int yAxis = analogRead(A1); // Read Joysticks Y-axis
  
  int xAxisL = analogRead(JOY_XL);
  int motorSpeed = map(xAxisL, 0, 1023, -255, 255);

  motorSpeedA = 0;
  motorSpeedB = 0;
 analogWrite(RPWMR, 0);
  analogWrite(LPWMR, 0);
  analogWrite(RPWML, 0);
 analogWrite(LPWML, 0);

  if (motorSpeed > 0) {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
  } else {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
  }
  analogWrite(enA, abs(motorSpeed));

  // Y-axis used for forward and backward control
  if (yAxis < 470) {
  //Forward
  digitalWrite(RPWMR, LOW);
  digitalWrite(LPWMR, HIGH);
  digitalWrite(RPWML, HIGH);
  digitalWrite(LPWML, LOW);
  // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
  motorSpeedA = map(yAxis, 470, 0, 0, 255);
  motorSpeedB = map(yAxis, 470, 0, 0, 255);
  }

  else if (yAxis > 520) {
  //Reverse
  digitalWrite(RPWMR, HIGH);
  digitalWrite(LPWMR, LOW);
  digitalWrite(RPWML, LOW);
  digitalWrite(LPWML, HIGH);
  // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
  motorSpeedA = map(yAxis, 550, 1023, 0, 255);
  motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  }

  // If joystick stays in middle the motors are not moving
  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
  }

  if (xAxis < 470) {
  // Turn right
  digitalWrite(RPWMR, HIGH);
  digitalWrite(LPWMR, LOW);
  digitalWrite(RPWML, HIGH);
  digitalWrite(LPWML, LOW);
  // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
  motorSpeedA = map(yAxis, 470, 0, 0, 255);
  motorSpeedB = map(yAxis, 470, 0, 0, 255);
  }

  else if (xAxis > 550) {
  // Turn left
  digitalWrite(RPWMR, LOW);
  digitalWrite(LPWMR, HIGH);
  digitalWrite(RPWML, LOW);
  digitalWrite(LPWML, HIGH);
  // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
  motorSpeedA = map(yAxis, 550, 1023, 0, 255);
  motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  }

  if (motorSpeedA < 70) {
    motorSpeedA = 0;
  }
  if (motorSpeedB < 70) {
    motorSpeedB = 0;
  }
  delay(100);
}

I moved your topic to an appropriate forum category @kazama777.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

I can't read all your code now, so if you've already done this what I suggest never mind.

Make one section of code to control the motors using single characters like will come from the blue tooth.

Now write another section of code for the joysticks, and have that section make fake characters exactly according to their meaning had they really come from the blue tooth.

So the motors respond, not knowing who fed them the characters.

This can be useful also as it slots immediately and easily into simulation of either control mechanism by simply typing characters into the serial monitor and have them processed as if they came from the blue tooth.

Divide and conquer. One code section or function if you wanna make it pretty to control the motors coupled with several input methods using the same interface, whether the moves were initiated by joystick or blue tooth or sensors or whatever.

a7

i dont really understand the term of feeding them fake characters though

Like this pseudocode:

// listen for the blue tooth
    if (blue tooth character available) {

// read the blue tooth character
        commandChar = blueToothRead();
    }

// take  peek at the joystick
    if (joystickX > 768) commandChar = 'L';   // fake L as if blue tooth sent it






// later, either way you got 'L'

    if (commandChar == 'L') {

// whatever makes the thing go left

    }   

By fake I just meant artificial, that is you made it, and let it look like it was delivered from elsewhere.

HTH

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.