2

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 { } }

timemage
  • 5,639
  • 1
  • 14
  • 25
PGI
  • 21
  • 2

1 Answers1

1
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.

timemage
  • 5,639
  • 1
  • 14
  • 25