pleasedontcode

Servo Scanner rev_01

Nov 19th, 2025
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Servo Scanner
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-11-19 11:24:55
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* i think my project is all good but i want to add */
  21.     /* buzzer in it */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Servo.h>Β //https://github.com/arduino-libraries/Servo
  29. #include <Ultrasonic.h>Β //https://github.com/ErickSimoes/Ultrasonic
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t sonar_HC-SR04_Echo_PIN_D3 = 3;
  37.  
  38. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  39. const uint8_t sonar_HC-SR04_Trigger_PIN_D2  = 2;
  40.  
  41. /***** DEFINITION OF PWM OUTPUT PINS *****/
  42. const uint8_t servo_Servomotor_PWMSignal_PIN_D5 = 5;
  43.  
  44. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  45.  
  46. /****** USER_CODE *****/
  47. #include <Servo.h>
  48. const int trigPin = 8;
  49. const int echoPin = 9;
  50. // defining time and distancelong duration;
  51. int duration;
  52. int distance;
  53. Servo myServo; // Object servovoid setup() {
  54.   pinMode(trigPin, OUTPUT); // trigPin as an Output
  55.   pinMode(echoPin, INPUT); // echoPin as an Input
  56.   Serial.begin(9600);
  57.   myServo.attach(10); // Pin Connected To Servo
  58. }
  59.  
  60. void loop() {
  61.   // rotating servo i++ depicts increment of one degree
  62.   for(int i=15;i<=165;i++){
  63.     myServo.write(i);
  64.     delay(30);
  65.     distance = calculateDistance();
  66.     Serial.print(i);
  67.     Serial.print(",");
  68.     Serial.print(distance);
  69.     Serial.print(".");
  70.   }
  71.   // Repeats the previous lines from 165 to 15 degrees
  72.   for(int i=165;i>15;i--){
  73.     myServo.write(i);
  74.     delay(30);
  75.     distance = calculateDistance();
  76.     Serial.print(i);
  77.     Serial.print(",");
  78.     Serial.print(distance);
  79.     Serial.print(".");
  80.   }
  81. }
  82.  
  83. int calculateDistance(){
  84.   digitalWrite(trigPin, LOW);
  85.   delayMicroseconds(2);
  86.   // Sets the trigPin on HIGH state for 10 micro seconds
  87.   digitalWrite(trigPin, HIGH);
  88.   delayMicroseconds(10);
  89.   digitalWrite(trigPin, LOW);
  90.   duration = pulseIn(echoPin, HIGH);
  91.   distance= duration*0.034/2;
  92.   return distance;
  93. }
  94.  
  95. /* END CODE */
  96.  
Advertisement
Add Comment
Please, Sign In to add comment