Analog reading via bluetooth

Hello,
I am making a project in which I am trying to communicate the LDR values collected by one arduino(slave) to another arduino(master) via bluetooth. The readings of the LDR is displayed on the serial monitor.
The slave part of the arduino code :

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
#define ldrPin A0
int state = 20;
int ldrValue = 0;
void setup() {
  pinMode(9, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
  digitalWrite(9, HIGH);
  pinMode(ldrPin, INPUT);
  BTSerial.begin(9600);
  Serial.begin(9600);
  
}
void loop() 
{
 ldrValue = analogRead(ldrPin);
 BTSerial.println(ldrValue);
 Serial.println(ldrValue); 
 delay(10);
}

I tried getting the readings of LDR in serial monitor to check whether the readings are correct. The readings shown in Serial monitor were correct.

The code of master part of the project which receives the readings from slave :

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
const byte numChars = 1024;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
    pinMode(9, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
    digitalWrite(9, HIGH);
    BTSerial.begin(9600);
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithEndMarker();
    showNewData();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    
    while (BTSerial.available() > 0 && newData == false) {
        rc = BTSerial.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);
        delay(1000);
        newData = false;
    }
}

But The serial monitor did not show the correct readings of LDR . Instead it showed something like this.

<Arduino is ready>
This just in ... 8
This just in ... 3
This just in ... 4
This just in ... 3
This just in ... 3
This just in ... 3
This just in ... 3
This just in ... 4
This just in ... 3
This just in ... 4
This just in ... 3
This just in ... 3

I have referred this link for the Arduino serial communication:
Serial input basics

For simplicity I have attached the circuit diagram of master and slave in pdf file. Kindly help me out since it has been days since I have stuck up in this issue.

Thank you!

Doc1.pdf (270 KB)

I recommend taking the line:

       delay(1000);

out of your master sketch code. The master should be constantly listening for incoming data on SoftwareSerial. If you want to the data to come at a specific interval then do that in the slave code.

Try running this simplified sketch on the slave:

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX

void setup() {
  pinMode(9, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
  digitalWrite(9, HIGH);
  BTSerial.begin(9600);
  Serial.begin(9600);
}

void loop() {
  BTSerial.println("hello");
  Serial.println("hello");
  delay(1000);
}

Then report back with what is being shown in the serial monitor of the master.

Removed Delay(1000) from master and used your code as slave .The serial monitor of master is showing something like this:

<Arduino is ready>
This just in ... h
This just in ... h
This just in ... h
This just in ... h
This just in ... h
This just in ... h

I think its the problem with the decryption.The master is not able to receive exact data.

Great, that’s much more useful. So we can see it’s only receiving the first character of the “hello” string that was sent by the slave.

I’m not sure exactly what your final goal is for this project. If it’s only to display the reading on the serial monitor you could use this much more simple code on your master:

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
const byte numChars = 1024;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
  pinMode(9, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
  digitalWrite(9, HIGH);
  BTSerial.begin(9600);
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
}

void loop() {
  if (BTSerial.available()) {
    Serial.print(BTSerial.read());
  }
}

If you actually need to store the received value in a variable to do something else with it then it will take more than that but it’s worth a quick test with the code above to see if you start getting the whole string. So please give it a try and report back with your results.

Now it is displaying some random numbers .

serial.println(BTSerial.read()) is showing something like this:

<Arduino is ready>
49
53
49
13
10
49
52
57
13
10
49

Oops, I meant:

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
const byte numChars = 1024;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
 pinMode(9, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
 digitalWrite(9, HIGH);
 BTSerial.begin(9600);
 Serial.begin(9600);
 Serial.println("<Arduino is ready>");
}

void loop() {
 if (BTSerial.available()) {
   Serial.write(BTSerial.read());
 }
}

the other was printing ASCII values of the received characters but unfortunately they don’t match my expectations so give the above code a try with the simplified slave code from my previous reply and we’ll try to figure out what’s going on from there.

Thank you so much!!
It is working. Replaced slave program with my ldr code, it is showing the correct values!!.
I need to store that value in a variable for furthur operation.How can I do that?

Ok, I looked at the master code again and it's fine except for one small bug:

const byte numChars = 1024;

byte can hold a maximum value of 255 so 1024 overflows it multiple times and that statement ends up being equivalent to:

const byte numChars = 0;

so you can see how that would cause some problems. The fix is simple, simply change the line to:

const unsigned int numChars = 1024;

Using the appropriate type means numChars will indeed be 1024. Of course that's a way larger array than will ever be required to send an analogRead() value so setting it to such a large number will just waste memory. Other than that your original master sketch will work fine as is.

Even though the bug was simple, I hope the process I've walked you through can demonstrate an effective troubleshooting technique. When you encounter a problem you don't know how to solve it is always a good idea to write a simplified test sketch as a "sanity check". Once you have that working it at least tells you that some things are working and thus narrows down the cause of the problem. Another effective technique is to just add a bunch of Serial.println() statements throughout your code so you can see exactly where things go wrong.