Cheap HC_SR04 scan function.

Hello. Working with a clone HC_SR04, I got it to work with out having the echoPin stuck on HIGH. Now implementing it into a new sketch. I am not sure as to why my obstacle avoiding bot will run full speed Forward and not turn or go Backwards. The code here is a little chopped up at the end Cause I have been Moving things around and working on bracket placement. Any constructive input would be swell Thanks.

//keyes l298 shield 1 hc_sr04
//defne pins
#include <NewPing.h>
#define trigPin 12
#define echoPin 11
#define MAX_DISTANCE 200



//Motor A
int enableA = 10;
int pinA1 = 3;
int pinA2 = 2;

//Motor B
int enableB = 9;
int pinB1 = 5;
int pinB2 = 4;
//speed
int pwm_speed = 255;
void setup() {
  Serial.begin (9600);
  //configure pin modes
  pinMode (enableA, OUTPUT);
  pinMode (pinA1, OUTPUT);
  pinMode (pinA2, OUTPUT);

  pinMode (enableB, OUTPUT);
  pinMode (pinB1, OUTPUT);
  pinMode (pinB2, OUTPUT);
}
NewPing sonar(trigPin, echoPin, MAX_DISTANCE);
unsigned int uS;
int triggerDistance = 15;
int distance;
int scan() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).

  Serial.print("Ping: ");
  Serial.print(sonar.convert_cm(uS)); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");

  if (uS == 0 && digitalRead(echoPin) == HIGH) {
    pinMode(echoPin, OUTPUT);
    digitalWrite(echoPin, LOW);
    delay(100);
    pinMode(echoPin, INPUT);
  }



}
//motor function
void Forward() {
  digitalWrite (pinA1, HIGH);
  digitalWrite (pinA2, LOW);
  digitalWrite (pinB1, HIGH);
  digitalWrite (pinB2, LOW);
  analogWrite (enableA, pwm_speed);
  analogWrite (enableB, pwm_speed);
}
void Backwards() {
  digitalWrite (pinA1, LOW);
  digitalWrite (pinA2, HIGH);
  digitalWrite (pinB1, LOW);
  digitalWrite (pinB2, HIGH);
  analogWrite (enableA, pwm_speed);
  analogWrite (enableB, pwm_speed);
}
void Left() {
  digitalWrite (pinA1, HIGH);
  digitalWrite (pinA2, LOW);
  digitalWrite (pinB1, LOW);
  digitalWrite (pinB2, HIGH);
  analogWrite (enableA, pwm_speed);
  analogWrite (enableB, pwm_speed);
}
void Right() {
  digitalWrite (pinA1, LOW);
  digitalWrite (pinA2, HIGH);
  digitalWrite (pinB1, HIGH);
  digitalWrite (pinB2, LOW);
  analogWrite (enableA, pwm_speed);
  analogWrite (enableB, pwm_speed);
}
void Stop() {
  digitalWrite (pinA1, LOW);
  digitalWrite (pinA2, LOW);
  digitalWrite (pinB1, LOW);
  digitalWrite (pinB2, LOW);
  analogWrite (enableA, pwm_speed);
  analogWrite (enableB, pwm_speed);
}
void loop() {
  // test
  /*
   Forward();
   Serial.println ("Forward");
   delay(1000);
   Backwards();
   Serial.println("Backwards");
   delay(1000);
   Right();
   Serial.println ("Right");
   delay(1000);
  /////////////////////////////////
  */
  distance = scan();
  if (distance >= triggerDistance) {
    Backwards();
    delay(700);
    Serial.println("Backwards");
    Right();
    delay(300);
    Serial.println("Right");
    Stop();
    Serial.println("Stopping");

    distance = scan();
    if ( distance > triggerDistance) {
      Forward();
      Serial.println("Forward");


    }
    else {
      Forward();
      Serial.println("Forward");

    }
  }
}

You are only acting if there is no obstacle in sight,
I think you reversed the logic (at least in the first check),
creating a hit and run bot. :wink:

