0% found this document useful (0 votes)
5 views

Cod Pet Feeder

This document contains an Arduino sketch that utilizes an ultrasonic sensor and a servo motor. It measures the distance using the ultrasonic sensor and maps that distance to an angle for the servo motor. The setup includes pin configurations, serial communication, and a loop for continuous distance measurement and servo adjustment.

Uploaded by

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

Cod Pet Feeder

This document contains an Arduino sketch that utilizes an ultrasonic sensor and a servo motor. It measures the distance using the ultrasonic sensor and maps that distance to an angle for the servo motor. The setup includes pin configurations, serial communication, and a loop for continuous distance measurement and servo adjustment.

Uploaded by

Luca Patrascu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <Servo.

h>

// Define the pins for the ultrasonic sensor

const int trigPin = 9;

const int echoPin = 8;

// Define the servo motor

Servo myServo;

// Variables for duration and distance

long duration;

int distance;

void setup() {

// Set up the pins for the ultrasonic sensor

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

// Attach the servo motor to pin 7

myServo.attach(7);

// Initialize serial communication

Serial.begin(9600);

}
void loop() {

// Send out a pulse to start the measurement

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Measure the duration of the echo pulse

duration = pulseIn(echoPin, HIGH);

// Calculate the distance in centimeters

distance = duration * 0.034 / 2;

// Print the distance to the Serial Monitor

Serial.print("Distance: ");

Serial.println(distance);

// Map the distance to a servo angle (0 to 180 degrees)

int angle = map(distance, 0, 100, 0, 180);

// Constrain the angle to stay within the servo's range

angle = constrain(angle, 0, 180);

You might also like