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
}
manual do mundo
Posted by
eduardoviktor0













Comments