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);
}
}