Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Servo Scanner
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-11-19 11:24:55
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* i think my project is all good but i want to add */
- /* buzzer in it */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Servo.h>Β //https://github.com/arduino-libraries/Servo
- #include <Ultrasonic.h>Β //https://github.com/ErickSimoes/Ultrasonic
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t sonar_HC-SR04_Echo_PIN_D3 = 3;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t sonar_HC-SR04_Trigger_PIN_D2 = 2;
- /***** DEFINITION OF PWM OUTPUT PINS *****/
- const uint8_t servo_Servomotor_PWMSignal_PIN_D5 = 5;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- /****** USER_CODE *****/
- #include <Servo.h>
- const int trigPin = 8;
- const int echoPin = 9;
- // defining time and distancelong duration;
- int duration;
- int distance;
- Servo myServo; // Object servovoid setup() {
- pinMode(trigPin, OUTPUT); // trigPin as an Output
- pinMode(echoPin, INPUT); // echoPin as an Input
- Serial.begin(9600);
- myServo.attach(10); // Pin Connected To Servo
- }
- void loop() {
- // rotating servo i++ depicts increment of one degree
- for(int i=15;i<=165;i++){
- myServo.write(i);
- delay(30);
- distance = calculateDistance();
- Serial.print(i);
- Serial.print(",");
- Serial.print(distance);
- Serial.print(".");
- }
- // Repeats the previous lines from 165 to 15 degrees
- for(int i=165;i>15;i--){
- myServo.write(i);
- delay(30);
- distance = calculateDistance();
- Serial.print(i);
- Serial.print(",");
- Serial.print(distance);
- Serial.print(".");
- }
- }
- int calculateDistance(){
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- // Sets the trigPin on HIGH state for 10 micro seconds
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distance= duration*0.034/2;
- return distance;
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment