Using the u8g2 library with 1.3 inch OLED

Hi Everyone,

I've been trying to use the u8g2 library to print sensor data from a moisture sensor and four LDRs onto an OLED with an SH1106 driver. The sensor data is being sent from an Arduino Uno to an Arduino Mega using a transmitter and a receiver. The data is being transmitted and received correctly, I've tested this with the serial monitor and data is being printed on the display. However, the values on the screen aren't changing as they should.
Any suggestions to fix this would be greatly appreciated?

Transmitter code:

#include <SPI.h> //SPI library
#include <RH_ASK.h> //RadioHead Amplitude Shift Keying Library

//Pin connections
#define MoistSensor 0
#define LDR3 2
#define LDR4 3
#define LDR5 4
#define LDR6 5

int Moisture; //Stores moisture value
//Stores LDR values
int Light3;
int Light4;
int Light5;
int Light6;

//Define strings
String str_Moisture;
String str_Light3;
String str_Light4;
String str_Light5;
String str_Light6;
String str_out;

RH_ASK driver; //Creates the RH_ASK object called driver


void setup() {
  pinMode(MoistSensor, OUTPUT);
  pinMode(LDR3, INPUT);
  pinMode(LDR4, INPUT);
  pinMode(LDR5, INPUT);
  pinMode(LDR6, INPUT);
  Serial.begin(9600);
  if (!driver.init()) //Initilizes the RH_ASK object
    Serial.println("INIT FAILED");

}

void loop() {
  ////////////////////////////////////////////
  int VAL;
  VAL = analogRead(MoistSensor);
  Moisture = map(VAL, 607, 257, 0, 100);
  /////////////////////////////////////////////
  int LIGHT3;
  LIGHT3 = analogRead(LDR3);
  Light3 = map(LIGHT3, 342, 1022, 0, 100);
  //////////////////////////////////////////
  int LIGHT4;
  LIGHT4 = analogRead(LDR4);
  Light4 = map(LIGHT4, 342, 1022, 0, 100);
  /////////////////////////////////////////
  int LIGHT5;
  LIGHT5 = analogRead(LDR5);
  Light5 = map(LIGHT5, 342, 1022, 0, 100);
  /////////////////////////////////////////
  int LIGHT6;
  LIGHT6 = analogRead(LDR6);
  Light6 = map(LIGHT6, 342, 1022, 0, 100);

  str_Moisture = String(Moisture);
  str_Light3 = String(Light3);
  str_Light4 = String(Light4);
  str_Light5 = String(Light5);
  str_Light6 = String(Light6);

  str_out = str_Moisture + "," + str_Light3 + "," + str_Light4 + "," + str_Light5 + "," + str_Light6;

  static char *msg = str_out.c_str();

  driver.send((uint8_t*)msg, strlen(msg));
  driver.waitPacketSent();


}

Receiver & OLED code:

#include <SPI.h> //SPI library
#include <RH_ASK.h> //RadioHead Amplitude Shift Keying Library
#include <Wire.h>
#include <U8g2lib.h>
#define time_delay 2000
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);

//Define same strings used in transmitter, one for each variable & one for comma limited string we will be receiving
String str_Moisture;
String str_Light3;
String str_Light4;
String str_Light5;
String str_Light6;
String str_out;

RH_ASK driver; //Creates the RH_ASK object called driver

void u8g2_prepare() {
  u8g2.setFont(u8g2_font_6x10_tf);
  u8g2.setFontRefHeightExtendedText();
  u8g2.setDrawColor(1);
  u8g2.setFontPosTop();
  u8g2.setFontDirection(0);
}

void Print_Sensors() {
  u8g2.clearBuffer();
  u8g2_prepare();
  u8g2.setCursor(0, 0);
  u8g2.print("Soil Moisture: ");
  u8g2.setCursor(30, 10);
  u8g2.print(str_Moisture);
  u8g2.print("%");
  u8g2.setCursor(0, 30);
  u8g2.print("Light (1,2,3,4):");
  u8g2.setCursor(10, 40);
  u8g2.print(str_Light3);
  u8g2.print("%");
  u8g2.sendBuffer();
  delay(time_delay);
}


void setup() {

  Serial.begin(9600); //Setup serial monitor parameters
  if (!driver.init()) //Initilizes the RH_ASK object
    Serial.println("INIT FAILED");
  u8g2.begin();
  u8g2_prepare();

}

void loop() {

  //Set buffer to size of expected message
  uint8_t buf[41];
  uint8_t buflen = sizeof(buf);

  if (driver.recv(buf, &buflen)) //If valid packet is received then...
  {
    str_out = String((char*)buf);// Convert received data into string (will be our comma delimited text)

    for (int i = 0; i < str_out.length(); i++) { //Measures the string length & the comman position is where the string is devided into two substrings
      if (str_out.substring(i, i + 1) == ",") {
        str_Moisture = str_out.substring(0, i);
        str_Light3 = str_out.substring(i + 1);
        str_Light4 = str_out.substring(i + 1);
        str_Light5 = str_out.substring(i + 1);
        str_Light6 = str_out.substring(i + 1);
        break;

      }
    }

    //Print_Sensors();
  
    u8g2.clearBuffer();
    u8g2_prepare();
    u8g2.setCursor(0, 0);
    u8g2.print("Soil Moisture: ");
    u8g2.setCursor(30, 10);
    u8g2.print(str_Moisture);
    u8g2.print("%");
    u8g2.setCursor(0, 30);
    u8g2.print("Light (1,2,3,4):");
    u8g2.setCursor(10, 40);
    u8g2.print(str_Light3);
    u8g2.print("%");
    u8g2.sendBuffer();
    delay(200);



  }
}

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