Go Down

Topic: 3 HC-SR04 sensors on bot acting like "bumpercar" (Read 708 times) previous topic - next topic

caswan

I have a 4wd bot we have been building.  we added 3 HC-SR04 sensors for automation and distance control but it does not stop and turn until the sensors touch the wall.  it prints to the serial monitor with accurate readings but once connected to motors its like there is a delay.  the only delays i have are for the sensors.
I am new to this so if someone can educate me i would appreciate any help. below is my void loop if you need more let me know. the "f,l,r" in each name is to identify front left right its not a spelling thing.

void loop() {
 
  digitalWrite(ftrigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(ftrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ftrigPin, LOW);
  delayMicroseconds(5);
  pingTimef = pulseIn(fechoPin, HIGH); 
  targetDistancef= pingTimer/74; 
  targetDistancef=targetDistancef/2;

   digitalWrite(ltrigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(ltrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ltrigPin, LOW);   
  delayMicroseconds(5);
  pingTimel = pulseIn(lechoPin, HIGH); 
  targetDistancel= pingTimel/74; 
  targetDistancel=targetDistancel/2;

  digitalWrite(rtrigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(rtrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(rtrigPin, LOW);
  delayMicroseconds(5);
  pingTimer = pulseIn(rechoPin, HIGH); 
  targetDistancer= pingTimer/74; 
  targetDistancer=targetDistancer/2;

if (targetDistancef >=4 && targetDistancel >= 3 && targetDistancer  >= 3 )
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
analogWrite(enablePinMotor1,153);
analogWrite(enablePinMotor2,153);
}
else if (targetDistancef <4 && targetDistancer < targetDistancel)
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
analogWrite(enablePinMotor1,200);
analogWrite(enablePinMotor2,200);
}

else if (targetDistancef <4 && targetDistancer > targetDistancel)
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
analogWrite(enablePinMotor1,200);
analogWrite(enablePinMotor2,200);
}

}
   

knut_ny

in your last 'else':

change ">"  to ">="
(or remove everyting behing 'else'.. which will perform equally)
Ny

caswan

thanks for the info. i tried your suggestion but still no joy. its acting like there is a timing issue between the sensors and motor output but i cant find it. here is the full code






int ftrigPin=3;
int fechoPin=2;
int ltrigPin=5;
int lechoPin=4;
int rtrigPin=7;
int rechoPin=6; 
float pingTimef; 
float targetDistancef;
float pingTimel;
float targetDistancel;
float pingTimer; 
float targetDistancer;
const int motorPin1 = A0;
const int motorPin2 = A1;
const int motorPin3 = A2;
const int motorPin4 = A3;
const int enablePinMotor1 = 10;
const int enablePinMotor2 = 11;


 
void setup() {
 
 
  Serial.begin(9600);
 
 
pinMode(ftrigPin, OUTPUT);
pinMode(fechoPin, INPUT);
pinMode(ltrigPin, OUTPUT);
pinMode(lechoPin, INPUT);
pinMode(rtrigPin, OUTPUT);
pinMode(rechoPin, INPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(enablePinMotor1, OUTPUT);
pinMode(enablePinMotor2, OUTPUT);
 
 
}
 
void loop() {
 
  digitalWrite(ftrigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(ftrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ftrigPin, LOW);
  delayMicroseconds(5);
  pingTimef = pulseIn(fechoPin, HIGH); 
  targetDistancef= pingTimer/74; 
  targetDistancef=targetDistancef/2;
 
   digitalWrite(ltrigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(ltrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ltrigPin, LOW);
  delayMicroseconds(5);
  pingTimel = pulseIn(lechoPin, HIGH); 
  targetDistancel= pingTimel/74; 
  targetDistancel=targetDistancel/2;
 
  digitalWrite(rtrigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(rtrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(rtrigPin, LOW);
  delayMicroseconds(5);
  pingTimer = pulseIn(rechoPin, HIGH); 
  targetDistancer= pingTimer/74;
  targetDistancer=targetDistancer/2;
 
 Serial.print("The Distance to Target 1 is: ");
  Serial.print(targetDistancef);
  Serial.println(" inch");
  delay(200);
 

  Serial.print("The Distance to Target 2 is: ");
  Serial.print(targetDistancel);
  Serial.println(" inch");
  delay(200);

  Serial.print("The Distance to Target 3 is: ");
  Serial.print(targetDistancer);
  Serial.println(" inch");
  delay(200);
 
if (targetDistancef >=4 && targetDistancel >= 3 && targetDistancer  >= 3 )
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
analogWrite(enablePinMotor1,200);
analogWrite(enablePinMotor2,200);


}

if (targetDistancef <=4 && targetDistancer < targetDistancel)
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
analogWrite(enablePinMotor1,200);
analogWrite(enablePinMotor2,200);

}

if (targetDistancef <4 && targetDistancer >= targetDistancel)
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
analogWrite(enablePinMotor1,200);
analogWrite(enablePinMotor2,200);

}

}

knut_ny

u've have cx delay(200)
get rid og them. Moving that fast - make .6 seconds a loooong time
Ny

caswan

thank again but i took the delays of serial print and that doesnt change the motor reaction or sensor delivery. still same issue.

caswan

thanks for your help.  turns out it is a timing issue i had to adjust distance to reaction time. works now!

Go Up