eduardoviktor0
Published © CC0

Sensor de distancia

fácil 5componentes

BeginnerFull instructions provided1,774
Sensor de distancia

Things used in this project

Hardware components

Enclosure for Arduino Uno
Enclosure for Arduino Uno
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
5 mm LED: Green
5 mm LED: Green
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Custom parts and enclosures

switchcase2_ALT1yc68oN.ino

Schematics

imagens_-_atalho_uonTqFQmWw.lnk

Code

Novo Documento de Texto.txt

Arduino
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(8, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(1, OUTPUT);
}

void loop()
{
  if (0.01723 * readUltrasonicDistance(0, 0) == 30) {
    digitalWrite(8, HIGH);
  } else {
    if (0.01723 * readUltrasonicDistance(0, 0) == 20) {
      digitalWrite(5, HIGH);
    } else {
      if (0.01723 * readUltrasonicDistance(0, 0) == 10) {
        digitalWrite(1, HIGH);
      }
    }
  }
  delay(10); // Delay a little bit to improve simulation performance
}

Credits

manual do mundo

Posted by eduardoviktor0

Comments