0

I am trying to control a robot, where I have one HC05 module as a master connected to an Arduino Uno with an attached joystick. This Arduino sends data signals that will run blocks of code on the slave Arduino, which will have four servo motors connected to it, but for testing purposes, only two are connected. This Arduino also has an HC05 Bluetooth module connected to it, but it is configured as a slave. I have tried everything to get the Bluetooth functionality to work, but it does not work! If you are interested in the background of this, this is for my Rapid Assembly and Design project for school, which I have a report on soon. We are creating a robot that can operate on land and traverse the sea, so we are making it waterproof as well. P.S. both modules are HC05 modules.

Master Code:

#include <SoftwareSerial.h>
int yPin = A0;
int xPin = A1;
int buttonPin = 2;
int xPosition = 0;
int yPosition = 0;
int buttonState = 0;
const int RX_PIN = 10;
const int TX_PIN = 9;
SoftwareSerial BL(RX_PIN, TX_PIN);
const int BLUETOOTH_BAUD_RATE = 9600;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  BL.begin(39600);
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  xPosition = analogRead(xPin);
  yPosition = analogRead(yPin);
  buttonState = digitalRead(buttonPin);
  /*if (buttonState == 1) {
    if (xPosition >= 450 & xPosition <= 550) {
      if (yPosition >=450 & yPosition <= 550) {
        Serial.write("a1 ");
        bluetooth.write("a1");
      }
      if (yPosition > 550) {
        Serial.write("a2 ");
        bluetooth.write("a2");
      }
      if (yPosition < 450) {
        Serial.write("a3 ");
        bluetooth.write("a3");
      }
    }
    if (yPosition >= 450 & yPosition <= 550) {
      if (yPosition >= 450 & yPosition <= 550) {
        Serial.write("b1 ");
        bluetooth.write("b1");
      }
      if (xPosition > 550) {
        Serial.write("b2 ");
        bluetooth.write("b2");
      }
      if (xPosition < 450) {
        Serial.write("b3 ");
        bluetooth.write("b3");
      }
    }
  }
  delay(100);
} */
  if (buttonState == 1) {
    if (xPosition >= 450 & xPosition <= 550) {
      if (yPosition >= 450 & yPosition <= 550) {
        BL.write("a1");
        BL.print("a1");
        Serial.print("a1");
        Serial.write("a1");
      }
      if (yPosition > 550) {
        BL.write("a2");
        BL.print("a2");
        Serial.print("a2");
        Serial.write("a2");
      }
      if (yPosition < 450) {
        BL.write("a3");
        BL.print("a3");
        Serial.print("a3");
        Serial.write("a3");
      }
    }
    if (yPosition >= 450 & yPosition <= 550) {
      if (xPosition >= 450 & xPosition <= 550) {
        BL.write("b1");
        BL.print("b1");
        Serial.print("b1");
        Serial.write("b1");
      }
      if (xPosition < 450) {
        BL.write("b2");
        BL.print("b2");
        Serial.print("b2");
        Serial.write("b2");
      }
      if (xPosition > 550) {
        BL.write("b3");
        BL.print("b3");
        Serial.print("b3");
        Serial.write("b3");
      }
    }
  }
}

Slave Code:

// Global Area 
#include <Servo.h>
#include <SoftwareSerial.h>
int xPin = A1; // X connects to analog pin
int yPin = A0; // Y connects to analog pin
int buttonPin = 2; // Digital input pin for joystick state
int xPosition = 0; // Digital input pin for joystick X
int yPosition = 0; //  Digital input pin for joystick Y
const int RX_PIN = 10;
const int TX_PIN = 9;
int motorBRPin = 3;
int motorBLPin = 4;
int motorPRPin = 6;
int motorPLPin = 5;
int buttonState = 0; // Joystick state variable
Servo motor_br; // Declaration of servo with name of motor
Servo motor_pr;
Servo motor_bl;
Servo motor_pl;
SoftwareSerial BL(RX_PIN, TX_PIN);
const int BLUETOOTH_BAUD_RATE = 9600;
String state = "";

void setup() {
  Serial.begin(9600); // Begin serial monitor
  BL.begin(9600);
  pinMode(xPin, INPUT); // Declaring xPin as an input
  pinMode(yPin, INPUT); // Declaring yPin as an input
  pinMode(buttonPin, INPUT_PULLUP); // Declaring buttonPin as an input
  motor_br.attach(motorBRPin); // Telling servo which pin our servo motor is attached to
  motor_bl.attach(motorBLPin);
  motor_pr.attach(motorPRPin);
  motor_pl.attach(motorPLPin);
  }
