Advertisement
pleasedontcode

Scanner rev_177

Nov 20th, 2023
57
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: Scanner
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-11-20 12:15:13
  15.     - Source Code generated by: AlexWind
  16.  
  17. ********* Pleasedontcode.com **********/
  18. /****** DEFINITION OF LIBRARIES *****/
  19. #include <Arduino.h>
  20. #include <Servo.h>
  21. #include "Ultrasonic-distance-sensor-easyC-SOLDERED.h"
  22.  
  23. /****** SYSTEM REQUIREMENT 1 *****/
  24. /* when something in front ultrasonic sensor senses */
  25. /* it and then the servo motor rotates and moves to */
  26. /* space where no obstacle is present */
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31.  
  32. /***** DEFINITION OF PWM OUTPUT PINS *****/
  33. const uint8_t servo_Servomotor_PWMSignal_PIN = 3;
  34. const uint8_t ultrasonic_TRIG_PIN = 2;
  35. const uint8_t ultrasonic_ECHO_PIN = 4;
  36.  
  37. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  38. Servo myservo; // Instance of Servo class
  39. Ultrasonic_Sensor ultrasonic(ultrasonic_TRIG_PIN, ultrasonic_ECHO_PIN); // Instance of Ultrasonic_Sensor class
  40.  
  41. void setup(void)
  42. {
  43.   // put your setup code here, to run once:
  44.  
  45.   pinMode(servo_Servomotor_PWMSignal_PIN, OUTPUT);
  46.  
  47.   myservo.attach(servo_Servomotor_PWMSignal_PIN); // Attaching the servo to the corresponding pin
  48.  
  49.   ultrasonic.begin(); // Initializing the ultrasonic sensor
  50.  
  51. }
  52.  
  53. void loop(void)
  54. {
  55.   // put your main code here, to run repeatedly:
  56.  
  57.   // Check if obstacle is detected by ultrasonic sensor
  58.   if (ultrasonic.takeMeasure() == 0) // Change Ultrasonic_Sensor::MEASURE_SUCCESS to 0
  59.   {
  60.     // Get the distance measured by the ultrasonic sensor
  61.     uint16_t distance = ultrasonic.getDistance();
  62.  
  63.     // If an obstacle is detected within a certain range
  64.     if (distance < 50)
  65.     {
  66.       // Rotate the servo to a position where no obstacle is present
  67.       myservo.write(90); // Example value, adjust according to your setup
  68.     }
  69.   }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement