I recently started a project of controlling 2 servos with a joystick over Bluetooth using two HC-05 modules. I started out by being able to control the servos with the joystick directly connected to the board. I have done a large amount of searching online, but haven't found anything helpful. the closest thing I found was on this forum.HC-05 communication
Is there more to this or just a general chit chat on your passtime ....?
Use that example. What's the trouble?
Which Arduino or other compatible MCU boards will future engineers use?
I tried to modify the code to fit my needs.`
bluetooth_cannon2.1.ino (890 Bytes)
bluetooth_cannon1.1.ino (793 Bytes)
Please post your code in your post, and not as a download attachment
You should post code by using code-tags so that your code appears in a window which allows for easy reading and copying.
There is an automatic function for doing this in the Arduino-IDE. It's
just three steps
1)press Ctrl-T for autoformatting your code
2)do a rightclick with the mouse and choose "copy for forum"
3)paste clipboard into write-window of a posting
Alternatively, the code tags can be found by clicking on the code icon in your posting window.
![]()
Here it is`#include <Servo.h>
#include <SoftwareSerial.h>0
SoftwareSerial BTserial(0, 1); // RX | TX
int Laser = 8;
int LED = 9;
int Xval;
int Yval;
Servo Xservo; // create servo object to control a servo
Servo Yservo;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
BTserial.begin(38400);
Xservo.attach(12); // attaches the servo on pin 9 to the servo object
Yservo.attach(11);
pinMode(Laser, OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
if (BTserial.available())
{
char inval = Serial.read();
if (inval == 'a')
{
Serial.println("engage cannon");
digitalWrite(Laser, HIGH);
delay(500);
Xval = map(Xval, 0, 1023, 0, 180);
Xservo.write(Xval);
Yval = map(Yval, 0, 1023, 0, 180);
Yservo.write(Yval);
}
}
}
``#include <Servo.h>
#include <SoftwareSerial.h>0
SoftwareSerial BTserial(0, 1); // RX | TX
Servo Xservo; // create servo object to control a servo
Servo Yservo;
int Xpin = A4; // analog pin used to connect the potentiometer
int Ypin = A5;
#define button 8
int Xval; // variable to read the value from the analog pin
int Yval;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
BTserial.begin(38400);
Xservo.attach(12); // attaches the servo on pin 9 to the servo object
Yservo.attach(11);
pinMode(button, INPUT);
}
void loop() {
Xval = analogRead(Xpin);
Yval = analogRead(Ypin);
if (BTserial.available())
{
Serial.write(Xval);
Serial.write(Yval);
if (button == HIGH)
{
Serial.write("a");
delay(500);
}
}
}`
So what exactly is the issue? Powering the servos? Finding a board that can do exactly what you want?
the problem is getting the board with the joystick to communicate with the board with the servos. I am trying to control servos attached onto a gimbal mounting for a rover.
What with?
SoftwareSerial with 0 and 1 is a problem. (Don't use D0 or D1.)
I'm using two Arduino uno boards with a HC-05 attached to each one
OK. No SoftwareSerial using D0/D1.
Perhaps you should dispense with the joystick/servo aspects and concentrate on getting comms between your two boards.
There are two ways to do that - one is with BIND, which is harder to set up, but more dependable (IMO) and then there's the 'easier' way.
I changed the pins from 0 and 1, to 2 and 3. when the boards are powered the lights on the HC-05 modules blink in unison.
Write a couple sketches. With one the transmitter will send a letter ('X') every couple of seconds or so. With the other, the receiver will toggle an LED (pin 13?) each time it receives an 'X'.
Just a suggestion.
I've used these modules in the past with just buttons, but not with joysticks. I know the modules are connected, but I don't know why the servos aren't moving.
I would forget about the Bluetooth for a while and just focus on working code with the joysticks, the servos, and one arduino.
Can you please post the code without bluetooth which does what you want.
As you saw in the previously referenced thread, adding bluetooth communications using the techniques of Serial Input Basics, is quite straightforward.
#include <Servo.h>
Servo Xservo; // create servo object to control a servo
Servo Yservo;
int Xpin = A4; // analog pin used to connect the potentiometer
int Ypin = A5;
#define button 8
int Xval; // variable to read the value from the analog pin
int Yval;
void setup() {
Xservo.attach(12); // attaches the servo on pin 9 to the servo object
Yservo.attach(11);
pinMode(button, INPUT);
}
void loop() {
Xval = analogRead(Xpin); // reads the value of the potentiometer (value between 0 and 1023)
Xval = map(Xval, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
Xservo.write(Xval);
delay(10);
Yval = analogRead(Ypin); // reads the value of the potentiometer (value between 0 and 1023)
Yval = map(Yval, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
Yservo.write(Yval);
delay(10);
// waits for the servo to get there
}
Don't do 2 Bluetooth writes. It is easy to get out of sync.
Sending one joystick's values.
#include <SoftwareSerial.h>
const byte xPin = A0;
const byte yPin = A1;
SoftwareSerial ss(4, 7);
void setup()
{
Serial.begin(115200);
ss.begin(9600);
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 1000;
if (millis() - timer >= interval)
{
timer = millis();
int xValue = analogRead(xPin);
int yValue = analogRead(yPin);
Serial.print("x value = ");
Serial.print(xValue);
Serial.print(" y value = ");
Serial.println(yValue);
char buffer[15];
snprintf(buffer, 14, "%d,%d", xValue, yValue);
Serial.println(buffer);
ss.println(buffer);
}
}
Receive one joystick data. Uses methods from the serial input basics tutorial: :
#include <SoftwareSerial.h>
int xValue = 0;
int yValue = 0;
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
SoftwareSerial ss(4, 7);
void setup()
{
Serial.begin(115200);
ss.begin(9600);
}
void loop()
{
recvWithEndMarker();
//showNewData();
if (newData)
{
parseData();
displayData();
newData = false;
}
}
void recvWithEndMarker()
{
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (ss.available() > 0 && newData == false)
{
rc = ss.read();
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData()
{
if (newData == true)
{
Serial.print("This just in ... ");
Serial.println(receivedChars);
//newData = false;
}
}
void parseData()
{
char *strings[2]; // an array of pointers to the pieces of the above array after strtok()
char *ptr = NULL; byte index = 0;
ptr = strtok(receivedChars, ","); // delimiter comma
while (ptr != NULL)
{
strings[index] = ptr;
index++;
ptr = strtok(NULL, ",");
}
//Serial.println(index);
/*
// print all the parts
Serial.println("The Pieces separated by strtok()");
for (int n = 0; n < index; n++)
{
Serial.print("piece ");
Serial.print(n);
Serial.print(" = ");
Serial.println(strings[n]);
}
*/
// convert string data to numbers, map to servo ranges
xValue = atoi(strings[0]);
yValue = atoi(strings[1]);
}
void displayData()
{
Serial.print("x value = ");
Serial.print(xValue);
Serial.print(" y value = ");
Serial.print(yValue);
Serial.println(); // blank line
}
I just tried this code and only the joystick end shows data on the serial monitor.
The issue is with your hardware and not the code. The receiving code works for me with a terminal app on my phone.
Are you using the software serial pins from the code given to you by @groundFungus? Is the serial monitor set to 115200 baud?
You previously said that you can communicate between the two modules.
Can you still do that? If so, can you post some send and receive code which works properly for you?
What blink pattern do you see on the two modules?