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,6391 gold badge14 silver badges25 bronze badges
asked Jun 10, 2021 at 16:08
3
  • 3
    Stuck how? What's the specific issue? Commented Jun 10, 2021 at 16:31
  • 1
    (Unrelated, but consider naming things to be a bit more descriptive, e.g., if you have to comment M1 with right just name it rightMotor or something :) Might also be convenient to move some of the code out of loop into separate functions to help figure out what's going on.) Commented Jun 10, 2021 at 17:02
  • this coding doesn't detect the distance it prints 0 everytime I'm guessing if it is in the wrong order Commented Jun 11, 2021 at 3:14

1 Answer 1

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.

answered Jun 11, 2021 at 11:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.