Working code but it's slow

Hello.
I have a ST7920 lcd connected to an arduino uno, the code reads 1 temps sensor and displays the data in 8 rows. Later i will connect 6 more sensors. I used u8g2 lib.The code is working but the refresh rate of the readings is rather slow, every line update is about half a second (the first line takes 0,5 seconds, the second 1 second and the last one updates every four seconds… Probably is the code is written but thats what i found.
Below is the code and the wiring.

#include <Arduino.h>
#include <U8g2lib.h>
#include <DallasTemperature.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif


U8G2_ST7920_128X64_1_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* CS=*/ 10, /* reset=*/ 8);
#define ONE_WIRE_BUS 7

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float Celsius = 0;

void setup(void) {
  u8g2.begin();
  u8g2.clearDisplay();
  u8g2.setFont(u8g2_font_baby_tr);
  sensors.begin();
  }

void loop(void) {
  u8g2.firstPage();
 do {
  LcdScreen();
  poop1();
  } while ( u8g2.nextPage() );
}

void LcdScreen()
{
  u8g2.setCursor(0, 7);
  u8g2.print(F("K/M OIL TEMP LOW!"));
  u8g2.setCursor(0, 15);
  u8g2.print(F("K/M OIL TEMP HIGH"));
  u8g2.setCursor(0, 23);
  u8g2.print(F("K/M WATER TEMP IN"));
  u8g2.setCursor(0, 31);
  u8g2.print(F("K/M WATER TEMP OUT"));
  u8g2.setCursor(0, 39);
  u8g2.print(F("K/M OIL PRESS"));
  u8g2.setCursor(0, 47);
  u8g2.print(F("MEIVT OIL TEMP"));
  u8g2.setCursor(0, 55);
  u8g2.print(F("MEIVT OIL PRESS"));
  u8g2.setCursor(0, 63);
  u8g2.print(F("S/W PRESS!"));
  
  
  u8g2.setCursor(100, 7);               
  u8g2.print(Celsius+1);                       // Display data, Sensor + 1
  u8g2.setCursor(100, 15);
  u8g2.print(Celsius+2);
  u8g2.setCursor(100, 23);
  u8g2.print(Celsius+3);
  u8g2.setCursor(100, 31);
  u8g2.print(Celsius+4);
  u8g2.setCursor(100, 39);
  u8g2.print(Celsius+5);
  u8g2.setCursor(100, 47);
  u8g2.print(Celsius+6);
  u8g2.setCursor(100, 55);
  u8g2.print(Celsius+7);
  u8g2.setCursor(100, 63);
  u8g2.print(Celsius);                      // display sensor data
}
void poop1(void)
{
  sensors.requestTemperatures();
  Celsius = sensors.getTempCByIndex(0);    // Read temp data
  
}

sensors.requestTemperatures(); That’s 3/4 of a second at maximum resolution, IIRC.

3/4 of a second is fast enough for readingr but I'm referring to the display time. The temp reading appears to the lcd every 4 seconds ( last line).

You are calling the LcdScreen() function in every iteration of loop() - that's bound to slow things down. It really only needs to be updated 2 or 3 times per second.

Also you are updating all parts of the LCD in on call to the function. Split that up so (say) 2 lines are updated on each successive iteration of loop(). The difference should not be noticeable to the human eye.

...R

I will try it tomorrow Robin, thanks

I split the LcdScreen() in 4 parts, each part is reading 2 sensors and displaying their data. Each sensor updates it's data in the screen every 8 seconds, one by one. So nothing happened, it's still slow. I tried to update only the reading values in the screen and keep the rest unchanged but no luck.

What do firstPage and nextPage do for you?

I don't have a firstpage() or nextpage() subs. I found the code from u8g2 examples.

I'd be inclined to try it without them, or at least to drop nextPage and move the firstPage call into setup. They're the only bits of your code I can think of that could be responsible for the slowness and since you're not using multiple pages as far as I can see, you may not need them.

first/next is how that library handles that display. It just is. I haven't tried doing without them even if I am sure the paging is not needed. YMMV as usual.

You are slowing things down by reading the temperature sensor within first/next block, and maybe more often than strictly necessary at that. I don't know how your temperature library works, but it takes a long time to get the temp from that sensor, there are ways to split the request to take a new reading from getting the new reading whilst you do something else, possibly updating the display with the previous requested reading.

HTH

a7

In my code above I have LcdScreen displaying 8 labels and temp readings. I split the screen in 2 parts, screen1 (labels) and screen2 (readings). Normally labels should be displayed first and readings after. Strangely it displays 1 line at a time, label-reading then label-reading and so on. It takes 16 seconds to display everything. I moved firstpage to setup. As a result, only 1 line is printed. I removed do-while and first-next pages and then I have only readings.. So they are needed.

use millis() to see how where the bottleneck is.

prev=millis(); call function Serial.println(millis()-prev);

alto777: I don't know how your temperature library works, but it takes a long time to get the temp from that sensor, there are ways to split the request to take a new reading from getting the new reading whilst you do something else, possibly updating the display with the previous requested reading.

This concept is key to getting reasonable performance using DS18B20-type sensors and is supported by the DallasTemperature library by using the proper API calls. Using the simple-minded, blocking, version of 'requestTemperatures()' followed by 'getTempCByIndex()' is the easiest technique for newbie programmers to understand. But, it is the worst choice for overall system performance.

Is there another way for reading temperature besides the one you mentioned? Is it faster?

I made a very simple project. Reading 8 sensors and displaying them in Serial. Print() to pc. It took almost 5 seconds to display all 8 readings in 1 line.

koskap: Is there another way for reading temperature besides the one you mentioned? Is it faster?

Yes, if I say some method gives the worst performance, then, obviously, there's a better way. That's what I was referring to. Use non-blocking techniques to start the conversion, do useful work while the conversion is taking place, and use device addresses (rather than 'ByIndex") to retrieve the temperature readings. See My Reply #13 in this Thread.

koskap: I split the LcdScreen() in 4 parts, each part is reading 2 sensors and displaying their data.

You need to post the updated program for that to have any meaning for us. The devil is in the detail.

...R

I changed the resolution to 9 and that speeded up things. Now readings are fast, almost 6 seconds for a reading and displaying 8 sensors.

One more question about wiring. The lcd has 3.3v for backlight. I have seen in few diagrams that 5v can be connected. Is it possible?

Are you still busy-waiting for readings?

I'm interested in any other way.