U8g2 Display doesnt Work as it should

Hi, im new and I have a project in which I want to build a sonar, but I have a problem, the output values on the display, which are supposed to show the distance, change much too slowly and lag, but I don't understand why that is please help me im very frustrated.
Sry for my bad english :slight_smile:

#include "U8g2lib.h"
#define Echopin 13
#define Trigpin 12
#define SERVOPIN 10
#include "Servo.h"
#include "HCSR04.h"
#define Buzzer 9

// 1,3 Zoll SH1106
 U8G2_SH1106_128X64_NONAME_F_HW_I2C oled(U8G2_R0);
UltraSonicDistanceSensor Abstandssensor (Trigpin,Echopin);


Servo Servomotor;
byte Servowinkel = 0;
int Serv = 0;
unsigned long previousMillis = 0;//für den Timer
const long intervall = 30;//für den Timer

void setup() 
{
  oled.setBusClock(100000);
  oled.begin();
  oled.clearBuffer();
  Serial.begin(9600);
  Servomotor.attach(SERVOPIN);
  pinMode(Buzzer,OUTPUT);
  digitalWrite(Buzzer,0);
  Servomotor.write (0);
}

void loop() 
{
 
 
  //Zeichnen der Benutzeroberfläche OLED:

  oled.setDrawColor(1);// Farbe weiß
  
   
  oled.firstPage();
  do 
  {
  
   oled.drawRFrame(0, 0, 128, 64, 3); //Rahmen
   oled.setFont(u8g2_font_helvB12_tf); //12 px
   oled.drawStr(2,15,"Sonar:");
   oled.drawLine(3,18,51,18); //Unterschrift von "Sonar"

   //Radar:
   oled.drawCircle(65,62, 34, U8G2_DRAW_UPPER_RIGHT);//Großer Kreis Rechts
   oled.drawCircle(63,62,34,U8G2_DRAW_UPPER_LEFT); //Großer Kreis Links

   oled.drawCircle(65,62, 24, U8G2_DRAW_UPPER_RIGHT); //Mittlerer Kreis Rechts
   oled.drawCircle(63,62,24,U8G2_DRAW_UPPER_LEFT);//Mittlerer Kreis Links

    oled.drawCircle(65,62, 14, U8G2_DRAW_UPPER_RIGHT);//Kleiner Kreis Rechts
   oled.drawCircle(63,62,14,U8G2_DRAW_UPPER_LEFT);//Kleiner Kreis Rechts

   oled.drawLine(64,62,64,25);//Mittlere Linie
   oled.drawLine(64,62,36,36);//Linke Linie
   oled.drawLine(64,62,92,36);//Rechte Linie

   oled.drawFrame(79,3,46,15);//Rechteck cm anzeige
   
   //damit cm immer direkt eine Freizeile hinter der entfernung steht 
  unsigned long currentMillis = millis();
  if((currentMillis-previousMillis)>= intervall){//delay
  previousMillis = currentMillis;
   int Abstand = Abstandssensor.measureDistanceCm();
   oled.setFont(u8g2_font_ncenB08_tr);
   if(Abstand<=99){
   if(Abstand<10){
   oled.drawStr(88,14,"cm");
   }
   else{
   oled.drawStr(94,14,"cm");
   }
   }
   else{
   oled.drawStr(82,14,"no");
   }
  }
  // x Koordinaten Radar OLED Display Start: 31 Ende: 99
  // y Koordinaten Radar OLED Display Start: 25 Ende: 62

  for(int Serv= 0; Serv<= 165;Serv++) // Servomotor geht nur dreht nur bis 165 alles danach ist quasi wie delay
{
Servomotor.write(Serv);
delay(10);
unsigned long currentMillis = millis();
if((currentMillis-previousMillis)>= intervall){//delay
previousMillis = currentMillis;
int Abstand = Abstandssensor.measureDistanceCm();
if(Abstand<30){
oled.setCursor(82,14);
oled.setFont(u8g2_font_ncenB08_tr);
oled.print(Abstand);
Serial.print(Abstand);
Serial.println();
}
}
}


for(int Serv=165; Serv>0;Serv--) //for schleife wird solange ausgeführt bis sie false ist dann geht es erst weiter
{
Servomotor.write(Serv);
delay(10);
unsigned long currentMillis = millis();
if((currentMillis-previousMillis)>= intervall){//delay
previousMillis = currentMillis;
int Abstand = Abstandssensor.measureDistanceCm();
if(Abstand<30){
oled.setCursor(82,14);
oled.setFont(u8g2_font_ncenB08_tr);
oled.print(Abstand);
Serial.print(Abstand);
Serial.println();
}
}
}


   
    
  


  
  }
  while (oled.nextPage());
  
}

