Multiple Sonar Sensors Giving Weird Values

Hi, so i'm using two HC-SR04 sonar sensors on my dwenguino, they control the motors of the robot.

We started out with one sonar on the front, which worked good. But now we wanted to add a second sonar on the back of the robot. But when testing the sonar and writing both values to the LCD, the sonar attached to the front of the robot always gave correct values, while the sonar connected to the back of the robot always gave weird/glitchy values on the LCD, the value jumps to 0 alot, and is just really inconsistent.
Nothing happens when switching the two sonars, so it has nothing to do with the sonar itself.

I'm using this code to test :

#include <LiquidCrystal.h>                                
#include <Wire.h>                                         
#include <Dwenguino.h>                                    
#include <NewPing.h>                                      
#include <dht.h>   

#define MAX_DISTANCE 500  

void setup() {
  
  initDwenguino();
}

void loop() {
  
NewPing sonar2(14, 13, MAX_DISTANCE);
NewPing sonar1(12, 11, MAX_DISTANCE);


  unsigned int afstand2 = sonar2.ping_cm();
  unsigned int afstand1 = sonar1.ping_cm();

dwenguinoLCD.clear();

dwenguinoLCD.setCursor(0,0);

dwenguinoLCD.print(afstand1);

dwenguinoLCD.setCursor(0,1);

dwenguinoLCD.print(afstand2);
}
}

Any help would be appreciated!

This looks like an initialisation which should be in setup() and not the loop() :

NewPing sonar2(14, 13, MAX_DISTANCE);
NewPing sonar1(12, 11, MAX_DISTANCE);

Maybe the remaining code in the loop() should be executed only periodically and not on every loop iteration:
Something like:

void loop() {
static unsigned long lastIteration = 0;
if (millis() - lastIteration > 100 ) { // 100 mS interval
lastIteration = millis() ;

// Your code here executed once every 100 mS

}

}

That doesn't work, if i put it in the setup, i get a compilation error :

True. I was too quick there. It certainly does not belong in the loop though and should be global definition. See this example:

   #include <NewPing.h>
     
    #define TRIGGER_PIN  12
    #define ECHO_PIN     11
    #define MAX_DISTANCE 200
     
    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
     
    void setup() {
      Serial.begin(115200);
    }
     
    void loop() {
      delay(50);
      Serial.print("Ping: ");
      Serial.print(sonar.ping_cm());
      Serial.println("cm");
    }