// C++ code
//
#include <Servo.h>
int distance = 0;
Servo servo_6;
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(9, OUTPUT);
servo_6.attach(6, 500, 2500);
pinMode(8, OUTPUT);
}
void loop()
{
digitalWrite(9, HIGH);
servo_6.write(90);
distance = 0.01723 * readUltrasonicDistance(7, 7);
if (distance <= 100) {
servo_6.write(180);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
delay(1000); // Wait for 1000 millisecond(s)
servo_6.write(90);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
}
servo_6.write(90);
}
Comments
Please log in or sign up to comment.