Outside of a typo with that Greater Than Less Than mix up, with them switched now it just goes Backwards and Right then Forward for a split second. new code

//keyes l298 shield 1 hc_sr04
//defne pins
#include <NewPing.h>
#define trigPin 12
#define echoPin 11
#define MAX_DISTANCE 100



//Motor A
int enableA = 10;
int pinA1 = 3;
int pinA2 = 2;

//Motor B
int enableB = 9;
int pinB1 = 5;
int pinB2 = 4;
//speed
int pwm_speed = 200;
void setup() {
  Serial.begin (9600);
  //configure pin modes
  pinMode (enableA, OUTPUT);
  pinMode (pinA1, OUTPUT);
  pinMode (pinA2, OUTPUT);

  pinMode (enableB, OUTPUT);
  pinMode (pinB1, OUTPUT);
  pinMode (pinB2, OUTPUT);
}
NewPing sonar(trigPin, echoPin, MAX_DISTANCE);
unsigned int uS;
int triggerDistance = 15;
int distance;
int scan() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).

  Serial.print("Ping: ");
  Serial.print(sonar.convert_cm(uS)); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");

  if (uS == 0 && digitalRead(echoPin) == HIGH) {
    pinMode(echoPin, OUTPUT);
    digitalWrite(echoPin, LOW);
    delay(100);
    pinMode(echoPin, INPUT);
  }



}
//motor function
void Forward() {
  digitalWrite (pinA1, HIGH);
  digitalWrite (pinA2, LOW);
  digitalWrite (pinB1, HIGH);
  digitalWrite (pinB2, LOW);
  analogWrite (enableA, pwm_speed);
  analogWrite (enableB, pwm_speed);
}
void Backwards() {
  digitalWrite (pinA1, LOW);
  digitalWrite (pinA2, HIGH);
  digitalWrite (pinB1, LOW);
  digitalWrite (pinB2, HIGH);
  analogWrite (enableA, pwm_speed);
  analogWrite (enableB, pwm_speed);
}
void Left() {
  digitalWrite (pinA1, HIGH);
  digitalWrite (pinA2, LOW);
  digitalWrite (pinB1, LOW);
  digitalWrite (pinB2, HIGH);
  analogWrite (enableA, pwm_speed);
  analogWrite (enableB, pwm_speed);
}
void Right() {
  digitalWrite (pinA1, LOW);
  digitalWrite (pinA2, HIGH);
  digitalWrite (pinB1, HIGH);
  digitalWrite (pinB2, LOW);
  analogWrite (enableA, pwm_speed);
  analogWrite (enableB, pwm_speed);
}
void Stop() {
  digitalWrite (pinA1, LOW);
  digitalWrite (pinA2, LOW);
  digitalWrite (pinB1, LOW);
  digitalWrite (pinB2, LOW);
  analogWrite (enableA, pwm_speed);
  analogWrite (enableB, pwm_speed);
}
void loop() {
  // test
  /*
   Forward();
   Serial.println ("Forward");
   delay(1000);
   Backwards();
   Serial.println("Backwards");
   delay(1000);
   Right();
   Serial.println ("Right");
   delay(1000);
  /////////////////////////////////
  */
  distance = scan();
  if (distance <= triggerDistance) {
    Serial.println("Backwards");
    Backwards();
    delay(500);
    distance = scan();
   Right();
      Serial.println("Right");
   delay(500);
   Stop();
   delay(1000);
   if (distance > triggerDistance){
     Forward();
     Serial.println ("Forward");
   }
   else {
     Forward();
     Serial.println ("Forward");
   }
  }
  else {
    Forward();
    Serial.println ("Forward");
  }
}

I know it has to be a flow issue but I can not pin point it. logically I think this should work just fine. Thanks again.

That If clause needs a brushup

    if (distance > triggerDistance) {
      Forward();
      Serial.println ("Forward");
    }
    else {
      Forward();
      Serial.println ("Forward");
    }

Checking in with update to my progress. I have made some changes to the code, now the little guy will roll Forward. However finishing the if claus with an else to only stop, It will not stop. with (distance == triggerDistance) will not Stop only go Forward. When (distance <= triggerDistance) it goes Forward then Stops then Forward over and over. I am beginning to think that the clone HC_SR04 sensor is the root of the problem. Notice the if statement to get it to go Forward had to make the distance == 0 this is causing a problem with Stopping at triggerDistance or less, Thank you for input on the code any further help is appreciated. Using these sensors would be nice but I feel I may have to toss them. code here.

//keyes l298 shield 1 hc_sr04
//defne pins
#include <NewPing.h>
#define trigPin 12
#define echoPin 11
#define MAX_DISTANCE 100



//Motor A
int enableA = 10;
int pinA1 = 3;
int pinA2 = 2;

//Motor B
int enableB = 9;
int pinB1 = 5;
int pinB2 = 4;
//speed
int pwm_speed = 255;
void setup() {
  Serial.begin (9600);
  //configure pin modes
  pinMode (enableA, OUTPUT);
  pinMode (pinA1, OUTPUT);
  pinMode (pinA2, OUTPUT);

  pinMode (enableB, OUTPUT);
  pinMode (pinB1, OUTPUT);
  pinMode (pinB2, OUTPUT);
}
NewPing sonar(trigPin, echoPin, MAX_DISTANCE);
unsigned int uS;
int triggerDistance = 30;
int distance;
int scan() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).

  Serial.print("Ping: ");
  Serial.print(sonar.convert_cm(uS)); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");

  if (uS == 0 && digitalRead(echoPin) == HIGH) {
    pinMode(echoPin, OUTPUT);
    digitalWrite(echoPin, LOW);
    delay(100);
    pinMode(echoPin, INPUT);
  }



}
//motor function
void Forward() {
  digitalWrite (pinA1, HIGH);
  digitalWrite (pinA2, LOW);
  digitalWrite (pinB1, HIGH);
  digitalWrite (pinB2, LOW);
  analogWrite (enableA, pwm_speed);
  analogWrite (enableB, pwm_speed);
}
void Backwards() {
  digitalWrite (pinA1, LOW);
  digitalWrite (pinA2, HIGH);
  digitalWrite (pinB1, LOW);
  digitalWrite (pinB2, HIGH);
  analogWrite (enableA, pwm_speed);
  analogWrite (enableB, pwm_speed);
}
void Left() {
  digitalWrite (pinA1, HIGH);
  digitalWrite (pinA2, LOW);
  digitalWrite (pinB1, LOW);
  digitalWrite (pinB2, HIGH);
  analogWrite (enableA, pwm_speed);
  analogWrite (enableB, pwm_speed);
}
void Right() {
  digitalWrite (pinA1, LOW);
  digitalWrite (pinA2, HIGH);
  digitalWrite (pinB1, HIGH);
  digitalWrite (pinB2, LOW);
  analogWrite (enableA, pwm_speed);
  analogWrite (enableB, pwm_speed);
}
void Stop() {
  digitalWrite (pinA1, LOW);
  digitalWrite (pinA2, LOW);
  digitalWrite (pinB1, LOW);
  digitalWrite (pinB2, LOW);
  analogWrite (enableA, pwm_speed);
  analogWrite (enableB, pwm_speed);
}
void loop() {
  // test
  /*
   Forward();
   Serial.println ("Forward");
   delay(1000);
   Backwards();
   Serial.println("Backwards");
   delay(1000);
   Right();
   Serial.println ("Right");
   delay(1000);
}*/
  /////////////////////////////////
  
  delay(50);
  
   distance = scan();
  if (distance > triggerDistance || distance == 0){
   Forward();
   Serial.println("Forward");
  } 
  else {
    (distance == triggerDistance);
    Stop();
    Serial.println("Stopping");
  }
}