Good day everyone ^^, we are trying to display the circle on the OLED once the received RSSI is greater than 0 however both the OLED and the serial monitor stops functionig together.. is OLED incompatible working with softwareSerial? I am getting RSSI value from HC-05 bluetooth module. Here is our codes. and thank you in advance for the replies
#include <SoftwareSerial.h>
SoftwareSerial bt(10,11);
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 13
#define OLED_DC 11
#define OLED_CS 12
#define OLED_CLK 10
#define OLED_MOSI 9
Adafruit_SSD1306 display( OLED_MOSI,OLED_CLK,OLED_DC,OLED_RESET,OLED_CS);
//Define for delay
//unsigned long previousMillis = 0;
//const long interval = 100;
const int buttonPin1 = 2;
int buttonState1 = 0;
//Define for Data
char btData;
String splitString(String str, String devider, int arrayNumber);
String deviceAddress;
String deviceClass;
String deviceRSSI;
String message;
bool ATCommandsVisible = true;
bool RSSIVisible = true;
String atQuestEnd = "?\r\n";
String atSetEnd = "\r\n";
String atStart = "AT+";
String Version = "VERSION";
String Address = "ADDR";
String Name = "NAME";
String Role = "ROLE";
String UART = "UART";
//String State = "STATE";
//String Password = "PSWD";
//String Reset = "ORGL";
//String Initialize = "INIT";
//String accesCode = "IAC=9E8B33";
//String deviceType = "CLASS=0";
//String searchRSSI = "INQM=1,2,48";
//RSSI, limit of devices receivable(will find more of the same), max time spend searching *1.28
//String receiveRSSI = "INQ";
//String roleIsMaster = "ROLE=1";
//String roleIsSlave = "ROLE=0";
//String magnitude = " ";
void setup() {
Serial.begin(9600);
bt.begin(38400);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
pinMode(buttonPin1, INPUT);
}
void loop() {
buttonState1=digitalRead(buttonPin1);
//unsigned long currentMillis = millis();
bt.listen();
if (bt.available()) {
btData = bt.read();
message.concat(btData);
if (btData == '\n') {
filterCode();
}
if (ATCommandsVisible) {
Serial.write(btData);
}
}
// if (currentMillis - previousMillis > interval) {
// previousMillis = currentMillis;
//}
int receivedRSSI = pow((-(int(hexToDec(deviceRSSI)) - int(hexToDec("D666")))),0.45);
if (receivedRSSI > 0){
drawcircle();
}
}
void drawcircle(){
display.clearDisplay();
for(int16_t i=max(display.width(),display.height())/2; i>0; i-=3) {
// The INVERSE color is used so circles alternate white/black
display.fillCircle(display.width() / 2, display.height() / 2, i, INVERSE);
display.display(); // Update screen with each newly-drawn circle
//delay(1);
}
//delay(2000);
}
void filterCode() {
if (message.substring(0, 5) == "+INQ:") {
deviceAddress = splitString(message, ",", 0).substring(5);
deviceClass = splitString(message, ",", 1);
deviceRSSI = splitString(message, ",", 2);
if (RSSIVisible) {
Serial.println(sqrt(-(int(hexToDec(deviceRSSI))-int(hexToDec("D666")))));
}
}
message = "";
}
String splitString(String str, String devider, int arrayNumber) {
int previousDevider = 0;
int deviders = 1;
for (int i = 0; i < str.length(); i++) {
if (str.substring(i, i + devider.length()) == devider) {
deviders++;
}
}
int devideCounter = 0;
String devidedString[deviders + 1];
for (int i = 0; i < str.length(); i++) {
if (str.substring(i, i + devider.length()) == devider) {
if (devideCounter) {
devidedString[devideCounter] = str.substring(devidedString[devideCounter - 1].length() + 1, i);
} else {
devidedString[0] = str.substring(0, i);
}
devideCounter++;
previousDevider = i + devider.length();
}
}
devidedString[devideCounter] = str.substring(previousDevider, str.length());
if (arrayNumber > deviders) {
return "ERROR; array number not found. Array number is to big.";
} else {
return devidedString[arrayNumber];
}
}
unsigned int hexToDec(String hexString) { //converts HEX to DEC
unsigned int decValue = 0;
int nextInt;
for (int i = 0; i < hexString.length(); i++) {
nextInt = int(hexString.charAt(i));
if (nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9);
if (nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15);
if (nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15);
nextInt = constrain(nextInt, 0, 15);
decValue = (decValue * 16) + nextInt;
}
return decValue;
}