Ultrasonic sensor not working?

Hi everyone, I'm quiet new to programming and I need some help. I am building a cart that will reverse when it detects the edge. However, my ultrasonic sensor isn't working and my vehicle keeps falling off the edge. Help would be appreciated, thanks! Here is my code:

// Motor 1
int In1 = 7;
int In2 = 8;
int EN1=5;
int SPEED = 200; // Needs to be a PWM pin to be able to control motor speed

// Motor 2
int In3 = 6;
int In4 = 12;

// Motor 3
int In5 = 3;
int In6 = 2;

// Motor 4
int In7 = 4;
int In8 = 11;


const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;

void setup() {
  // put your setup code here, to run once:

pinMode(In1,OUTPUT);
pinMode(In2, OUTPUT);
pinMode(In3,OUTPUT);
pinMode(In4, OUTPUT);
pinMode(In5,OUTPUT);
pinMode(In6, OUTPUT);
pinMode(In7,OUTPUT);
pinMode(In8, OUTPUT);
pinMode(EN1, OUTPUT);


analogWrite(EN1,SPEED);

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication


}


void loop() {

   long duration, distance; // start the scan
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(2); // delays are required for a succesful sensor operation.
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); //this delay is required as well!
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;// convert the distance to centimeters.
  
  if (distance > 2)/*if there's an edge detection 2 cm, ahead, do the following: */ {   
   Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.

Serial.println (" The obstacle is declared a threat due to edge. ");
Serial.println (" Turning !");
    //delay(800);
digitalWrite(In1,HIGH);
digitalWrite(In2,LOW);
digitalWrite(In3,HIGH);
digitalWrite(In4,LOW);
digitalWrite(In5,HIGH);
digitalWrite(In6,LOW);
digitalWrite(In7,LOW);
digitalWrite(In8,HIGH);
}
else{
  
digitalWrite(In1,LOW);
digitalWrite(In2,HIGH);
digitalWrite(In3,LOW);
digitalWrite(In4,HIGH);
digitalWrite(In5,LOW);
digitalWrite(In6,HIGH);
digitalWrite(In7,HIGH);
digitalWrite(In8,LOW);
  }
}

if (distance > 2)did you really mean more than 2cm?
Not many ultrasonic sensors will reliably read very small distances.

Print distance before testing it. What do you see ?

AWOL:
if (distance > 2)did you really mean more than 2cm?
Not many ultrasonic sensors will reliably read very small distances.

Yes that seems to be the issue I changed it to 5 and the wheels reverse whenever it is close to the edge. However, One of my wheels isnt turning at all now and one of them goes in the opposite direction!

The wheels turning in the opposite direction was an easy fix, but one of my motor still isn’t turning. I am also not sure why it does back and forth at the edge either :frowning:

  if (distance > 2)/*if there's an edge detection 2 cm, ahead, do the following: */ {

Comments do not HAVE to follow the statement that they go with. This would be FAR easier to understand as

 // See if there's an edge detection 2 cm ahead 
  if (distance > 2)
  {
     // There is. Do the following

The names of the pins should reflect what they are for, not the direction that other device cares about. In is a stupid term to include in the name of a variable that holds a pin number where the pin's mode is OUTPUT.

PaulS:

  if (distance > 2)/*if there's an edge detection 2 cm, ahead, do the following: */ {

Comments do not HAVE to follow the statement that they go with. This would be FAR easier to understand as

 // See if there's an edge detection 2 cm ahead 

if (distance > 2)
  {
    // There is. Do the following




The names of the pins should reflect what they are for, not the direction that other device cares about. In is a stupid term to include in the name of a variable that holds a pin number where the pin's mode is OUTPUT.

what should I rewrite it too? Like I said, this is my very first time I have used an arduino, sorry if any of my codes offended you.

what should I rewrite it too?

What role, EXACTLY, does the pin you have stored in the variable currently named In1 play? It is NOT an input pin, so the name is CLEARLY wrong, from the perspective of the Arduino, where the code runs.

tan_dennis97:
I am also not sure why it does back and forth at the edge either :frowning:

It looks like your sketch says:
If there is an object within 5 cm,
run in reverse
else
run forward.
When you detect something within 5 cm it will back up until it doesn't detect something within 5 cm, then go forward again. Perhaps you meant to turn while backing up but your motor control code is hard to read and not commented so the intent is unclear.

digitalWrite(In1,HIGH);
digitalWrite(In2,LOW);
digitalWrite(In3,HIGH);
digitalWrite(In4,LOW);
digitalWrite(In5,HIGH);
digitalWrite(In6,LOW);
digitalWrite(In7,LOW);
digitalWrite(In8,HIGH);
}
else{
  
digitalWrite(In1,LOW);
digitalWrite(In2,HIGH);
digitalWrite(In3,LOW);
digitalWrite(In4,HIGH);
digitalWrite(In5,LOW);
digitalWrite(In6,HIGH);
digitalWrite(In7,HIGH);
digitalWrite(In8,LOW);

I assume you have a 4wd robot. 7/8 are the opposite of the other 3.

3 wheel trying to move forward and 1 wheel reverse. I don't think it will turn much.
You should try both wheels on one side going forward while the other side reverse.

johnwasser:
It looks like your sketch says:
If there is an object within 5 cm,
run in reverse
else
run forward.
When you detect something within 5 cm it will back up until it doesn't detect something within 5 cm, then go forward again. Perhaps you meant to turn while backing up but your motor control code is hard to read and not commented so the intent is unclear.

Yes I apologise for the code as this is my first time programming. That is correct, my intent of the robot is to reverse and turn around once it is close the the edge.

Gabriel_swe:

digitalWrite(In1,HIGH);

digitalWrite(In2,LOW);
digitalWrite(In3,HIGH);
digitalWrite(In4,LOW);
digitalWrite(In5,HIGH);
digitalWrite(In6,LOW);
digitalWrite(In7,LOW);
digitalWrite(In8,HIGH);
}
else{
 
digitalWrite(In1,LOW);
digitalWrite(In2,HIGH);
digitalWrite(In3,LOW);
digitalWrite(In4,HIGH);
digitalWrite(In5,LOW);
digitalWrite(In6,HIGH);
digitalWrite(In7,HIGH);
digitalWrite(In8,LOW);




I assume you have a 4wd robot. 7/8 are the opposite of the other 3. 

3 wheel trying to move forward and 1 wheel reverse. I don't think it will turn much. 
You should try both wheels on one side going forward while the other side reverse.

I got 3 of the wheels rotating in one direction, but the other one does not rotate at all.

As every wheel is separately driven, it is easy to believe you have 2 pcs of L298N or similar motor driver, but there is only one of this lines.

int EN1=5;
int SPEED = 200; // Needs to be a PWM pin to be able to control motor speed

Does this signal goes to all the enable pins on motor drivers?