Code doesnt do what it should do

hi guys,
so i just made obstacle avoiding robot using one ultrasonic sensor and two H bridges(l289n), and two dc motors and the arduino uno.But the program i wrote wouldn't do what i want.I want to turn when it sees a object ten cm way, however, what it does do is that only the right dc motor turn and when there is something infront of it both turn. help please!

#define echoPin 6
#define trigPin 7

int maximumRange = 200;
long duration, distance;
void setup() {

Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);

}

void loop() {

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

distance = duration/58.2;

Serial.println(distance);
if(distance>=10)
{
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
}
else
{
digitalWrite(9, HIGH);
digitalWrite(8, LOW);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
}
}

this is my code. help !!

Without knowing what is connected and how, it is impossible to help. It would help a little if the pins had names instead of numbers.

I notice that pin 12 always gets written LOW and never HIGH. Is this intentional?

I also noticed that the turning (when it happens) will only happen until the distance is greater than or equal to 10 cm. Is that intentional?

Darned if I know why duration and distance are declared as long. duration should be unsigned long. Because distance is declared as long, it can NEVER have a fraction. A distance of 12.9 cm will be shown as 12 cm.

What does your Serial.println(...) tell you? More Serial.println(...) inside the if/else might help as well.

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your code.
  2. Paste the autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a photograph of the wiring.

Good Luck!

His code is slightly modified from the Article "Arduino HC-SR04 (Ultrasonic sensor)" at -

http://blog.whatgeek.com.pt/arduino/arduino-hc-sr04-ultrasonic-sensor/

… except, as you noted, he's attempting to to do something on the unnamed pins 8, 9, 11 and 12.

Nice find!

I see that the original code had a delay(100) in it. In general, I hate delay(...) and prefer to use millis() and an FSM, but I hope that the sensor can operate without it.

I also see that the original code converted time to distance using a divisor of 58, while the code from the OP uses a divisor of 58.2

In any case, I hope that the OP understands the differences between integer arithmetic and floating point arithmetic, but it doesn't affect the current question.