4716

the value of ultrasonic sensor will be printed in led

code:
// C++ code
//
int ultrasonicsensor = 0;

long readUltrasonicDistance(int triggerPin, int echoPin)
{
  pinMode(triggerPin, OUTPUT);  // Clear the trigger
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echoPin, HIGH);
}

void setup()
{
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
}

void loop()
{
  ultrasonicsensor = 0.01723 * readUltrasonicDistance(A0, A0);
  if (ultrasonicsensor > 4 && ultrasonicsensor < 6) {
    digitalWrite(0, HIGH);
  } else {
    digitalWrite(0, LOW);
  }
  if (ultrasonicsensor > 7 && ultrasonicsensor < 12) {
    digitalWrite(1, HIGH);
  } else {
    digitalWrite(1, LOW);
  }
  if (ultrasonicsensor > 13 && ultrasonicsensor < 17) {
    digitalWrite(3, HIGH);
  } else {
    digitalWrite(3, LOW);
  }
  if (ultrasonicsensor > 18 && ultrasonicsensor < 25) {
    digitalWrite(4, HIGH);
  } else {
    digitalWrite(4, LOW);
  }
  if (ultrasonicsensor > 26 && ultrasonicsensor < 32) {
    digitalWrite(5, HIGH);
  } else {
    digitalWrite(5, LOW);
  }
  if (ultrasonicsensor > 33 && ultrasonicsensor < 49) {
    digitalWrite(6, HIGH);
  } else {
    digitalWrite(6, LOW);
  }
  if (ultrasonicsensor > 50 && ultrasonicsensor < 100) {
    digitalWrite(7, HIGH);
  } else {
    digitalWrite(7, LOW);
  }
  delay(10); // Delay a little bit to improve simulation performance
}
by mohamed hashir.