Ultrasonic Sensor only reads for 2 sec

Hello, I have connected the sensor and have uploaded the code and it returns correct measurements, but only for 2 seconds and I can't figure out why? When I change the delay it will return measurements at those intervals but only for 2seconds i.e. (100) gives 20 readings (2000) gives 1 reading. Any help in seeing what I am missing would be a great help. Here is the code that I am running:

/* * Ultrasonic Sensor HC-SR04 and Arduino Tutorial * * Crated by Dejan Nedelkovski, * www.HowToMechatronics.com * */

// defines pins numbers const int trigPin = 11; const int echoPin = 12;

// defines variables long duration; int distance;

void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication }

void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microsecond duration = pulseIn(echoPin, HIGH);

// Calculating the distance distance= (duration/2) / 74;

// Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance);

delay(100);

}

How is your sensor powered?

const byte trigPin = 11;
const byte echoPin = 12;


void setup() 
{
  pinMode(trigPin, OUTPUT); 
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}

void loop() 
{
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  unsigned long duration = pulseIn(echoPin, HIGH);

  unsigned long distance= (duration/2) / 74;

  Serial.print("Distance: ");
  Serial.println(distance);

  delay(100);

}

(I didn't compile this code, so it isn't tested either)

Which sensor, exactly?