user77617: I am making a line follower car that follows the line when push button is pressed. And by using ultrasonic sensor I want the car to stop when object ahead distance is closer than 100 and then make servo motor turn to 180 degrees.
Used:

Arduino UNO
2 DC motor
cytron maker line sensor
1 servo motor
1 ultrasonic sensor
1 push button.

Got stuck coding, I would like some help please.
int E1 = 4;
int M1 = 5; // Right
int M2 = 6; // Left
int E2 = 7;
int trigPin = 4;
int echoPin = 5;


void setup() {
  Serial.begin(9600);
  
  pinMode(E1, OUTPUT);
  pinMode(M1, OUTPUT);
  pinMode(E2, OUTPUT);
  pinMode(M2, OUTPUT);
  
  digitalWrite(E1, HIGH);
  digitalWrite(E2, HIGH);
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}


void loop() {
  float duration, distance;
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = ((float)(340 * duration) / 1000) / 2;
  
  Serial.print("\nDIstance:");
  Serial.print(distance);
  Serial.println("mm\n");
  delay(200);
  
  int D1 = digitalRead(8);
  int D2 = digitalRead(9);
  int D3 = digitalRead(10);
  int D4 = digitalRead(11);
  int D5 = digitalRead(12);
  
  if (D1 == 0 && D2 == 0 && D3 == 1 && D4 == 0 && D5 == 0) {
    analogWrite(M1, 100);
    analogWrite(M2, 100);
  }
  else if (D1 == 0 && D2 == 1 && D3 == 1 && D4 == 0 && D5 == 0) {
    analogWrite(M1, 80);
    analogWrite(M2, 100);
  }
  else if (D1 == 0 && D2 == 1 && D3 == 0 && D4 == 0 && D5 == 0) {
    analogWrite(M1, 60);
    analogWrite(M2, 100);
  }
  else if (D1 == 1 && D2 == 1 && D3 == 0 && D4 == 0 && D5 == 0) {
    analogWrite(M1, 30);
    analogWrite(M2, 100);
  }
  else if (D1 == 1 && D2 == 0 && D3 == 0 && D4 == 0 && D5 == 0) {
    analogWrite(M1, 0);
    analogWrite(M2, 100);
  }
  else if (D1 == 0 && D2 == 0 && D3 == 1 && D4 == 1 && D5 == 0) {
    analogWrite(M1, 100);
    analogWrite(M2, 80);
  }
  else if (D1 == 0 && D2 == 0 && D3 == 0 && D4 == 1 && D5 == 0) {
    analogWrite(M1, 100);
    analogWrite(M2, 60);
  }
  else if (D1 == 0 && D2 == 0 && D3 == 0 && D4 == 1 && D5 == 1) {
    analogWrite(M1, 100);
    analogWrite(M2, 30);
  }
  else if (D1 == 0 && D2 == 0 && D3 == 0 && D4 == 0 && D5 == 1) {
    analogWrite(M1, 100);
    analogWrite(M2, 0);
  }
  else if (D1 == 0 && D2 == 0 && D3 == 0 && D4 == 0 && D5 == 0) {
    analogWrite(M1, 0);
    analogWrite(M2, 0);
  }
  else if (D1 == 1 && D2 == 1 && D3 == 1 && D4 == 1 && D5 == 1) {
    analogWrite(M1, 00);
    analogWrite(M2, 00);
  }
  else {
  }
}


user70020: int M1 = 5; // Right
...
int echoPin = 5;

You appear to be trying to use one of your motors on the same pin as your return signal from the ultrasonic range finder.  When analogWrite(M1, whatever), the internals of analogWrite will set the pin to output, overriding your own pinMode(echoPin, INPUT) in setup().
Later when you pulseIn(echoPin, HIGH), you're just seeing whatever analogWrite is doing with it.  If you're not touching any buttons, your code is executing analogWrite(M1, 0);, so no "distance".
For more fun:  If all of this is correct, if you hold down the buttons to execute analogWrite(M1, 100), pulseIn should report a value of about (1.0/980) * 100/256 or 399.  The 980 being the frequency pin 5 uses on the UNO for analogWrite.  This should cause your code to print something around 66.

77617: <p>I am making a line follower car that follows the line when push button is pressed. And by using ultrasonic sensor I want the car to stop when object ahead distance is closer than 100 and then make servo motor turn to 180 degrees.</p>
<p>Used:</p>
<ul>
<li>Arduino UNO</li>
<li>2 DC motor</li>
<li>cytron maker line sensor</li>
<li>1 servo motor</li>
<li>1 ultrasonic sensor</li>
<li>1 push button.</li>
</ul>
<p>Got stuck coding, I would like some help please.</p>
<pre class="lang-cpp prettyprint-override"><code>int E1 = 4;
int M1 = 5; // Right
int M2 = 6; // Left
int E2 = 7;
int trigPin = 4;
int echoPin = 5;


