0

I have two bluetooth modules(HC05) connected to separate arduinos. One acting as master and other as slave. One LDR is connected to the slave part which will be taking continuous readings and sending it to master via bluetooth.

The modules are successfully paired.I could even control an led connected to master using a pushbutton connected to slave.

Since 4 days I am struggling to get the readings of LDR on the serial monitor of master.

The slave part of the project(having the LDR):

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
#define ldrPin A0
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(1000);
 }

The master part of the project which will be getting the reaings and displaying on serial monitor:

#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);
        newData = false;
    }
}

But the problem is that in the serial monitor only the highest digit ( 3 in 392) is displayed in the serial monitor. The readings are correct but the complete readings are not displayed. The serial monitor showed something like this:

<Arduino is ready>
This just in ... 1
This just in ... 1
This just in ... 1
This just in ... 1
This just in ... 1
This just in ... 3
This just in ... 3
This just in ... 3
This just in ... 3
This just in ... 3

Ifin the Slave part instead of LDR readings if I am sending a string "hello", then it is printing as :

<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 have referred this link for serial communications Serial input basics

Can someone please help me out as I am new to arduino.

1 Answers1

0

To read a string directly into a variable you can use:

BTSerial.readString() 

instead of:

BTSerial.read() 

like in the official documentation

Dan Ionescu
  • 3,135
  • 1
  • 12
  • 17