// Define pin numbers
#include <Servo.h> // Include the Servo library
Servo servo;
int motor = 12;
int motor2 = 11;
const int trigPin = 9; // Trigger pin connected to digital pin 9
const int echoPin = 10; // Echo pin connected to digital pin 10
void setup() {
// put your setup code here, to run once:
pinMode(motor, OUTPUT);
pinMode(motor2, OUTPUT);
Serial.begin(9600);
servo.attach(3);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
// Map the potentiometer value to the servo angle range (0 to 180 degrees)
// Write the mapped angle to the servo
// Add a small delay to stabilize the movement
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the pulse on the echo pin
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters (speed of sound = 34300 cm/s)
int distance = duration * 0.034 / 2;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Add a short delay
delay(100);
servo.write(90);
}