safwan092

project_0533607317

Oct 20th, 2022
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. const int trigPin = 10;
  4. const int echoPin = 11;
  5. const int cameratrigger = A0;
  6. const int buzzer = 12;
  7. long duration;
  8. int distance;
  9.  
  10. Servo myServo;
  11.  
  12. void setup() {
  13. pinMode(trigPin, OUTPUT);
  14. pinMode(echoPin, INPUT);
  15. pinMode(cameratrigger, OUTPUT);
  16. pinMode(buzzer, OUTPUT);
  17. Serial.begin(9600);
  18. myServo.attach(9); // Defines on which pin is the servo motor attached
  19. }
  20. void loop() {
  21. // rotates the servo motor from 15 to 165 degrees
  22. for (int i = 15; i <= 165; i++) {
  23. myServo.write(i);
  24. delay(30);
  25. distance = calculateDistance();// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
  26. if (distance < 30) {
  27. digitalWrite(cameratrigger, HIGH);
  28. digitalWrite(buzzer, HIGH);
  29. delay(200);
  30. digitalWrite(cameratrigger, LOW);
  31. digitalWrite(buzzer, LOW);
  32. delay(200);
  33. }
  34. Serial.print(i); // Sends the current degree into the Serial Port
  35. Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  36. Serial.print(distance); // Sends the distance value into the Serial Port
  37. Serial.print("."); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  38. }
  39. // Repeats the previous lines from 165 to 15 degrees
  40. for (int i = 165; i > 15; i--) {
  41. myServo.write(i);
  42. delay(30);
  43. distance = calculateDistance();
  44. if (distance < 30) {
  45. digitalWrite(cameratrigger, HIGH);
  46. digitalWrite(buzzer, HIGH);
  47. delay(200);
  48. digitalWrite(cameratrigger, LOW);
  49. digitalWrite(buzzer, LOW);
  50. delay(200);
  51. }
  52. Serial.print(i);
  53. Serial.print(",");
  54. Serial.print(distance);
  55. Serial.print(".");
  56. }
  57. }
  58.  
  59. int calculateDistance() {
  60. digitalWrite(trigPin, LOW);
  61. delayMicroseconds(2);
  62. digitalWrite(trigPin, HIGH);
  63. delayMicroseconds(10);
  64. digitalWrite(trigPin, LOW);
  65. duration = pulseIn(echoPin, HIGH);
  66. distance = duration * 0.034 / 2;
  67. return distance;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment