Bluetooth Monitor & OLED Display not updating

Hi All,
I am currently working on a sensor device, using an Uno R4 WIFI, where the readings from 2 Sharp IR Sensors are read, and then displayed on both an OLED Display, which is updated every 2 seconds, as well as a tablet via Bluetooth, updated every Minute. Unfortunately it is not working, the OLED display remains blank, and the Bluetooth doesn't seem to update/display the assigned values. I based the code on the included "Battery Monitor" example, which I modified. For reference, I am using the Nordic nRF Connect & nRF Logger apps, on the tablet. apologizes if the code is a bit messy.
My Questions are:

  1. Why is the OLED Display, not displaying/updating the output?
    2)Why does the tablet not update/display the assigned values?
  • 2a) Do I need to remedy this on the tablet, if so how?
/*
  Code is modified from Battery Monitor Example found in BLE library
*/
//Libraries used by Board.
#include <SharpIR.h>
#include <ArduinoBLE.h>

//included libraries OLED Display
#include "SPI.h" //Includes library for SPI communication of display
#include "Adafruit_GFX.h" //Includes core graphics library
#include "Adafruit_SSD1351.h" //Includes hardware specific library

//OLED screen dimensions
#define SCREEN_WIDTH 128 //pixel width
#define SCREEN_HEIGHT 128 //pixel height

//OLED pin definitions
#define SCLK_PIN 13 //defines s clock pin
#define MOSI_PIN 11 //defines master-out slave-in SDA pin
#define RST_PIN   3 //defines reset pin
#define DC_PIN    5 //defines master-in slave-out pin
#define CS_PIN    4 //defines chip select pin

// Colour definitions
#define BLACK           0x0000
#define BLUE            0x001F
#define RED             0xF800
#define GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0  
#define WHITE           0xFFFF
#define GREY            0x8410
#define ORANGE          0xE880

//OLED Display Parameter definitions
Adafruit_SSD1351 display = Adafruit_SSD1351(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI, CS_PIN, DC_PIN, RST_PIN); 

//Sharp IR Sensors with pinout Addresses
SharpIR sensorLeft( SharpIR::GP2Y0A02YK0F, A0 );
SharpIR sensorRight( SharpIR::GP2Y0A02YK0F, A1 );

 // Bluetooth® Low Energy Battery Service
BLEService cc5IRSensorService("21027c5d-e0af-4097-bb81-38c2d9abce12");

// Bluetooth® Low Energy Battery Level Characteristic - MODIFY TO IR SENSORS
BLEUnsignedCharCharacteristic southIRSensorChar("47054fb1-cd0e-46df-9695-c9f804be82a4",  // standard 16-bit characteristic UUID
    BLERead | BLENotify | BLEBroadcast | BLEWrite); // remote clients will be able to get notifications if this characteristic changes
BLEUnsignedCharCharacteristic northIRSensorChar("a7264e56-29f0-4972-9f0f-24f1bc218ee5",  // standard 16-bit characteristic UUID
    BLERead | BLENotify | BLEBroadcast | BLEWrite); // remote clients will be able to get notifications if this characteristic changes

//Set Global Variables
int Avgleftdeflect = 0; //Avg of 20 IRSensor readings across 200 millis
int Avgrightdeflect = 0;
int southEdge = 0;  // last deflection reading from South IR Sensor.
int northEdge = 0;  // last deflection reading from North IR Sensor.
unsigned long previousMillis = 0;  // last time the deflection was checked, in ms.
unsigned long blueLEDelay = 60000; //delay between updates to bluetooth.
unsigned long displayPrevious = 0; //time since last output to OLED Display.
unsigned long oledDelay = 2000; //OLED Display update delay

//String outputs for OLED Display
String Ldis;
String rightDistance;

void setup() {
  display.begin();        //OLED Display Set Up
  display.fillScreen(BLACK);
  display.setRotation(2);

  Serial.begin(9600);    // initialize serial communication
  while (!Serial);

  pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1);
  }

  /* Set a local name for the Bluetooth® Low Energy device - MODIFY TO SEND/RECIEVE IR SENSOR DATA
     Create and Assign advertised Service
     Assign sensor characteristics to service
  */
  BLE.setLocalName("CC5 Slack Edge Sensor");
  BLE.setAdvertisedService(cc5IRSensorService); // add the service UUID
  cc5IRSensorService.addCharacteristic(southIRSensorChar); // add the South IR Sensor characteristic
  cc5IRSensorService.addCharacteristic(northIRSensorChar); // add the North IR Sensor characteristic
  BLE.addService(cc5IRSensorService); // Add the IR Sensor service
  southIRSensorChar.writeValue(southEdge); // set initial value for southIRSensor characteristic
  northIRSensorChar.writeValue(northEdge); // set initial value for northIRSensor characteristic

   // start advertising
  BLE.advertise();

  Serial.println("Bluetooth® device active, waiting for connections...");
}