Have you tried 400000 ?

Please Auto-Format your code and re-post it. It is difficult to follow with such poor indentation.

The OLED will not be updated until this line is executed.

Before that line can be executed, your code executes 2x for-loops that repeat 165 times and delay 10ms in each loop.

So your display will only update once every 3~3.5 seconds.

Yes it changes nothing

#include "U8g2lib.h"
#include "Servo.h"
#include "HCSR04.h"

#define Echopin 13
#define Trigpin 12
#define SERVOPIN 10

// 1,3 Zoll SH1106
U8G2_SH1106_128X64_NONAME_1_HW_I2C oled(U8G2_R0);
UltraSonicDistanceSensor Abstandssensor(Trigpin, Echopin);


Servo Servomotor;
int Serv = 0;

void setup() {
  oled.setBusClock(400000);
  oled.begin();
  oled.clearBuffer();
  Serial.begin(9600);
  Servomotor.attach(SERVOPIN);
  Servomotor.write(0);
}

void loop() {


  //Zeichnen der Benutzeroberfläche OLED:

  oled.setDrawColor(1);  // Farbe weiß


  oled.firstPage();
  do {

    oled.drawRFrame(0, 0, 128, 64, 3);   //Rahmen
    oled.setFont(u8g2_font_helvB12_tf);  //12 px
    oled.drawStr(2, 15, "Sonar:");
    oled.drawLine(3, 18, 51, 18);  //Unterschrift von "Sonar"

    //Radar:
    oled.drawCircle(65, 62, 34, U8G2_DRAW_UPPER_RIGHT);  //Großer Kreis Rechts
    oled.drawCircle(63, 62, 34, U8G2_DRAW_UPPER_LEFT);   //Großer Kreis Links

    oled.drawCircle(65, 62, 24, U8G2_DRAW_UPPER_RIGHT);  //Mittlerer Kreis Rechts
    oled.drawCircle(63, 62, 24, U8G2_DRAW_UPPER_LEFT);   //Mittlerer Kreis Links

    oled.drawCircle(65, 62, 14, U8G2_DRAW_UPPER_RIGHT);  //Kleiner Kreis Rechts
    oled.drawCircle(63, 62, 14, U8G2_DRAW_UPPER_LEFT);   //Kleiner Kreis Rechts

    oled.drawLine(64, 62, 64, 25);  //Mittlere Linie
    oled.drawLine(64, 62, 36, 36);  //Linke Linie
    oled.drawLine(64, 62, 92, 36);  //Rechte Linie

    oled.drawFrame(79, 3, 46, 15);  //Rechteck cm anzeige




    // x Koordinaten Radar OLED Display Start: 31 Ende: 99
    // y Koordinaten Radar OLED Display Start: 25 Ende: 62

    for (int Serv = 0; Serv <= 165; Serv++)  // Servomotor geht nur dreht nur bis 165 alles danach ist quasi wie delay
    {
      Servomotor.write(Serv);
      delay(10);
      int Abstand = Abstandssensor.measureDistanceCm();
      if (Abstand < 30) {
        oled.setFont(u8g2_font_ncenB08_tr);
        if (Abstand <= 99) {
          if (Abstand < 10) {
            oled.drawStr(88, 14, "cm");  //damit cm immer direkt eine Freizeile hinter der entfernung steht
          } else {
            oled.drawStr(94, 14, "cm");
          }
        } else {
          oled.drawStr(82, 14, "no");
        }

        oled.setCursor(82, 14);

        oled.print(Abstand);
        Serial.print(Abstand);
        Serial.println();
      }
    }


    for (int Serv = 165; Serv > 0; Serv--)  //for schleife wird solange ausgeführt bis sie false ist dann geht es erst weiter
    {
      Servomotor.write(Serv);
      delay(10);
      int Abstand = Abstandssensor.measureDistanceCm();
      oled.setFont(u8g2_font_ncenB08_tr);
      if (Abstand < 30) {
        if (Abstand <= 99) {
          if (Abstand < 10) {
            oled.drawStr(88, 14, "cm");  //damit cm immer direkt eine Freizeile hinter der entfernung steht
          } else {
            oled.drawStr(94, 14, "cm");
          }
        } else {
          oled.drawStr(82, 14, "no");
        }

        oled.setCursor(82, 14);

        oled.print(Abstand);
        Serial.print(Abstand);
        Serial.println();
      }
    }







  }

  while (oled.nextPage());
}