void loop() {
  xPosition = analogRead(xPin); // Declaring the value of xPosition; The value given by the xPin read by the board at the analog input pin
  yPosition = analogRead(yPin); // Declaring the value of yPosition; The value given by the yPin read by the board at the analog input pin
  buttonState = digitalRead(buttonPin); // Declaring the value of buttonState;
  //Printing for reference
  /*Serial.print("X: ");
  Serial.print(xPosition);
  Serial.print(" | Y: ");
  Serial.print(yPosition);
  Serial.print(" | Button: ");
  Serial.println(buttonState);*/
  // If statements, if joystick is not pressed and is being pushed up, the motor will run clockwise at full speed.
  /*if (buttonState == 1) {
    if (xPosition >= 450 & xPosition <= 550) {
      if (yPosition >= 450 & yPosition <= 550) {
        motor_br.write(90);
        motor_bl.write(90);
      }
      if (yPosition > 550) {
        motor_br.write(0);
        motor_bl.write(0);
      }
      if (yPosition < 450) {
      motor_br.write(0);
      motor_bl.write(0);
      }
    }
    if (yPosition >= 450 & yPosition <= 550) {
      motor_br.write(90);
      motor_bl.write(90);
      motor_pr.write(90);
      motor_pl.write(90);
      if (xPosition < 450) {
        motor_br.write(0);
        motor_bl.write(0);
        motor_pr.write(0);
        motor_pl.write(0);
      }
      if (xPosition > 550) {
        motor_br.write(180);
        motor_bl.write(180);
        motor_pr.write(180);
        motor_pl.write(180);
    }

    }

    /*else if (xPosition < 450) {
      motor_br.write(0);
      motor_bl.write(0);
      motor_pr.write(0);
      motor_pl.write(0);
    }
    else if (xPosition > 550) {
      motor_br.write(180);
      motor_bl.write(180);
      motor_pr.write(180);
      motor_pl.write(180);
    }
    else if (yPosition >= 450 & yPosition <= 550) {
      motor_br.write(90);
      motor_bl.write(90);
    }
    else if (yPosition < 550) {
      motor_br.write(180);
      motor_bl.write(180);
    }
    else if (yPosition > 450) {
      motor_br.write(0);
      motor_bl.write(0);
    }
  }
  if (buttonState == 1) {
    if (xPosition >= 450 & xPosition <= 550) {
      motor_br.write(90);
      motor_bl.write(90);
      motor_pr.write(90);
      motor_pl.write(90);
    }
    else if (xPosition < 450) {
      motor_br.write(0);
      motor_bl.write(0);
      motor_pr.write(0);
      motor_pl.write(0);
    }
    else if (xPosition > 550) {
      motor_br.write(180);
      motor_bl.write(180);
      motor_pr.write(180);
      motor_pl.write(180);
    }
  }
  else if (buttonState == 0) {
    if (xPosition >= 450 & xPosition <= 550) {
      motor_br.write(90);
      motor_bl.write(90);
      motor_pr.write(90);
      motor_pl.write(90);
    }
    else if (xPosition < 450) {
      motor_br.write(0);
      motor_bl.write(0);
      motor_pr.write(0);
      motor_pl.write(0);
    }
    else if (xPosition > 550) {
      motor_br.write(180);
      motor_bl.write(180);
      motor_pr.write(180);
      motor_pl.write(180);
    }
  }*/
    while (BL.available()) {
      state = BL.read();
      state = Serial.read();
      Serial.print(state);
    }
    if (state == "a1" or "b1") {
      motor_br.write(90);
      motor_bl.write(90);
      state = "";
    }
    if (state == "a2") {
      motor_br.write(0);
      motor_bl.write(0);
      state = "";
    }
    if (state == "a3") {
      motor_br.write(180);
      motor_bl.write(180);
      state = "";
    }
    if (state == "b2") {
      motor_br.write(0);
      motor_bl.write(0);
      state = "";
    }
    if (state == "b3") {
      motor_br.write(180);
      motor_bl.write(180);
      state = "";
    }
    state = "";
  }
  • 1
    can you specify the problem you are facing. – Vaibhav Apr 10 at 6:50
  • You use 2 very different baudrates for master and slave. Though I don't know, if that really matters, as long as you don't spam the interface. Why are you sending the data twice on the master, once as ASCII and once as byte data (by using print/write)? And why are you setting the state variable twice on the slave, once from the Bluetooth connection and once from the hardware Serial? – chrisl Apr 10 at 7:04
  • It seems, that you mixed the master and slave code together. What should the joystick code do at the slave (which should be the robot from your description)? – chrisl Apr 10 at 7:04
  • How did you connect everything? Please provide schematics or a wiring diagram – chrisl Apr 10 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.