void loop() {
  int dValsouth;
  int dValnorth;

  for (int i = 0; i <= 20; i++) {
      int lftdft = 30 - sensorLeft.getDistance();
      int rghtdft = 30 - sensorRight.getDistance();
      dValsouth = dValsouth + lftdft;
      dValnorth = dValnorth + rghtdft; 
    }
  //Average of readings 
  dValsouth = dValsouth / 20;
  dValnorth = dValnorth / 20;
  //Assign to Global Variable
  Avgleftdeflect = dValsouth;
  Avgleftdeflect = dValnorth;


  // wait for a Bluetooth® Low Energy central
  BLEDevice central = BLE.central();

  // if a central is connected to the peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's BT address:
    Serial.println(central.address());
    // turn on the LED to indicate the connection:
    digitalWrite(LED_BUILTIN, HIGH);

    // check the battery level every Minute = 60,000 milliseconds
    // while the central is connected:
    while (central.connected()) {
      long currentMillis = millis();
      // Update OLED Display evey 2 seconds (2000 milliseconds).
      if (currentMillis - displayPrevious >= oledDelay) {
        displayPrevious += oledDelay;
        oledUpdate();
      }
      //Update Tablet Data with Sensor readings every Minute
      if (currentMillis - previousMillis >= blueLEDelay) {
        previousMillis += blueLEDelay;
        updateSensorData();
      }
    }
    // when the central disconnects, turn off the LED:
    digitalWrite(LED_BUILTIN, LOW);
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}  

void updateSensorData() {

  //Assign data call to outputs map to range
  int sEdge  = Avgleftdeflect;
  int nEdge = Avgrightdeflect;
  int southLevel = sEdge;
  int northLevel = nEdge;

  if (southLevel != southEdge || northLevel != northEdge) {      // if the Edge deflection has changed
    Serial.print("South Edge Deflection is now: "); // print to Serial Monitor
    Serial.println(southLevel);
    Serial.print("North Edge Deflection is now: "); // print to Serial Monitor
    Serial.println(northLevel);
    southIRSensorChar.writeValue(southLevel);  // and update IR Sensor characteristics
    northIRSensorChar.writeValue(northLevel);
    southIRSensorChar.broadcast();    //Broadcast updated data values to tablet
    northIRSensorChar.broadcast();
    southEdge = southLevel;           // save the delfection Values for next comparison
    northEdge = northLevel;
  }
}

void oledUpdate() {
    //Convert Data to Text
    Ldis = String(Avgleftdeflect);
    rightDistance = String(Avgrightdeflect);
  
    //Ouput to OLED Display
    display.fillScreen(BLACK); //Clear Screen operation
    display.setTextColor(WHITE, BLACK); //Set Screen Colour Scheme
    display.setTextSize(2); //Set the Text Size
    display.setCursor(0, 0); //Cursor Starting Position Line One.
    display.print("South Edge");//Display Film left Edge Slack.
    display.setCursor(0, 25);
    display.print(Ldis);
    display.setCursor(0, 40);
    display.print("cm +/- 1cm");
    display.drawLine(0, 55, 128, 55, WHITE); //Draw Screen Devider line.
    display.setCursor(0, 60);
    display.print("North Edge"); //Display Film Right Edge Slack
    display.setCursor(0, 80);
    display.print(rightDistance);
    display.setCursor(0, 95);
    display.print("cm +/- 1cm");
}

You need display.display() to show the data. The other display.xyz() commands are loading the display buffer.

I thought that at first glance too, but the SSD1351 library is different than the SSD1306. There doesn't appear to be a display buffer in RAM, every command goes right to the display

@xfpd, I have run just the OLED display section by itself, and it works, the display shows the sensor values, and updates every 2 seconds, but when I include the bluetooth code, it stops working. not sure why.

Show the IDE output from compiling the full sketch. Perhaps a memory shortage.

I did not know this. Thank you.

I don't think it's a memory issue, the program memory and dynamic memory don't exceed 40% of available, see snip below.

Does the bluetooth code run on the Uno R4 as expected?
Perhaps the libraries for OLED and BT have conflicts in pins or timers? I do not know.

It seems to run, fine, I can connect to the board from the tablet, an the UUID's are all correct for the service an characteristics, as for the pin issue, the R4 has integrated Bluetooth, so I don't think its a pin conflict.

Ah, right. I know nothing about R4 (and forgot to look at the devices being used). Looking inside the Adafruit library, your sketch looks configured correctly. Obviously I have missed something concerning compatibility. I wish I could help. Sorry.

So I have Solved the OLED issue, turns out it was a pin conflict, just not with the Bluetooth, turns out the inbuilt LED uses the same pin as the SCLK pin on the OLED, I've removed the inbuilt LED sequence as it's not really necessary.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.