! HELP ! Communication between two HC-05 Bluetooth - Project for College

Hello everyone,

I'm doing a remote control car project for college.
The project consists of:

Remote Control:

1 Bluetooth module to send the information to the car.
2 Joysticks to send signals to the Arduino

Car:

1 Bluetooth module to receive control information;
1 servo motor - move the direction of the car
1 H-Bridge - To receive the Bluetooth signals and send to the motors.
2 Motors - Receives the signals from the H-bridge and starts driving the car.

My problem is in programming.
I can get the servo to move as I wish, but when I try to set the engine motion, I do not succeed.

When I try to send signals to my engine, it simply blends in with the signals from my servo, and ends the servo and the motor being controlled by the 2 Joysticks, instead of each Joystick controlling its due component.

Below is my program.
I went back to square one.
At this point I can control the Servo motor normally.
I just need some help in developing programming to control the engines.

Note: The connections are correct and the communication is perfect, with no delay.

My only problem is to generate the codes to move the motors, without affecting the movement of the servo

I really need help, this project is for Monday 05/13 and I'm panic ..... :confused:

Master:

int state = 0;
int potValue = 0;
int pot2Value = 0;

void setup() {
Serial.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
state = Serial.read(); // Reads the data from the serial port
}

// Reading the potentiometer
potValue = analogRead(A0);
int potValueMapped = map(potValue, 0, 1023, 0, 255);
Serial.write(potValueMapped); // Sends potValue to servo motor
delay(10);

// Reading the potentiometer 2
pot2Value = analogRead(A1);
Serial.write("{");
Serial.write(pot2Value);
Serial.write("}");
delay(10);

}

Slave:

#include <Servo.h>

Servo myServo;
int motorA = 5; // velocidade motor A - de 0 a 255
int motorB = 6; // velocidade motor B - de 0 a 255
int dirA = 7; // direcao do motor A - HIGH ou LOW
int dirB = 8; // direcao do motor B - HIGH ou LOW
int state = 20;
int pot2Value = 0;
boolean flag_pot2 = false;

void setup() {
 pinMode(motorA, OUTPUT);
 pinMode(motorB, OUTPUT);
 pinMode(dirA, OUTPUT);
 pinMode(dirB, OUTPUT);
 myServo.attach(9);
 Serial.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
  state = Serial.read(); // Reads the data from the serial port
   if(state == "{")
   {
     flag_pot2 = true;
   }
    if(flag_pot2 && state != "{")
    {
      analog2Value = state;
    }
    else
    {
      flag_pot2 = false;
    }
    //Limpa Buffer
    state = " ";
}
// Controlling the servo motor
myServo.write(state);
delay(10);

if(pot2Value < 512) {
 //Reverso
 int velocidade = map(pot2Value, 511, 0, 0, 255);
 digitalWrite(dirA, LOW); // SENTIDO DE ROTACAO
 digitalWrite(dirB, LOW);
 analogWrite(motorA, velocidade); // VELOCIDADE
 analogWrite(motorB, velocidade);
} else {
   //Frente
   int velocidade = map(pot2Value, 512, 1023, 0, 255);
 digitalWrite(dirA, HIGH); // SENTIDO DE ROTACAO
 digitalWrite(dirB, HIGH);
 analogWrite(motorA, velocidade); // VELOCIDADE
 analogWrite(motorB, velocidade);
 
}

}

Please, I really need some help solving this ... :sob:

Mestre.ino (655 Bytes)

Escravo.ino (1.46 KB)

Also here

Serial.write(potValueMapped); // Sends potValue to servo motor
Serial.write("{");
Serial.write(pot2Value);
Serial.write("}");

Why two different ways of sending the data?

The write() method takes a byte, or an array of bytes. What do you supposed it does with your int?

It would be far better to read Serial Input Basics - updated - Introductory Tutorials - Arduino Forum and send the data like "<1:230>" or "<2:101>".

Initially when I tried to send it the same way, the information was mixing, I tried to send it like this to see if that would work, but it did not work.

I do not have much experience with this language, in fact, no experience, it is very difficult to understand what I should to do.

You are suggesting, then, that I adopt only one method of sending, and that through the link that indicated to me, I try to send the information like this "<1: 230>" or "<2: 101>".

I will try.

If you can give me some other tip, I would be very grateful.

Thank you.

I removed the modification from my program and returned it to the starting point.

I would just like to know from this point how to send the data from potentiometer 2 to the slave, without this value being mixed with the data of potentiometer 1.

Master:

int state = 0;
int potValue = 0;
int pot2Value = 0;

void setup() {
Serial.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
state = Serial.read(); // Reads the data from the serial port
}

// Reading the potentiometer
potValue = analogRead(A0);
int potValueMapped = map(potValue, 0, 1023, 0, 255);
Serial.write(potValueMapped); // Sends potValue to servo motor
delay(10);

// Reading the potentiometer 2
pot2Value = analogRead(A1);
delay(10);

}

Slave:

#include <Servo.h>

Servo myServo;
int motorA = 5; // velocidade motor A - de 0 a 255
int motorB = 6; // velocidade motor B - de 0 a 255
int dirA = 7; // direcao do motor A - HIGH ou LOW
int dirB = 8; // direcao do motor B - HIGH ou LOW
int state = 20;
int pot2Value = 0;

void setup() {
 pinMode(motorA, OUTPUT);
 pinMode(motorB, OUTPUT);
 pinMode(dirA, OUTPUT);
 pinMode(dirB, OUTPUT);
 myServo.attach(9);
 Serial.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
  state = Serial.read(); // Reads the data from the serial port
}
// Controlling the servo motor
myServo.write(state);
delay(10);

if(pot2Value < 512) {
 //Reverso
 int velocidade = map(pot2Value, 511, 0, 0, 255);
 digitalWrite(dirA, LOW); // SENTIDO DE ROTACAO
 digitalWrite(dirB, LOW);
 analogWrite(motorA, velocidade); // VELOCIDADE
 analogWrite(motorB, velocidade);
} else {
  //Frente
 int velocidade = map(pot2Value, 512, 1023, 0, 255);
 digitalWrite(dirA, HIGH); // SENTIDO DE ROTACAO
 digitalWrite(dirB, HIGH);
 analogWrite(motorA, velocidade); // VELOCIDADE
 analogWrite(motorB, velocidade);
 
}

}

I would just like to know from this point how to send the data from potentiometer 2 to the slave, without this value being mixed with the data of potentiometer 1.

You have made no effort to incorporate the methods of Serial Input Basics.

If you really think you must send two values, in binary, put them in, and send, an array.

Keep in mind that serial data is NOT guaranteed to be delivered without corruption. So, if one byte of your array gets lost, the receiver will use the speed value of one packet and the direction value of the next packet as the values for the direction and speed. Your car will not perform anything like you expect.

It is far better to send the data in ASCII format, where one lost byte won't be as important. You might see a momentary blip in speed or direction, if you send "<S:123, D:237>" and the first '2' gets lost. But, that will corrected when the next packet arrives.