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 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
Kindly help me out since it has been days since I have stuck up in this issue.
Thank you!