I have paired two Hc05 modules for my project.For testing purpose I have connected initially a pushbutton to slave and led to master and I am able to control the led using pushbutton via Blueotth.
Now I have connected a LDR(Light dependant Resistors) to slave which will be communicating to master connected to 0.96'' OLED . The OLED should be displaying the values of LDR
Slave code:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
#define ldrPin A0
int state = 20;
int ldrValue = 0;
char ldr;
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, OUTPUT);
BTSerial.begin(9600);
}
void loop()
{
ldrValue = analogRead(A0);
BTSerial.write(ldrValue);
delay(10);
}
Master code :
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
int state = 0;
#define OLED_RESET 4 // not used / nicht genutzt bei diesem Display
Adafruit_SSD1306 display(OLED_RESET);
char inChar;
String string;
void setup() {
pinMode(13, OUTPUT);
// initialize with the I2C addr 0x3C / mit I2C-Adresse 0x3c initialisieren
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
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);
display.display();
delay(2000);
display.clearDisplay();
display.setTextColor(INVERSE);
}
void loop()
{
if(BTSerial.available() > 0)
{ // Checks whether data is comming from the serial port
state = BTSerial.read(); // Reads the data from the serial port
}
display.clearDisplay();
display.setCursor(30,0);
display.setTextSize(1);
display.print("LDR Reading:");
display.setCursor(30,10);
display.setTextSize(2);
display.print(state);
delay(10);
display.display();
}
But I am unable to get the complete reading . The OLED shows readings as 0,1,2,3 (single digits fluctuating randomly.
Can someone help me out? Thanks in advance