Arduino restarting

So I’m not that good with C++/C#. I’m not sure what could be wrong with this code:

#include <Servo.h>

#define trig 13
#define echo 12
Servo myservo;
int servoLeft = 10; //angle of microservo rotation to scan on left
int servoForward = 75;
int servoRight = 150; //angle of microservo rotation to scan on right
int a=0;
int motorA1 = 2;
int motorA2 = 4;
int motorB1 = 6; 
int motorB2 = 7;
int czas, scanLeft, scanRight, scanForward;

void setup() {
  // servo pin definition
  myservo.attach(9);

   //ultrasonic sensor
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);

  //motors
  pinMode(motorA1, OUTPUT);
  pinMode(motorA2, OUTPUT);
  pinMode(motorB1, OUTPUT);
  pinMode(motorB2, OUTPUT);
}

void loop() {
 
  scanForwardServo();
  delay(250);
  scanRightServo();
  delay(250);
  scanLeftServo();
  delay(250);

if(scanForward>40){
  forward();
}  

if(scanForward<30){
  backward();
  delay(700);
  moveStop();
}

if(scanForward<40 && scanLeft>scanRight){
  left();
  delay(1000);
  moveStop();
}

if(scanForward<40 && scanRight>scanLeft){
  right();
  delay(1000);
  moveStop;
}
}

void scanForwardServo(){
  myservo.write(servoForward);
  digitalWrite(trig, HIGH);
  delay(500);
  digitalWrite(trig, LOW);
  czas = pulseIn(echo, HIGH);
  scanForward = (czas/2)/29.1;
}

 void scanRightServo(){
  myservo.write(servoRight);

  delay(50);
  digitalWrite(trig, HIGH);
  delay(500);
  digitalWrite(trig, LOW);
  czas = pulseIn(echo, HIGH);
  scanRight = (czas/2)/29.1;
 }

 void scanLeftServo(){
  myservo.write(servoLeft);

  delay(50);
  digitalWrite(trig, HIGH);
  delay(500);
  digitalWrite(trig, LOW);
  czas = pulseIn(echo, HIGH);
  scanLeft = (czas/2)/29.1;
}

void forward(){
  digitalWrite(motorA1, HIGH);
  digitalWrite(motorA2, LOW);
  digitalWrite(motorB1, HIGH);
  digitalWrite(motorB2, LOW); 
}

void backward(){
  digitalWrite(motorA1, LOW);
  digitalWrite(motorA2, HIGH);
  digitalWrite(motorB1, LOW);
  digitalWrite(motorB2, HIGH);
}

void left(){
  digitalWrite(motorA1, HIGH);
  digitalWrite(motorA2, LOW);
  digitalWrite(motorB1, LOW);
  digitalWrite(motorB2, LOW);
}

void right(){
  digitalWrite(motorA1, LOW);
  digitalWrite(motorA2, LOW);
  digitalWrite(motorB1, HIGH);
  digitalWrite(motorB2, LOW); 
}

void moveStop(){
  digitalWrite(motorA1, LOW);
  digitalWrite(motorA2, LOW);
  digitalWrite(motorB1, LOW);
  digitalWrite(motorB2, LOW);
}

It keeps restarting the Arduino after executing

  scanForwardServo();
  delay(250);
  scanRightServo();
  delay(250);
  scanLeftServo();
  delay(250);

Any idea on what could be wrong?

What does it do after that block of code? It appears that it tries to go forward, backward, left, or right. That mean that it is firing up a motor, which suggests that you are trying to power the motors by the Arduino, which will NOT work.

I'm using an L298N motor controller:) After that block of code it turns the servo and reads the distances as it's meant to do by the methods (scanRightServo, scanLeftServo, scanForwardServo).

Do you have a separate power supply for the motors? Or do you feed the arduino from the same power supply?

currently I power them both from usb 5v, same power supply.

You need more power than a single USB connection can provide.

tachanka: currently I power them both from usb 5v, same power supply.

So when you switch the motor on it draws so much power that the voltage drops below the limits of the processor on the board and as a result the arduino reboots.

Use a separate power supply for the motors.

ok, will try that tomorrow.