Problem with OLED Display Voltages (String Command) using transmitter and Receiver

Hi .. I hope that you are doing well ..

I want to display battery voltages on OLED using a string command but don't know where I'm lacking ...

I'm using two Arduino one is the transmits and another is receiving a signal and displays it on OLED

Here is the circuit diagram for better understanding

here is the code of the receiver to show display on OLED

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32


Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

String inputString = "";         // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete

void setup() {
  Serial.begin(4800);
  inputString.reserve(200);
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  display.clearDisplay();
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.display();
}
int i = 0;
void loop() {
  if (i>=0) i++;
  if (stringComplete) {

    String strID = (inputString.substring(0, 4));
    if (strID == "$ABC") {
      Serial.print(inputString);
      for (byte i = 0; i <= 35; i++) {
        char c = inputString[i];
        if (c == 'V') {
          int index = inputString.indexOf("V");
          String Volts = inputString.substring(index - 6, index + 1);
          Serial.println(Volts);

          display.clearDisplay();
          display.drawRect(0, 0, 128, 64, SSD1306_WHITE);
          display.setTextSize(1);
          display.setCursor(25, 5);
          display.println ("ABC System");
          display.setCursor(25, 15);
          display.println ("Started");
          display.setTextSize(2);
          display.setCursor(24, 35);
          display.println(Volts);
          display.display();
        }
      }
      for ( ; ; ) {}
    }
    inputString = "";
    stringComplete = false;
  }
if (i>123) i = 0;
}
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

Here is the code of the transmitter that transmits
I want to implement the battery voltages with string to catch it but dont know where to start it at

//BATTERY READINGS VARIABLES
const int Battery = A0;
float Voltage = 0.0; //input battery voltage
float Voltage1 = 0.0; //output voltage for analog pin A0
float R1 = 1590; // will use R1 =47k ohms and 27k ohms resistors
float R2 = 1000; // will use R2 =10k ohms resistor 
int readValue = 0;
long previousMillis_battery = 0;
byte Bat_start_timer = 0;
int battery_wait;
unsigned long currentMillis = 0; // VALID FOR ALL FUNCTIONS(COUNTER)

//BATTERY FUNCTION
void battery()
{
 readValue = analogRead(Battery);
 Voltage1 = readValue * (5.0 / 1023); 
 Voltage = Voltage1 / (R2/(R1+R2));
 if(Bat_start_timer == 0) battery_wait = 2000;
 if(currentMillis - previousMillis_battery > battery_wait) {
//    Serial.println("Battery Voltages,"); Serial.print(Voltage); Serial.print(",V,");
  previousMillis_battery = currentMillis;
    battery_wait = 2000; Bat_start_timer = 0; }

} // battery FUNCTION END

void setup()
{
  Serial.begin(4800);
  pinMode(Battery, INPUT);
  Serial.println("Battery Voltages");
  
}

void loop()
{
   int i = 0;
  if (i >= 0) i++;{
  Serial.println ("$ABC, 12.10V,\n"); 
//  Serial.print(Voltage);
}
i = 0;
delay (100);


//    currentMillis = millis();
//    battery();
//    delay(500);
}

Thank you so much.

For development I would use software serial ports on the Unos to communicate between them. That way you have the hardware serial ports (USB) for debugging.

The serial input basics tutorial may help you with the serial communications. The tutorial uses strings (null terminated character arrays) instead of the potentially problematic String class.

1 Like

did you understand my code ?

We can read your code and predict how it will behave. I would not say we understand it, because quite a few parts make no sense.

If you want us to help, you must describe the problem that you face more precicely. The above comments are much too vague. Tell us what happens when the code runs and what you expected or wanted to happen.

1 Like

Couple of points:

This code

... is equivalent to


  Serial.println ("$ABC, 12.10V,\n"); 

This is not a good idea...

int i = 0;
  for (byte i = 0; i <= 35; i++) 
Don't use the same variable name...

The problem I'm facing right now is that I could not display the Battery Voltages on my OLED. I'm trying to make changes in my code but I couldn't ...
Actually I 'm working on to show the battery voltages wirelessly from the transmitter to the receiver. But first I want to show it using wire as in the circuit diagram.

I'm stuck on my coding and don't know how to display voltages using a string and substring. If this will happen then it would be more precise and easy for me to show the voltages wirelessly as well.

This code makes no sense to me, for example

      for (byte i = 0; i <= 35; i++) {
        char c = inputString[i];
        if (c == 'V') {
          int index = inputString.indexOf("V");

The for-loop and if statement seem to be searching through the string for a "V" character, one position at a time. But then you use .indexOf(), which will find the "V" in a single step, so why bother with the for-loop? It is repeating code 35 times over when it needs to run only once.

on OLED, it displays this "$ABC, 12.10V,". I want to make some changes in the code to display the actual battery voltages in my OLED.

Did you understand my previous post?

1 Like

yes makes no sense, i checked the code without it still it displays "$ABC, 12.10V," on OLED

The point was that it is a bad idea to redefine a variable - "i" in this case.

Um... it makes perfect sense. I'm not sure what you are expecting?

I think the problem you should focus on it including a float value in the middle of a string.

is it possible that you will make changes in the code so that it will show the battery voltages ?

void loop()
{
  char buff[6];
  battery();
  dtostrf(Voltage, 5, 2, buff)
  Serial.print ("$ABC, ");
  Serial.print(buff);
  Serial.println("V,\n"); 
}
1 Like

can you please make changes according to your own in my code ?

Thank you ...

Thank you, its working; the voltags shows on my OLED but I m facing one problem ... I am not updated with my battery voltages everytime i need to shutdown an arduino and then power it again to show the voltages ...

Is there any way ?

If you connect the "transmitting" Arduino to serial monitor, can you see the voltage changing/updating?

Perhaps the transmitter Arduino is sending the data too fast/frequently and the receiving Arduino's serial buffer is overflowing and corrupting the messages. Try adding delay(1000); into loop() for the transmitter.

Could you make the buffer larger, for example 20 or 40 bytes.
The width of 5 for dtostrf() is a minimal width.
See here: https://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html
and search for the word "minimum".

Yes I see the voltages updating but on OLED, its not changed.

Here is the output of both Arduinos transmit and receive

Yes I updated it from 6 to 20 and 40 as well but I could get get the result.

that 11.84 prints on OLED and not updated