#include const int trigPin = 9; // Trig pin of the ultrasonic sensor const int echoPin = 10; // Echo pin of the ultrasonic sensor const int servoPin = 3; // Pin to which the servo is connected Servo myServo; // Create a servo object to control the servo motor void setup() { Serial.begin(9600); // Initialize serial communication for debugging myServo.attach(servoPin); // Attach the servo to its pin pinMode(trigPin, OUTPUT); // Set trig pin as output pinMode(echoPin, INPUT); // Set echo pin as input } void loop() { long duration, distance; // Triggering the ultrasonic sensor to measure distance digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Calculating the distance based on the time taken by the sound wave duration = pulseIn(echoPin, HIGH); distance = (duration / 2) / 29.1; // Divide by 29.1 or use your sensor's conversion factor Serial.print("Distance: "); Serial.println(distance); // Set the servo position based on the distance if (distance < 30) { // Adjust this value based on your setup myServo.write(90); // If an object is close, set servo to 90 degrees } else { myServo.write(0); // If no object is detected, set servo to 0 degrees } delay(100); // Add a small delay for stability }
Heattttt🔥
Slow by sure we will get to the peak of it all❤
❤❤❤
Proud of you son
Go higher
That’s my guys
#include
const int trigPin = 9; // Trig pin of the ultrasonic sensor
const int echoPin = 10; // Echo pin of the ultrasonic sensor
const int servoPin = 3; // Pin to which the servo is connected
Servo myServo; // Create a servo object to control the servo motor
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
myServo.attach(servoPin); // Attach the servo to its pin
pinMode(trigPin, OUTPUT); // Set trig pin as output
pinMode(echoPin, INPUT); // Set echo pin as input
}
void loop() {
long duration, distance;
// Triggering the ultrasonic sensor to measure distance
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Calculating the distance based on the time taken by the sound wave
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1; // Divide by 29.1 or use your sensor's conversion factor
Serial.print("Distance: ");
Serial.println(distance);
// Set the servo position based on the distance
if (distance < 30) { // Adjust this value based on your setup
myServo.write(90); // If an object is close, set servo to 90 degrees
} else {
myServo.write(0); // If no object is detected, set servo to 0 degrees
}
delay(100); // Add a small delay for stability
}