keeping right distance with the HC-SR04


hey guys,

i'm new forum , don't know if there allready thread problem this, see if guys can me out  :smiley-lol:

i'm trying make small vehicle move @ right distance object. set-up consists of a  hc-sr04 ultrasonic sensor, small dc-motor , h-bridge.

the problem have has code. underneeth can see short void loop i've got going right , i've marked problem is;
 
code: [select]
void loop() {
  digitalwrite(onoff, low);
  digitalwrite(trigpin, low);
  delaymicroseconds(2);
  digitalwrite(trigpin, high);
  delaymicroseconds(10);
  digitalwrite(trigpin, low);
  duration = pulsein(echopin, high);
  distance = (duration / 2) / 2.91;
  if (distance < 2000) {
    serial.print("distance: ");
    serial.println(distance);
  }
  else {
    serial.print("distance: ");
    serial.println("out of range");
  }

  if (100 <= distance <= 180) {                  // part can't figure out
    digitalwrite (directionl, low);
    digitalwrite (directionr, low);
    serial.print("motor off    ");
  }

  if (distance < 100) {
    digitalwrite (directionl, high);
    digitalwrite (directionr, low);
    serial.print("motor left  ");
  }

  if (distance > 180) {
    digitalwrite (directionl, low);
    digitalwrite (directionr, high);
    serial.print("motor right ");
  }

  delay(500);


}


my problem is, whenever distance lower or higher 100 or 180, program allways print "motor off   " because that's valid.

can guys tell me how tackle problem. i'm kinda new programming arduino , english isn't greatest. can use help!!

thanks allot , have night  :d

code: [select]
if (100 <= distance <= 180)this 1 comes once week.
i'm fed of explaining it.

use &&


Arduino Forum > Using Arduino > Programming Questions > keeping right distance with the HC-SR04


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts