Hi, I have a problem, i have 2 hc-05 modules and I setted them to connect automatically.
Few times ago i created an RC car controlled via android app, now i would like to control my car with another Arduino and a joystick module. I tried to write the code but when I power on both the Arduino they are connecting and disconnecting every 2-3 second... Any ideas?
Any ideas?
You haven't described the hardware (well enough).
You haven't posted any code.
So, yeah, I have some ideas, but I'll be civil and keep them to myself.
This is the sketch for the slave module, the output pin are for the motors (pwm speed and direction).
I used different values to make the program understand if the data are for the motors or the servo.
#include <Servo.h>
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(0, 1);
Servo myservo;
int vel;
void setup() {
pinMode (2, OUTPUT);
pinMode (3, OUTPUT);
pinMode (4, OUTPUT);
pinMode (5, OUTPUT);
myservo.attach(6);
BTSerial.begin(9600);
}
void loop() {
if (BTSerial.available()) {
int rec = (BTSerial.read());
if (rec > 0 && rec < 255) {
digitalWrite(3, HIGH);
digitalWrite(5, HIGH);
vel = map (rec, 0, 255, 255, 0);
analogWrite(2, vel);
analogWrite(4, vel);}
if (rec > 255 && rec < 510) {
digitalWrite(3, LOW);
digitalWrite(5, LOW);
vel = map (rec, 255, 510, 255, 0);
analogWrite(2, vel);
analogWrite(4, vel);}
if (rec > 550 && rec > 655) {
int pos = map (rec, 550, 655, 75, 180);
myservo.write(pos);
}
}
}
This is for the master module with the x and the y of the analog joystick.
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(0, 1);
void setup() {
BTSerial.begin(9600);
}
void loop() {
int x = analogRead(A0);
int y = analogRead(A1);
int vel = map (y, 0, 1023, 510, 0);
int pos = map (x, 0, 1023, 550, 655);
BTSerial.write(vel);
BTSerial.write(pos);
}
I also tried to send only one value but it do the same thing.
I hope that you can help me, maybe with another method, because i think that with this bt modules i can't send too many values per second.
SoftwareSerial BTSerial(0, 1);
Why are you doing SoftwareSerial on the hardware serial pins?
I tried other pins but it's the same thing
The code for reading the joysticks does not look right. The range of values is 0 to 1023, but that is from pulled all the way back to pushed all the way forward. Usually, one wants to map that to a range centered around 0, so pulled all the way back maps to a large negative number, centered maps to 0, and pushed all the way forward maps to a large positive number. (Of course, large is relative).
The write() method takes bytes. 510 is not a byte sized value.
Sending binary data makes reading and using it easier IF no data ever gets lost AND the values you are sending are byte-sized.
Sending ASCII data is slower, but MUCH easier to understand, debug, and perform error handling with.
Pitch the car in the garage for now. Concentrate on getting the communications working, with the blue tooth devices NOT connected to the hardware serial pins. Use the hardware serial pins, and the Serial instance, to debug your code.
if (rec > 0 && rec < 255) {
Those values do not match the range of values you think you are sending, so the range is strange. The only values that will be in rec that are not in that range are 0 and 255. Why are you excluding what could be perfectly reasonable values?
vel = map (rec, 0, 255, 255, 0);
An unnecessarily complex way to accomplish vel = 255 - rec.
if (rec > 550 && rec > 655) {
The value in rec will NEVER be in this range. That is a strange "range" anyway. The test will be true only for values of 656 or more.