Having read your code in more detail, I agree, increasing the i2c clock will make no difference.

The problem is the way you have structured your code.

Try this

#include "U8g2lib.h"
#include "Servo.h"
#include "HCSR04.h"

#define Echopin 13
#define Trigpin 12
#define SERVOPIN 10

// 1,3 Zoll SH1106
U8G2_SH1106_128X64_NONAME_1_HW_I2C oled(U8G2_R0);
UltraSonicDistanceSensor Abstandssensor(Trigpin, Echopin);


Servo Servomotor;
int Serv = 0;

void setup() {
  oled.setBusClock(400000);
  oled.begin();
  oled.clearBuffer();
  Serial.begin(9600);
  Servomotor.attach(SERVOPIN);
  Servomotor.write(0);
}

void loop() {


  //Zeichnen der Benutzeroberfläche OLED:

  oled.setDrawColor(1);  // Farbe weiß

    oled.drawRFrame(0, 0, 128, 64, 3);   //Rahmen
    oled.setFont(u8g2_font_helvB12_tf);  //12 px
    oled.drawStr(2, 15, "Sonar:");
    oled.drawLine(3, 18, 51, 18);  //Unterschrift von "Sonar"

    //Radar:
    oled.drawCircle(65, 62, 34, U8G2_DRAW_UPPER_RIGHT);  //Großer Kreis Rechts
    oled.drawCircle(63, 62, 34, U8G2_DRAW_UPPER_LEFT);   //Großer Kreis Links

    oled.drawCircle(65, 62, 24, U8G2_DRAW_UPPER_RIGHT);  //Mittlerer Kreis Rechts
    oled.drawCircle(63, 62, 24, U8G2_DRAW_UPPER_LEFT);   //Mittlerer Kreis Links

    oled.drawCircle(65, 62, 14, U8G2_DRAW_UPPER_RIGHT);  //Kleiner Kreis Rechts
    oled.drawCircle(63, 62, 14, U8G2_DRAW_UPPER_LEFT);   //Kleiner Kreis Rechts

    oled.drawLine(64, 62, 64, 25);  //Mittlere Linie
    oled.drawLine(64, 62, 36, 36);  //Linke Linie
    oled.drawLine(64, 62, 92, 36);  //Rechte Linie

    oled.drawFrame(79, 3, 46, 15);  //Rechteck cm anzeige

    oled.sendBuffer();


    // x Koordinaten Radar OLED Display Start: 31 Ende: 99
    // y Koordinaten Radar OLED Display Start: 25 Ende: 62

    for (int Serv = 0; Serv <= 165; Serv++)  // Servomotor geht nur dreht nur bis 165 alles danach ist quasi wie delay
    {
      Servomotor.write(Serv);
      delay(10);
      int Abstand = Abstandssensor.measureDistanceCm();
      if (Abstand < 30) {
        oled.setFont(u8g2_font_ncenB08_tr);
        if (Abstand <= 99) {
          if (Abstand < 10) {
            oled.drawStr(88, 14, "cm");  //damit cm immer direkt eine Freizeile hinter der entfernung steht
          } else {
            oled.drawStr(94, 14, "cm");
          }
        } else {
          oled.drawStr(82, 14, "no");
        }

        oled.setCursor(82, 14);

        oled.print(Abstand);
        oled.sendBuffer();
        Serial.print(Abstand);
        Serial.println();
      }
    }


    for (int Serv = 165; Serv > 0; Serv--)  //for schleife wird solange ausgeführt bis sie false ist dann geht es erst weiter
    {
      Servomotor.write(Serv);
      delay(10);
      int Abstand = Abstandssensor.measureDistanceCm();
      oled.setFont(u8g2_font_ncenB08_tr);
      if (Abstand < 30) {
        if (Abstand <= 99) {
          if (Abstand < 10) {
            oled.drawStr(88, 14, "cm");  //damit cm immer direkt eine Freizeile hinter der entfernung steht
          } else {
            oled.drawStr(94, 14, "cm");
          }
        } else {
          oled.drawStr(82, 14, "no");
        }

        oled.setCursor(82, 14);

        oled.print(Abstand);
        oled.sendBuffer();
        Serial.print(Abstand);
        Serial.println();
      }
    }
}

There are other problems with your code.

For example:

      if (Abstand < 30) {
        if (Abstand <= 99) {

If a value is less than 30, it must also be less than 99, so why test it against 99?

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