I am trying to display the readings of LDR connected to slave arduino through serial monitor of master arduino paired with bluetooth. I got the readings and it is working fine.
But when I connected OLED to the master arduino so that the LDR readings can be displayed on OLED as well as serial monitor, it is not displaying as expected.
So for simplicity I tried sending the word "hello" from slave to master. The code for slave is :
#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 I tried displaying the same only on serial monitor of master. The code:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
char state ;
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()) {
state = BTSerial.read();
Serial.write(state);
}
}
It is showing valid readings in serial monitor with hello being printed after every one second
<Arduino is ready>
hello
hello
hello
hello
But when I modified the code so that I can get the readings on OLED as well as serial monitor;
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
char sensor;
void setup()
{
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);// initialize with the I2C addr 0x3C (for the 128x32)(initializing the display)
Serial.begin(9600);
BTSerial.begin(9600);
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
}
void displayreading()
{
if (BTSerial.available()) //checks for incoming data
{
sensor = BTSerial.read();//reads data from bluetooth serial
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(30,0);
display.setTextSize(1);
display.print("LDR Reading:");
display.setCursor(30,10);
display.setTextSize(2);
display.print(sensor);
Serial.write(sensor);
}
}
void loop()
{
displayreading();
display.display();
}
The OLED is showing one letter at a time clearing the screen after every letter rapidly (since I did not add delay which will add up to the problem of data lag). Like : h , (clear screen) e (clear screen) l (clear screen) l (clear screen) 0 (clear screen)
The serial monitor is showing the readings as:
<Arduino is ready>
hello
hello
hello
hello
But the word hello is printed letter after letter and not in a whole like in the previous master code.There is one second gap after every printing of "hello".
Can somebody please help me with this problem? Thanks in advance
BTSerial.read()
reads a single byte.