# Arduino suddenly working without delay

**URL:** <https://forum.arduino.cc/t/arduino-suddenly-working-without-delay/1128154>\
**Category:** Programming\
**Created:** [May 17, 2023, 7:00pm UTC](https://forum.arduino.cc/t/arduino-suddenly-working-without-delay/1128154 "2023-05-17T19:00:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![khaiaz](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/khaiaz/32/914897_2.png) [@khaiaz](https://forum.arduino.cc/u/khaiaz)\
**Post date:** [May 17, 2023, 7:00pm UTC](https://forum.arduino.cc/t/arduino-suddenly-working-without-delay/1128154/1 "2023-05-17T19:00:15Z")

</div>

I'm working on automatic planting system where the a pump will supply water when the humidity of the sand are low. In this project, I'm also using ultrasonic sensor to control the water inside the tank. Once the tank already reach its minimum water level, a buzzer will ring. However, the system seems only can running properly (with delay) in a few minutes. After that, the system suddenly has no delay which make the serial monitor prints the output too fast (without delay). It's that the problem coming from the arduino board itself of my codes?

Here is the code:

```arduino
int water; //random variable 
const int TRIG_PIN = 9; // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int ECHO_PIN = 10; // Arduino pin connected to Ultrasonic Sensor's ECHO pin
const int BUZZER_PIN = 13; // Arduino pin connected to Piezo Buzzer's pin
const int DISTANCE_THRESHOLD = 20; // centimeters
float duration_us, distance_cm;

void setup(){
  Serial.begin(9600);
  pinMode(3,OUTPUT); //output pin for relay board, this will sent signal to the relay
  pinMode(6,INPUT); //input pin coming from soil sensor
  pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode
  pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode
  pinMode(BUZZER_PIN, OUTPUT);
}

void loop() { 
  water = digitalRead(6); // reading the coming signal from the soil sensor
  Serial.print(F("Water Level = "));
  Serial.println(water);

  if(water == 10) // if water level is full then cut the relay 
  {
  digitalWrite(3,LOW); // low is to cut the relay
  }
  else
  {
  digitalWrite(3,LOW); //high to continue proving signal and water supply
  }

  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW); // measure duration of pulse from ECHO pin
  duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance
  distance_cm = 0.017 * duration_us;

  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  if(distance_cm > DISTANCE_THRESHOLD)
    {
digitalWrite(BUZZER_PIN, HIGH);
delay(800);
digitalWrite(BUZZER_PIN, LOW);
}
  else
  { digitalWrite(BUZZER_PIN, LOW); // turn off Piezo Buzzer
  }
delay(800);
}

```

---

<div class="post-metadata">

**Author:** ![cncnotes](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cncnotes/32/1098836_2.png) [@cncnotes](https://forum.arduino.cc/u/cncnotes)\
**Post date:** [May 17, 2023, 7:05pm UTC](https://forum.arduino.cc/t/arduino-suddenly-working-without-delay/1128154/2 "2023-05-17T19:05:09Z")

</div>

> [@khaiaz](#):
>
> ```arduino
> if(water == 10) // if water level is full then cut the relay 
> {
> digitalWrite(3,LOW); // low is to cut the relay
> }
> else
> {
> digitalWrite(3,LOW); //high to continue proving signal and water supply
> }
> 
> ```

You are using LOW for both conditions.

Can you please clarify what you mean by system running properly?

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [May 17, 2023, 7:09pm UTC](https://forum.arduino.cc/t/arduino-suddenly-working-without-delay/1128154/3 "2023-05-17T19:09:09Z")

</div>

for who is serial output? is someone in greenhouse near and read messages with computer?  
i mean you can delete any serial instructions and problem disappear.

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [May 17, 2023, 7:20pm UTC](https://forum.arduino.cc/t/arduino-suddenly-working-without-delay/1128154/4 "2023-05-17T19:20:00Z")

</div>

```arduino
  int water = digitalRead(6); // reading the coming signal from the soil sensor
  if (water < 11)digitalWrite(3, LOW); // low is to cut the relay // if water level is full then cut the relay

```

is soil sensor digital or analog?  
where is it connected, to digital pin or to analog?  
should it be `analogRead` or `digitalRead`?

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [May 17, 2023, 7:27pm UTC](https://forum.arduino.cc/t/arduino-suddenly-working-without-delay/1128154/5 "2023-05-17T19:27:14Z")

</div>

```arduino
const int TRIG_PIN = 9; // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int ECHO_PIN = 10; // Arduino pin connected to Ultrasonic Sensor's ECHO pin
const int BUZZER_PIN = 13; // Arduino pin connected to Piezo Buzzer's pin
const int DISTANCE_THRESHOLD = 20; // centimeters

void setup() {
  Serial.begin(9600);
  pinMode(3, OUTPUT); //output pin for relay board, this will sent signal to the relay
  pinMode(6, INPUT); //input pin coming from soil sensor
  pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode
  pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode
  pinMode(BUZZER_PIN, OUTPUT);
}

void loop() {
  if (analogRead(A0) < 11)digitalWrite(3, LOW); // low is to cut the relay // if water level is full then cut the relay
  else digitalWrite(3, HIGH); //high to continue proving signal and water supply

  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW); // measure duration of pulse from ECHO pin
  int distance_cm = pulseIn(ECHO_PIN, HIGH) / 58; // calculate the distance

  if (distance_cm > DISTANCE_THRESHOLD) {
    digitalWrite(BUZZER_PIN, HIGH);
    delay(800);
    digitalWrite(BUZZER_PIN, LOW);
  }
  else digitalWrite(BUZZER_PIN, LOW); // turn off Piezo Buzzer

  delay(800);
}

```

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [May 17, 2023, 7:28pm UTC](https://forum.arduino.cc/t/arduino-suddenly-working-without-delay/1128154/6 "2023-05-17T19:28:57Z")

</div>

Please edit your post and present the code after pretty formatting by the IDE (CTRL-T for Auto Format).

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [May 17, 2023, 11:45pm UTC](https://forum.arduino.cc/t/arduino-suddenly-working-without-delay/1128154/7 "2023-05-17T23:45:42Z")

</div>

@khaiaz - [Here is a simulation](https://wokwi.com/projects/365016614871960577) where you can see that your code runs well. I made minor adjustments to make the simulation readable.

---

<div class="post-metadata">

**Author:** ![khaiaz](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/khaiaz/32/914897_2.png) [@khaiaz](https://forum.arduino.cc/u/khaiaz)\
**Post date:** [May 20, 2023, 7:37am UTC](https://forum.arduino.cc/t/arduino-suddenly-working-without-delay/1128154/8 "2023-05-20T07:37:32Z")

</div>

Thank you guys, the problem has been solved. I changed to a new and everything works fine.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 16, 2023, 7:38am UTC](https://forum.arduino.cc/t/arduino-suddenly-working-without-delay/1128154/9 "2023-11-16T07:38:08Z")

</div>

Closed on November 16, 2023, 7:38am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