void setup() {
  Serial.begin(9600);
  
  pinMode(E1, OUTPUT);
  pinMode(M1, OUTPUT);
  pinMode(E2, OUTPUT);
  pinMode(M2, OUTPUT);
  
  digitalWrite(E1, HIGH);
  digitalWrite(E2, HIGH);
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}


void loop() {
  float duration, distance;
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = ((float)(340 * duration) / 1000) / 2;
  
  Serial.print(&quot;\nDIstance:&quot;);
  Serial.print(distance);
  Serial.println(&quot;mm\n&quot;);
  delay(200);
  
  int D1 = digitalRead(8);
  int D2 = digitalRead(9);
  int D3 = digitalRead(10);
  int D4 = digitalRead(11);
  int D5 = digitalRead(12);
  
  if (D1 == 0 &amp;&amp; D2 == 0 &amp;&amp; D3 == 1 &amp;&amp; D4 == 0 &amp;&amp; D5 == 0) {
    analogWrite(M1, 100);
    analogWrite(M2, 100);
  }
  else if (D1 == 0 &amp;&amp; D2 == 1 &amp;&amp; D3 == 1 &amp;&amp; D4 == 0 &amp;&amp; D5 == 0) {
    analogWrite(M1, 80);
    analogWrite(M2, 100);
  }
  else if (D1 == 0 &amp;&amp; D2 == 1 &amp;&amp; D3 == 0 &amp;&amp; D4 == 0 &amp;&amp; D5 == 0) {
    analogWrite(M1, 60);
    analogWrite(M2, 100);
  }
  else if (D1 == 1 &amp;&amp; D2 == 1 &amp;&amp; D3 == 0 &amp;&amp; D4 == 0 &amp;&amp; D5 == 0) {
    analogWrite(M1, 30);
    analogWrite(M2, 100);
  }
  else if (D1 == 1 &amp;&amp; D2 == 0 &amp;&amp; D3 == 0 &amp;&amp; D4 == 0 &amp;&amp; D5 == 0) {
    analogWrite(M1, 0);
    analogWrite(M2, 100);
  }
  else if (D1 == 0 &amp;&amp; D2 == 0 &amp;&amp; D3 == 1 &amp;&amp; D4 == 1 &amp;&amp; D5 == 0) {
    analogWrite(M1, 100);
    analogWrite(M2, 80);
  }
  else if (D1 == 0 &amp;&amp; D2 == 0 &amp;&amp; D3 == 0 &amp;&amp; D4 == 1 &amp;&amp; D5 == 0) {
    analogWrite(M1, 100);
    analogWrite(M2, 60);
  }
  else if (D1 == 0 &amp;&amp; D2 == 0 &amp;&amp; D3 == 0 &amp;&amp; D4 == 1 &amp;&amp; D5 == 1) {
    analogWrite(M1, 100);
    analogWrite(M2, 30);
  }
  else if (D1 == 0 &amp;&amp; D2 == 0 &amp;&amp; D3 == 0 &amp;&amp; D4 == 0 &amp;&amp; D5 == 1) {
    analogWrite(M1, 100);
    analogWrite(M2, 0);
  }
  else if (D1 == 0 &amp;&amp; D2 == 0 &amp;&amp; D3 == 0 &amp;&amp; D4 == 0 &amp;&amp; D5 == 0) {
    analogWrite(M1, 0);
    analogWrite(M2, 0);
  }
  else if (D1 == 1 &amp;&amp; D2 == 1 &amp;&amp; D3 == 1 &amp;&amp; D4 == 1 &amp;&amp; D5 == 1) {
    analogWrite(M1, 00);
    analogWrite(M2, 00);
  }
  else {
  }
}
</code></pre>
 70020: <pre class="lang-cpp prettyprint-override"><code>int M1 = 5; // Right
...
int echoPin = 5;
</code></pre>
<p>You appear to be trying to use one of your motors on the same pin as your return signal from the ultrasonic range finder.  When <code>analogWrite(M1, whatever)</code>, the internals of <code>analogWrite</code> will <a href="https://github.com/arduino/ArduinoCore-avr/blob/1.8.3/cores/arduino/wiring_analog.c#L111" rel="nofollow noreferrer">set the pin to output</a>, overriding your own <code>pinMode(echoPin, INPUT)</code> in <code>setup()</code>.</p>
<p>Later when you <code>pulseIn(echoPin, HIGH)</code>, you're just seeing whatever <code>analogWrite</code> is doing with it.  If you're not touching any buttons, your code is executing <code>analogWrite(M1, 0);</code>, so no &quot;distance&quot;.</p>
<p>For more fun:  If all of this is correct, if you hold down the buttons to execute <code>analogWrite(M1, 100)</code>, <code>pulseIn</code> should report a value of about (1.0/980) * 100/256 or 399.  The 980 being the frequency pin 5 uses on the UNO for <a href="https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/" rel="nofollow noreferrer">analogWrite</a>.  This should cause your code to print something around 66.</p>
