Advertisement
pleasedontcode

Distance Servo rev_01

Feb 13th, 2024
102
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: Distance Servo
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-02-14 01:10:05
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* the servo motor is underneath the ultrasonic and */
  21.     /* rotates 90 degrees. Ultrasonic measures the */
  22.     /* distance to an obstacle */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Ultrasonic.h>    // https://github.com/ErickSimoes/Ultrasonic
  27. #include <Servo.h>         // https://github.com/arduino-libraries/Servo
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup();
  31. void loop();
  32. void updateOutputs();
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t ultrasonic_Echo_PIN = 3;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t ultrasonic_Trigger_PIN = 2;
  39.  
  40. /***** DEFINITION OF PWM OUTPUT PINS *****/
  41. const uint8_t servo_PWMSignal_PIN = 5;
  42.  
  43. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  44. bool ultrasonic_Trigger_PIN_rawData = false;
  45. uint8_t servo_PWMSignal_PIN_rawData = 90; // Rotate at 90 degrees default
  46.  
  47. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  48. float ultrasonic_Trigger_PIN_phyData = 0.0;
  49. float servo_PWMSignal_PIN_phyData = 0.0;
  50.  
  51. /****** DEFINITION OF LIBRARY CLASS INSTANCES *****/
  52. Ultrasonic ultrasonic(ultrasonic_Trigger_PIN, ultrasonic_Echo_PIN);
  53. Servo myservo;
  54.  
  55. void setup()
  56. {
  57.   // put your setup code here, to run once:
  58.   pinMode(ultrasonic_Echo_PIN, INPUT);
  59.   pinMode(ultrasonic_Trigger_PIN, OUTPUT);
  60.   pinMode(servo_PWMSignal_PIN, OUTPUT);
  61.  
  62.   myservo.attach(servo_PWMSignal_PIN);
  63. }
  64.  
  65. void loop()
  66. {
  67.   // put your main code here, to run repeatedly:
  68.   ultrasonic_Trigger_PIN_rawData = true;
  69.   delayMicroseconds(10);
  70.   ultrasonic_Trigger_PIN_rawData = false;
  71.  
  72.   unsigned int distance_cm = ultrasonic.read();
  73.  
  74.   if (distance_cm > 0 && distance_cm < 100)
  75.   {
  76.     int angle = map(distance_cm, 0, 100, 0, 180);
  77.     myservo.write(angle);
  78.   }
  79.  
  80.   delay(200);
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement