Serial printing change the value that i get

That is interesting but , when i counting a x variable , it gives wrong values. But when i add a serialprintln(x) , it is giving me normal values. Without serialprint , x counts unstable. Sometimes slow sometimes fast. ::slight_smile:
Same thing for the ultrasonic sensor. I have to print distance to get normal values.

Is always serial print harmful for Arduino? Because i will plug an adaptor to work Arduino and it will work maybe months like that.

 while (digitalRead(led) == HIGH) {
    not bekleme++;
    bekleme = 0;
    say++;
    x = 2  + say /160 ;
    tamburcap = 106;  //cm
    cap = 13.55;      //mm
    istenilen = 9999;
    long duration , distance;
    long durationt , distancet;
    digitalWrite(trigpin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigpin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigpin, LOW);
    duration = pulseIn(echopin, HIGH);
    distance = duration / 58.2;
   // Serial.println(distance);
    Serial.println(x);
    if (timerRunning == 0 && distance < 10) {

      t = 0;
      tektur = ((pi * tamburcap * 1 * pi * (cap / 10) * (cap / 10) * 7.8) / 4000);
      tur = sayac + 1;
      y = (60 * pi * (tamburcap / 100)) / x;
      digitalWrite (ledPin, HIGH);
      not say++;
      eventtime1 = millis();
       
      Serial.println(kilo);


      if (y < 40) {
       interval = 2 + (eventtime1 - eventtime2) / 1000;
        timerRunning = 1;

        if (interval >= 10 && interval < 60 ) {

          sure = 7;
          sur = sur + sure;
          ortsur = sur / (tur);
          v = (60 * pi * (tamburcap / 100)) / sure;
          kilo = ((pi * tamburcap * (tur ) * pi * (cap / 10) * (cap / 10) * 7.8) / 4000) ; // pi.d^2 /4 yaptık ve gr sonucunu kiloya çevirdik
          total = total + v;
          ortalama = total / tur;
          esp.println("AT+CIPSTART=\"TCP\",\"" + ip + "\",80");
          delay(100);
          String sar = "GET http://" + ip + "/esp8266/postAnlık.php?Hammadde=-&Miktar=" + String(kilo) + "kg&Hizi=" + String(v) + "m/dk&TurSüresi=" + String(interval) + "";
          esp.print("AT+CIPSEND=");
          esp.println(sar.length() + 2);
          esp.println("GET http://" + ip + "/esp8266/postAnlık.php?Hammadde=-&Miktar=" + String(kilo) + "kg&Hizi=" + String(v) + "m/dk&TurSüresi=" + String(interval) + "");
          delay(100);
          esp.println("GET http://" + ip + "/esp8266/postAnlık.php?Hammadde=-&Miktar=" + String(kilo) + "kg&Hizi=" + String(v) + "m/dk&TurSüresi=" + String(interval) + "");
          Serial.print("Haddehızı(m/dk):");
          sayac++;
        }
        if (interval > 2 && interval < 10) {
          sur = sur + interval ;
          ortsur = sur / (tur);
          v = (60 * pi * (tamburcap / 100)) / interval;
          x = 2  + say / 160;
          total = total + v;
          ortalama = total / tur;
          kilo = ((pi * tamburcap * (tur ) * pi * (cap / 10) * (cap / 10) * 7.8) / 4000) ; // pi.d^2 /4 yaptık ve gr sonucunu kiloya çevirdik
          sayac++;
          esp.println("AT+CIPSTART=\"TCP\",\"" + ip + "\",80");
          delay(100);
          String sar = "GET http://" + ip + "/esp8266/postAnlık.php?Hammadde=-&Miktar=" + String(kilo) + "kg&Hizi=" + String(v) + "m/dk&TurSüresi=" + String(interval) + "";
          esp.print("AT+CIPSEND=");
          esp.println(sar.length() + 2);
          esp.println("GET http://" + ip + "/esp8266/postAnlık.php?Hammadde=-&Miktar=" + String(kilo) + "kg&Hizi=" + String(v) + "m/dk&TurSüresi=" + String(interval) + "");
          esp.println("GET http://" + ip + "/esp8266/postAnlık.php?Hammadde=-&Miktar=" + String(kilo) + "kg&Hizi=" + String(v) + "m/dk&TurSüresi=" + String(interval) + "");
          // Serial.println(ortalama);
        }



        while (distance < 10) {
          not say++;
          say = 0;
          digitalWrite(trigpin, LOW);
          delayMicroseconds(2);
          digitalWrite(trigpin, HIGH);
          delayMicroseconds(10);
          digitalWrite(trigpin, LOW);
          duration = pulseIn(echopin, HIGH);
          distance = duration / 58.2;
          delay(1000);
        }

      }
    }

    else if (timerRunning == 1 && distance >= 10) {

      eventtime2 = millis();
      timerRunning = 0;

      digitalWrite (ledPin, LOW);


    }
  }

""here is my code. just a part because it is too long if its neccessary i can share. I can calculate the time between two cycle. I tried to use millis for eveywhere but my sensor goes crazy after that. So i count “x” and divide it to 160 in order to find a second .
I fixed the ultrasonic sensor by adding these :-
interval = 2 + (eventtime1 - eventtime2) / 1000;
timerRunning = 1;
-: inside if(y<40) -
they were inside if (timerRunning == 0 && distance < 10) this before.

but my counter x is still doesnt work right.

We really need to see your code in order to help you.