Hello! I'm a very newbie to Arduino. It hasn't even been a month since I started and I don't know a lot of things. That's why I'm asking for your help.
I have one Arduino Uno, one Arduino Leonardo, one Funduino Joystick Shield and 2 HC05 modules. I wanted to make a wireless joystick for myself using these materials. I set the HC05s as Master and Slave. I connected the Joystick Shield and Slave HC05 to Arduino Uno.
I put this code inside the Arduino Uno:
#define joy_x A0
#define joy_y A1
#define btn_a 2
#define btn_b 3
#define btn_c 4
#define btn_d 5
#define btn_e 6
#define btn_f 7
#define joy_btn 8
void setup()
{
for (int i = 2; i >= 8; i++)
{
pinMode(i, INPUT);
}
Serial.begin(115200);
}
void loop()
{
Serial.print('$');
if (analogRead(joy_x) < 1000) Serial.print("0");
if (analogRead(joy_x) < 100) Serial.print("0");
if (analogRead(joy_x) < 10) Serial.print("0");
Serial.print(analogRead(joy_x));
Serial.print(',');
if (analogRead(joy_y) < 1000) Serial.print("0");
if (analogRead(joy_y) < 100) Serial.print("0");
if (analogRead(joy_y) < 10) Serial.print("0");
Serial.print(analogRead(joy_y));
Serial.print(',');
Serial.print(digitalRead(btn_a));
Serial.print(',');
Serial.print(digitalRead(btn_b));
Serial.print(',');
Serial.print(digitalRead(btn_c));
Serial.print(',');
Serial.print(digitalRead(btn_d));
Serial.print(',');
Serial.print(digitalRead(btn_e));
Serial.print(',');
Serial.print(digitalRead(btn_f));
Serial.print(',');
Serial.print(digitalRead(joy_btn));
Serial.print('#');
delay(50);
}
I plugged Master HC05 into Arduino Leonardo and connected it to the computer.
I put this code into Arduino Leonardo:
#include <Joystick.h>
int pos_x = 0;
int pos_y = 0;
int a_btn = 0;
int b_btn = 0;
int c_btn = 0;
int d_btn = 0;
int e_btn = 0;
int f_btn = 0;
int joy_btn = 0;
void setup()
{
Serial.begin(115200); //USB-CDC
Serial1.begin(115200); //TTL Serial
Joystick.begin();
while(!Serial1){}
}
void loop()
{
readValues();
Joystick.setXAxis(pos_x);
Joystick.setYAxis(pos_y);
Joystick.setButton(0, !a_btn);
Joystick.setButton(1, !b_btn);
Joystick.setButton(2, !c_btn);
Joystick.setButton(3, !d_btn);
Joystick.setButton(4, !e_btn);
Joystick.setButton(5, !f_btn);
Joystick.setButton(6, !joy_btn);
Serial.print(pos_x);
Serial.print(",");
Serial.print(pos_y);
Serial.print(",");
Serial.print(a_btn);
Serial.print(",");
Serial.print(b_btn);
Serial.print(",");
Serial.print(c_btn);
Serial.print(",");
Serial.print(d_btn);
Serial.print(",");
Serial.print(e_btn);
Serial.print(",");
Serial.print(f_btn);
Serial.print(",");
Serial.println(joy_btn);
}
void readValues()
{
String readout = Serial1.readStringUntil('#');
String joy_x = readout.substring(1, 5);
pos_x = joy_x.toInt();
pos_x = map(pos_x, 0, 1023, -127, 127);
String joy_y = readout.substring(6, 10);
pos_y = joy_y.toInt();
pos_y = map(pos_y, 0, 1023, 127, -127);
String btn_a = readout.substring(11, 13);
a_btn = btn_a.toInt();
String btn_b = readout.substring(13, 15);
b_btn = btn_b.toInt();
String btn_c = readout.substring(15, 17);
c_btn = btn_c.toInt();
String btn_d = readout.substring(17, 19);
d_btn = btn_d.toInt();
String btn_e = readout.substring(19, 21);
e_btn = btn_e.toInt();
String btn_f = readout.substring(21, 23);
f_btn = btn_f.toInt();
String btn_joy = readout.substring(23, 25);
joy_btn = btn_joy.toInt();
}
But no matter what I did, I could not transfer any information. When I connect the joystick to the computer via cable, it works and I receive data, so the problem is not there. I checked the HC05s one by one and they both respond to AT commands, so I don't think they are broken either. However, when I connect the HC05 to the Joystick Shield, the LED on it does not blink. I don't know if it needs to flash or not. Do you think the problem is in the codes or does the Joystick Shield not detect the Bluetooth connection? I will be so grateful if you could help me!