I use 2 HC-05 Bluetooth Module to communicate 2 arduino nano.
I already set the baud rate for both HC05 is 9600, one as master and the other as slave.
The Master always send the value -1, although the value should be from analog input.
I already checked the analog read, and the value is correct.
But when I check the Bluetooth serial reading, the value is -1.
Anyone can help me?
I really appreciate any kind of help
masterlink.ino (1.19 KB)
slavelink.ino (1.22 KB)
Code:
Master:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
int state = 0;
const int ledPinon = 8; //the pin your led is connected to
const int ledPin = 7; //the pin your led is connected to
int xPin = A1;
int xPosition = 0;
int val = 0;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
pinMode(xPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
//ledblink();
//buttonState = digitalRead(buttonPin);
xPosition = analogRead(xPin);
//Serial.println(dat);
val = map(xPosition, 0, 1023, 0, 180);
if (mySerial.available()){
if (xPosition > 506) {
digitalWrite(ledPin, HIGH);
mySerial.write(val); //sends a 1 through the bluetooth serial link
}
else if ((xPosition <506)||(xPosition >502)){
mySerial.write('0');
digitalWrite(ledPin, LOW);
}
else if (xPosition <502){
mySerial.write(val);
backblink();
}
int check = mySerial.read();
Serial.print(val);
Serial.print(" | ");
Serial.println(check);
delay(200);
}
}
void ledblink(){
digitalWrite(ledPinon, HIGH);
delay(200);
digitalWrite(ledPinon, LOW);
delay(200);
}
void backblink(){
digitalWrite(ledPin, HIGH);
delay(80);
digitalWrite(ledPin, LOW);
delay(80);
}
Slave:
#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial BTserial(2, 3);
int BluetoothData; // the data given from Computer
Servo ESC;
long previousMillis = 0;
long interval = 1000;
int state = 0;
const int ledPinon = 8; //the pin your led is connected to
const int ledPin = 7; //the pin your led is connected to
const int ledPinback = 6; //the pin your led is connected to
void setup() {
// initialize digital pin 8 as an output.
Serial.begin(9600);
BTserial.begin(9600);
pinMode(ledPin, OUTPUT);
ESC.attach(9);
ledblink();
}
void loop() {
Serial.println(BluetoothData);
if (BTserial.available()>0) { // Checks whether data is comming from the serial port
state = BTserial.read(); // Reads the data from the serial port
BluetoothData=state;
ESC.write(BluetoothData);
Serial.println(BluetoothData);
}
// Controlling the LED
if (state > 90) {
digitalWrite(ledPin, HIGH); // LED ON
}
else{
//digitalWrite(ledPin, LOW); // LED ON
backblink(); // LED ON
}
}
void ledblink(){
digitalWrite(ledPinon, HIGH);
delay(200);
digitalWrite(ledPinon, LOW);
delay(200);
}
void backblink(){
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
My question is: Why do you include "if (mySerial.available())" in your master code when your slave code never sends any data? In that case, the rest of your code will never execute, because the if statement will never evaluate as true. My recommendation is that you remove that if statement entirely.
Also, did you correctly pair, bind, and link your HC-05 modules? How have you configured them?