My Arduino Uno Board is resetting itself?

I am using this code for obstacle avoiding robot using ultrasonic sensor

#include <Servo.h>

Servo myservo;

#define echoPin 4 // Echo Pin
#define trigPin 5 // Trigger Pin

const int sampling = 30;
unsigned long v ;

#define ir1 2
#define ir2 3
#define m1 9
#define m2 10
#define m3 11
#define m4 12
#define s1 6

int pos = 0;
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance;

void setup() {
  myservo.attach(s1);
  Serial.begin (250000);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(ir1, INPUT);
  pinMode(ir2, INPUT);
  for (int i = 9; i < 13; i++)
  {
    pinMode(i, OUTPUT);
  }

}

void loop() {

  for (int i = 0; i < sampling; i++)
  {
    int dist = ultrasonic();

    v += dist;
  }

  v = v / sampling;

  Serial.println(v);
  if (v < 20)
  {
    backfn();
    turnrightfn();
    stopfn();

    for (int i = 0; i < 180; i++)
    {
      myservo.write(i);
      delay(5);

    }

    for (int j = 180; j > 90 ; j--)
    {
      myservo.write(j);
      delay(5);
    }

  }
  else
  {
    forwardfn();
  }

}

void forwardfn()
{
  digitalWrite(m1, HIGH);
  digitalWrite(m2, LOW);
  digitalWrite(m3, HIGH);
  digitalWrite(m4, LOW);
  // delay(1500);
}

void turnrightfn()
{
  digitalWrite(m1, LOW);
  digitalWrite(m2, HIGH);
  digitalWrite(m3, HIGH);
  digitalWrite(m4, LOW);
  delay(500);
}

void backfn()
{
  digitalWrite(m1, LOW);
  digitalWrite(m2, HIGH);
  digitalWrite(m3, LOW);
  digitalWrite(m4, HIGH);
  delay(500);
}

double ultrasonic()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);

  //Calculate the distance (in cm) based on the speed of sound.
  distance = duration / 58.2;

  //Delay 50ms before next reading.
  return distance;
}

void stopfn()
{
  digitalWrite(m1, LOW);
  digitalWrite(m2, LOW);
  digitalWrite(m3, LOW);
  digitalWrite(m4, LOW);

}

It works as expected but my problem is, in between execution UNO board is resetting itself and it stops and starts fresh which eventually results in robot waiting for 2 to 3 seconds. I really dont understand what might be the problem, tried connecting 10uf capacitor between reset and ground but of no use, but I dont want my board to reset while executing the code, Any suggestion?

What is the power supply for the robot? The most likely reason for the reset problem is that something is drawing enough current to pull the power supply voltage down far enough to reset the board.

Don't power the servo from the Arduino.

Mark

It did the trick guys changing the batteries got me the solution.

Thanks for the info