Ethernet 5100 + HCSR04 = working one time and freeze

Hi everyone,

I connect the Arduino UNO with Ethernet Shield 5100. Program checks whether there is something in the range of 0-50. When something disappears out of sight, the Arduino sends the PHP information that has detected movement. I like everything ok, but once it detects motion, it will send the information then freezes.
After sending the first data, HC SR04 detects an object at a distance of 0 (even though there is no object in front of it). What one sampling, one zero even though there will be no object in front of the sensor. The sensor does not detect approaching - only shows zero. What’s going on?

Can someone please help me solve this problem?

#include <string>
#include <SPI.h>
#include <Ethernet.h>

int impuls = 12;
int sonar = 11;
long czas, cm;

String goodArray = ""; 
String helpArray = ""; 

String pauza = "false";

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "www.host.pl"; 
IPAddress ip(192,168,0,177);
EthernetClient client;

void setup() 
{
  pinMode(impuls, OUTPUT);
  pinMode(sonar, INPUT);
  Serial.begin(9600);
}

void loop()
{
  digitalWrite(impuls, LOW);
  delayMicroseconds(2);
  digitalWrite(impuls, HIGH);
  delayMicroseconds(10);
  digitalWrite(impuls, LOW);
  czas = pulseIn(sonar, HIGH);
  cm = microsecondsToCentimeters(czas);
  
  if(pauza == "false")
  {
    if(cm <= 50)
    {
      Serial.print(cm);
      Serial.print("\n");
      goodArray = goodArray + "," + cm;
      helpArray = "";
    }
    else if(cm > 50) 
    { 
      if(goodArray.length() >= 2)
      {
        helpArray = helpArray + "0";
        if(helpArray.length() >= 20)
        {
            pauza = "true";
            senddata(goodArray);
        }
      }
    }
    delay(100);
  }
}

long microsecondsToCentimeters(long microseconds)
{return microseconds / 29 / 2;}

void senddata(String dane)
{
  if (Ethernet.begin(mac) == 0) 
  {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }
  delay(1000);
  Serial.println("connecting...");

  if (client.connect(server, 80)) 
  {
    Serial.println("connected");
    client.println("GET /url.php?var=");
    client.println(dane);
    client.println(" HTTP/1.1");
    client.println("Host: www.host.pl");
    client.println("Connection: close");
    client.println();
  } 
  else 
  {
    Serial.println("connection failed");
  } 
  pauza = "false";
}

What's going on?

To be blunt, you are an idiot.

int impuls = 12;
int sonar = 11;

Pins 10, 11, 12, and 13 are used for communication between the Arduino and the Ethernet board. You can NOT use them for your ping sensor, too.