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 ![]()
#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());
}