Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Support us on : https://global-prog.com
- #include <Ultrasonic.h> // Ultrasonic Library
- #include <Servo.h> // Servo Library
- Servo servo;
- Ultrasonic ultrasonic(4,5); // Ultrasonic object for the Ultrasonic library
- int pos; // Variable to save the servo position
- int distance; // Variable to save the distance from Ultrasonic sensor
- int buzzer = 12; // Define the buzzer pin to pin 12
- void setup()
- {
- Serial.begin(9600); // Begin the serial monitor
- servo.attach(3); // Attach servo pin
- pinMode(buzzer, OUTPUT); // Make a buzzer pin as output
- }
- void loop()
- {
- for(pos=0;pos<=180;pos++){ // Loop to go from 0 to 180 degrees
- servo.write(pos); // Write the position to the servo
- distance=ultrasonic.read(); // Read the distance from the ultrasonic sensor
- Serial.print(pos+" , "); // Write the position to the serial monitor
- Serial.println(distance+" CM"); // Write the distance to the serial monitor
- if(distance<=100){ // Buzzer function when the distance less than 100cm
- digital.write(buzzer,HIGH); // Turn the buzzer on
- }
- else{
- digital.write(buzzer,LOW); // Turn the buzzer off
- }
- delay(50); // Wait for 50 milliseconds
- }
- for(pos=180;pos>=0;pos--){ // Loop to go from 180 to 0 degrees
- servo.write(pos); // Write the position to the servo
- distance=ultrasonic.read(); // Read the distance from the ultrasonic sensor
- Serial.print(pos+" ْ , "); // Write the position to the serial monitor
- Serial.println(distance+" CM"); // Write the distance to the serial monitor
- if(distance<=100){ // Buzzer function when the distance less than 100cm
- digital.write(buzzer,HIGH); // Turn the buzzer on
- }
- else{
- digital.write(buzzer,LOW); // Turn the buzzer off
- }
- delay(50); // Wait for 50 milliseconds
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement