RE: HS-04 sensor returning same value

hi, I’m having trouble using one of the HS-04 sensors in my project. I’m using ping_median() method to increase the accuracy when avoiding obstacles. I have one sensor at the front and one at the back. the front works perfectly fine however the back sensor seems to get ‘stuck’ and keeps returning the same value. I’m pretty sure it has something to do with the whole code rather than the sensors as one a separate sketch they work fine. I’m pretty new to this and can’t seem to work out a solution.

heres the code.

#include <Adafruit_MotorShield.h>

//declaring the pins for the IN pins on the L298N
const int rightForwardPin = 4;
const int rightBackwardPin = 2;
const int leftBackwardPin = 7;
const int leftForwardPin = 5;
int Speed = 255;
int Speed1 = 255;
unsigned int FrontDis;
unsigned int BackDis;
unsigned int median;
unsigned int median1;

bool Forward = true;

#include <NewPing.h>

#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 200

#define STOP_DISTANCE 15

#define TRIGGER_PIN1 9
#define ECHO_PIN1 8
#define MAX_DISTANCE1 200

NewPing sonar(12, 11, 200);
NewPing sonar1(9, 8, 200);

void setup (){

Serial.begin(115200);
//stating that the pins are OUTPUT
pinMode(9, OUTPUT);
pinMode(8, INPUT);
pinMode(12, OUTPUT);
pinMode(11, INPUT);
pinMode(rightForwardPin, OUTPUT);
pinMode(rightBackwardPin, OUTPUT);
pinMode(leftBackwardPin, OUTPUT);
pinMode(leftForwardPin, OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:

motion();
delay(100);

}

void motion(){

if(Forward == true){

if(FrontDis<=STOP_DISTANCE && FrontDis>5){
Forward = false;
backward();

}
else{

forward();

}
}

if(Forward == false){

if(BackDis<=STOP_DISTANCE && BackDis>5){
Forward = true;
forward();

}
else{

backward();

}
}

}

void forward(){
FrontSon();

analogWrite(leftBackwardPin, 0);
analogWrite(rightBackwardPin, 0);
analogWrite(leftForwardPin, 255);
analogWrite(rightForwardPin, 255);

if(STOP_DISTANCE<FrontDis && FrontDis<80){
Speed = map(FrontDis, STOP_DISTANCE, 80, 100, 255);
analogWrite(leftForwardPin, Speed);
analogWrite(rightForwardPin, Speed);
}

if(STOP_DISTANCE==FrontDis){

analogWrite(leftForwardPin, 50);
analogWrite(rightForwardPin, 50);

}

}
void backward(){
BackSon();

analogWrite(leftForwardPin, 0);
analogWrite(rightForwardPin, 0);
analogWrite(leftBackwardPin, 255);
analogWrite(rightBackwardPin, 255);

if(STOP_DISTANCE<BackDis && BackDis<80){
Speed1 = map(BackDis, STOP_DISTANCE, 80, 100, 255);
analogWrite(leftBackwardPin, Speed1);
analogWrite(rightBackwardPin, Speed1);
}

if(STOP_DISTANCE==BackDis){

analogWrite(leftBackwardPin, 50);
analogWrite(rightBackwardPin, 50);

}

}

void FrontSon(){

median = sonar.ping_median(5);
delay(50);
FrontDis = sonar.convert_cm(median);

Serial.print(“FrontDis:”);
Serial.print(FrontDis);
Serial.println(“cm”);
}
void BackSon(){

median1 = sonar1.ping_median(5);
delay(50);
BackDis = sonar1.convert_cm(median1);

Serial.print(“BackDis:”);
Serial.print(BackDis);
Serial.println(“cm”);
}

Why are median and median1 global variables? They are used ONLY in FrontSon() and BackSon() respectively.

Make them local variables in the appropriate functions.

sonar and sonar1 are dumb names, when you KNOW where the sensors are. Use names that make sense, like frontSonar and backSonar.

The delay()s in FrontSon() and BackSon() are completely useless. Get rid of them.

Modify loop() so that it does not call motion(), but, instead calls FrontSon() and BackSon(). If the sensors work without the motors working, then you have a hardware problem. If not, you have a software problem. Which problem are we trying to solve?

I've modified the code so that only the sensors are running in the loop. They both run fine without the back sensor returning the same value.

So I guess I have a hardware problem then...

So I guess I have a hardware problem then…

Something in motion() is causing problems. Call motion() again from loop(), after commenting out the calls to analogWrite() in forward() and backward().

If the sensors continue to read properly, you know that it is a motor issue. Uncomment one analogWrite() at a time, to determine which one causes the problem.

it seems to be the rightBackwardPin when I’m mapping the distance to pwm value.

if(STOP_DISTANCE<BackDis && BackDis<80){
Speed = map(BackDis, 80, STOP_DISTANCE, 255, 100);
analogWrite(rightBackwardPin, Speed);
analogWrite(leftBackwardPin, Speed);

any ideas on how to resolve this or should i just forget about trying to control the speed based on distance?

also thanks that provided a pretty quick way on debugging!!!

any ideas on how to resolve this

Can you use a different pin for the right backwards pin?

I've tried multiple different pins and still no difference.Could the issue be with the L298N Module as I'm running 4 motors off it rather than two?

Could the issue be with the L298N Module as I'm running 4 motors off it rather than two?

I wouldn't think so. The Arduino has no way of knowing that. It's strange that the problem is limited to one of the 4 pins that the module is connected to, and that it doesn't seem to matter which pin you use for the 4th pin.

You could always disconnect two of the motors, to see if anything changes, but I doubt that anything will.