HC-05 Module Receiving random data when button is pressed

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');
  }
}

My first thought is to look into the differences between .write and .print

just tried that, sadly its still sending the random numbers

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).

it just gets more and more confusing >:(

Please post your latest code and describe in detail the issues you see.

if i change this chunk of the code to what is shown below the slave no longer receives any data

  if (buttonState == LOW)
  {  
    BT.write('1');
    //Serial.print('1');
  }

upon turning it back on it receives the random numbers

Please post complete code.
Did you change the buttons to INPUT_PULLUP with wiring like this
Diagonal Button Wiring

//this is slave random info??? state != '14640' || '18OWF' || '2�G�z' || '1���H' || '2�eSs' || '1O�d' || 'G`475F'
#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)
  {
    digitalWrite(9, HIGH);
    Serial.println("working?");
  }

  else
  {
    digitalWrite(9, LOW);
  }
}

//this is master

#include <SoftwareSerial.h>


int buttonState = 0;
const int buttonPin = 2;

const int rxPin = 4;
const int txPin = 3;

SoftwareSerial BT(rxPin, txPin);

void setup() 
{
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);

  pinMode(buttonPin, INPUT_PULLUP);

  Serial.begin(9600);
  BT.begin(38400);
}

void loop()
{
  
   if  (BT.available() > 0)
   { 
    Serial.println("poo");
   }

  buttonState = digitalRead(buttonPin);

  if (buttonState == LOW)
  {  
    BT.write('1');
    Serial.write('1');
  }

  else if (buttonState == HIGH)
  {
    Serial.write('1');
  }
}

current code

I think the best way for you to proceed is to develop the slave as you require using a Bluetooth Serial Terminal app as the master.
I think the best one to use is Kai Morich's Serial Bluetooth Terminal.
https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal&hl=en_US&gl=US

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);
  }
}

I will also recommend that you review the material to be found in Serial Input Basics Tutorial. If you use the methods presented, your program will be more reliable and robust.
https://forum.arduino.cc/t/serial-input-basics-updated/382007

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.