Hello ukhelibob thank for the reply i forgot to use the new code i am trying to make a robot that follows anything in front or behind it here is the new code
Your new code and old code are similar, with only pin changes, and the same result (zero).
I corrected your sketch and added comments. I think you should practice with just the HC-SR04 and learn how to interpret its output.
long ultrason_10() {
long duration, distance;
digitalWrite(10, LOW); // trigger pin
delayMicroseconds(2); // let signal settle
digitalWrite(10, HIGH); // start pulse
delayMicroseconds(10); // hold HIGH for 10us
digitalWrite(10, LOW); // end pulse
duration = pulseIn(11, HIGH); // read ECHO pin for pulse
distance = duration / 58; // calculate centimeters
return distance; // send results to calling function
}
void setup() {
Serial.begin(9600);
pinMode(10, OUTPUT); // TRIG ... Declare pinMode() only one time per pin
pinMode(11, INPUT); // ECHO
pinMode(2, OUTPUT); // IN1 - right motor, forward.
pinMode(3, OUTPUT); // IN2 - right motor, reverse.
pinMode(4, OUTPUT); // IN3 - left motor, forward.
pinMode(5, OUTPUT); // IN4 - left motor, reverse.
}
void loop() {
// Serial.println(ultrason_10()); // not how to read the distance result
// if (Serial.read() < 25) { // not how to read the distance result
if (ultrason_10() < 25) { // closer than 25cm, reverse motors
digitalWrite(2, 0); // right motor reverse
digitalWrite(3, 1);
digitalWrite(4, 0); // left motor reverse
digitalWrite(5, 1);
} else { // otherwise, forward motors
digitalWrite(2, 1); // right motor forward
digitalWrite(3, 0);
digitalWrite(4, 1); // left motor forward
digitalWrite(5, 0);
}
}