Group project but is our code okay?

Hello everyone,
Me and my mate are working on an Arduino project. It is a robot with 3 ultrasonic sensors that should follow a hallway and turn when it gets close to the wall. (Left Sensor, Right Sensor, Front sensor). After the turn it should make a 90° turn and continue. Unfortunatly the robot doesn't allways make a 90° turn. We use normal DC motors with a high power driver and an arduino uno. How can we fix this problem?

// Sensors
#define trigPinR 3
#define echoPinR 2

#define trigPinL 13
#define echoPinL 12

#define trigPinV 11
#define echoPinV 10

long duurR, cmR;
long duurL, cmL;
long duurV, cmV;

// Motor 
 
int enA = 9;
int in1 = 8;
int in2 = 7;

int enB = 5;
int in3 = 6;
int in4 = 4;

float huidigeMeting = 0; // huideMeting means current measurement
float vorigeMeting = 0;  // vorigeMeting means previous measurement

#define huidigeMeting cmR

void setup()

// Sensors values
{
pinMode(echoPinR, INPUT);
pinMode(trigPinR, OUTPUT);
pinMode(echoPinL, INPUT);
pinMode(trigPinL, OUTPUT);
pinMode(echoPinV, INPUT);
pinMode(trigPinV, OUTPUT);
Serial.begin(9600);

//Motor
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  

//Motor goes forward
   digitalWrite(in1, LOW);
   digitalWrite(in2, HIGH);
   analogWrite(enA, 100);
Serial.println("Vooruit");
   digitalWrite(in3, HIGH);
   digitalWrite(in4, LOW);
   analogWrite(enB, 100);
delay(1000);

}
 
void loop()
{

//Sensor measurements
digitalWrite(trigPinR, LOW);
delayMicroseconds(5);
digitalWrite(trigPinR, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinR, LOW);
pinMode(echoPinR, INPUT);
duurR = pulseIn(echoPinR, HIGH);

cmR = duurR/2 * 0.0343;

digitalWrite(trigPinL, LOW);
delayMicroseconds(5);
digitalWrite(trigPinL, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinL, LOW);

pinMode(echoPinL, INPUT);
duurL = pulseIn(echoPinL, HIGH);

cmL = duurL/2 * 0.0343;

digitalWrite(trigPinV, LOW);
delayMicroseconds(5);
digitalWrite(trigPinV, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinV, LOW);
pinMode(echoPinV, INPUT);
duurV = pulseIn(echoPinV, HIGH);

cmV = duurV/2 * 0.0343;

Serial.print(cmL);
Serial.print("cmL");
Serial.println();
Serial.print(cmV);
Serial.print("cmV");
Serial.println();


//Motor goes forward
   digitalWrite(in1, LOW);
   digitalWrite(in2, HIGH);
   analogWrite(enA, 100);
Serial.println("Vooruit");
   digitalWrite(in3, HIGH);
   digitalWrite(in4, LOW);
   analogWrite(enB, 100);


//Comparison to stay straight using right sensor
if (huidigeMeting > vorigeMeting)
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
   analogWrite(enA, 200);

  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enB, 30); 
} 
else 
{digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
   analogWrite(enA, 30);

  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
   analogWrite(enB, 200); 
   }
vorigeMeting = huidigeMeting;
/*
Serial.print(huidigeMeting);
Serial.print("huidige");
Serial.println();
Serial.print(vorigeMeting);
Serial.print("vorige");
Serial.println();
delay(125);
*/


// Stop if cmV <75 
if (cmV <75) 
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
Serial.println(" Stop ");
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  delay(750);
if (cmR >200) { 
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
   analogWrite(enA, 220);
Serial.println(" Turn right ");
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
   analogWrite(enB, 220);
  delay(600);
  }
 }


//Correction w/ left sensor
if (cmL <20)
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
   analogWrite(enA, 200);
Serial.println(" Turn right ");
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
   analogWrite(enB, 30);
  delay(300);
  }

}

The way or not da way that is the question

Unfortunatly the robot doesn't allways make a 90° turn.

Sometimes it does the hokey pokey?

How can we fix this problem?

First, you clearly explain what the problem is.

float huidigeMeting = 0; // huideMeting means current measurement
float vorigeMeting = 0;  // vorigeMeting means previous measurement

#define huidigeMeting cmR

Why do you have a variable and a #define name that have the same name?

  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

I can't tell you how dumb that looks. The direction of pin on the motor shield is NOT a useful name on the Arduino side of the shield.

//Sensor measurements
digitalWrite(trigPinR, LOW);
delayMicroseconds(5);
digitalWrite(trigPinR, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinR, LOW);
pinMode(echoPinR, INPUT);
duurR = pulseIn(echoPinR, HIGH);

cmR = duurR/2 * 0.0343;

digitalWrite(trigPinL, LOW);
delayMicroseconds(5);
digitalWrite(trigPinL, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinL, LOW);

pinMode(echoPinL, INPUT);
duurL = pulseIn(echoPinL, HIGH);

cmL = duurL/2 * 0.0343;

digitalWrite(trigPinV, LOW);
delayMicroseconds(5);
digitalWrite(trigPinV, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinV, LOW);
pinMode(echoPinV, INPUT);
duurV = pulseIn(echoPinV, HIGH);

cmV = duurV/2 * 0.0343;

Putting 1/3 of that code in a function that took two arguments, the trigger and echo pins, and calling it three times would make a lot more sense.

There is NO reason that the time and distance values should be global.

Serial.print(cmL);
Serial.print("cmL");
Serial.println();
Serial.print(cmV);
Serial.print("cmV");
Serial.println();

Doesn't matter what cmR is, I see.

//Motor goes forward

The motor does not. The robot that the motor is attached to goes, or tries to, forward, even if there is a wall right in front of it.

//Comparison to stay straight using right sensor
if (huidigeMeting > vorigeMeting)

Your #define statement altered that code. Did it do so in a meaningful way?

vorigeMeting = huidigeMeting;

And, it altered that code...

If you properly indented your code, and printed it out, and connected the open and close curly braces with different colored lines, you'd see, I think, that some of your decision gates (the if statements) are nested in ways you don't expect.

NielsRobot:
After the turn it should make a 90° turn and continue. Unfortunatly the robot doesn't allways make a 90° turn.

You need to explain in a lot more detail what actually happens.
Does the problem always happen at one place and never at another place?

Have you any debug code to allow you to distinguish the occasions when it does turn correctly from those when it does not.

This sort of problem can be difficult to debug without access to the hardware for testing.

...R