0% found this document useful (0 votes)
10 views1 page

Código

This C++ code defines a function to read ultrasonic distance using a trigger and echo pin. In the setup function, several pins are configured as outputs, and in the loop function, it checks the distance and controls the state of the output pins based on the measured distance. If the distance is less than 10 units, certain pins are set high or low to signal an event.

Uploaded by

Adam Michel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

Código

This C++ code defines a function to read ultrasonic distance using a trigger and echo pin. In the setup function, several pins are configured as outputs, and in the loop function, it checks the distance and controls the state of the output pins based on the measured distance. If the distance is less than 10 units, certain pins are set high or low to signal an event.

Uploaded by

Adam Michel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// C++ code

//
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(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
}

void loop()
{
if (0.01723 * readUltrasonicDistance(11, 10) < 10) {
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
} else {
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
}
delay(10); // Delay a little bit to improve simulation performance
}

You might also like