So here is my problem
I have 2 arduino nanos with HC-05 modules connected when powered the modules pair and whenever i click the button on the master it receives values ... however it sends random numbers (im pretty sure the BAUD rates are correct as there are no unknown / corrupted characters)
have i missed out something?
and how can i fix this T-T
//slave
#include <SoftwareSerial.h>
int state = 0;
const int servoPin = 5;
const int motorPin = 9;
const int rxPin = 3;
const int txPin = 2;
SoftwareSerial BTSerial(rxPin, txPin);
void setup()
{
pinMode(servoPin, OUTPUT);
pinMode(motorPin, OUTPUT);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
digitalWrite(servoPin, LOW);
digitalWrite(motorPin, LOW);
Serial.begin(9600);
BTSerial.begin(38400);
Serial.println("on");
digitalWrite(9, LOW);
}
void loop()
{
Serial.println(state);
if (BTSerial.available() > 0)
{
state = (BTSerial.read());
Serial.println("up");
}
if (state != '1')
{
digitalWrite(9, HIGH);
}
else
{
digitalWrite(9, LOW);
}
}
#include <SoftwareSerial.h>
//master
int buttonState = 0;
const int buttonPin = 2;
const int rxPin = 4;
const int txPin = 3;
SoftwareSerial BTSerial(rxPin, txPin);
void setup()
{
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(buttonPin, INPUT);
BTSerial.begin(38400);
Serial.begin(9600);
}
void loop()
{
if (BTSerial.available() > 0)
{
Serial.println("poo");
}
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
BTSerial.write(1);
Serial.println("1");
}
else if (buttonState == LOW)
{
BTSerial.write('0');
}
}
Take the software serial baud rate to 9600. It does not always work reliably at 38400.
Your button pin may be floating, use INPUT_PULLUP mode, connect one side of the button to ground and the other to the input pin, and look for a LOW when pressed.
BTSerial.write(1);
Do you want to be similar to the .write('0'); BTSerial.write('1');
Whenever I set the software serial baud rate to 9600 on the master, the slave doesnt seem to receive anything.
I also just found out that its sending the Serial.write('1'); not the BTSerial.write('1');
(I found this out from removing the BTSerial.write('1'); command from the if(buttonState == HIGH) loop).
The code is easier when you use ascii characters for any numbers sent or read. Your code appears to depend on a new line character being sent, and reading it and discarding the value.
//if (state == 1)
if(state=='1')
#include <SoftwareSerial.h>
int state = 0;
const int servoPin = 5;
const int motorPin = 9;
const int rxPin = 2;
const int txPin = 3;
SoftwareSerial BTSerial(rxPin, txPin);
void setup()
{
pinMode(servoPin, OUTPUT);
pinMode(motorPin, OUTPUT);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
digitalWrite(servoPin, LOW);
digitalWrite(motorPin, LOW);
Serial.begin(9600);
BTSerial.begin(9600);
Serial.println("on");
digitalWrite(9, LOW);
}
void loop()
{
if (BTSerial.available() > 0)
{
state = (BTSerial.read());
Serial.println(state);
Serial.println(BTSerial.read());
}
//if (state == 1)
if (state == '1')
{
digitalWrite(9, HIGH);
Serial.println("working?");
}
else
{
digitalWrite(9, LOW);
}
}