Advertisement
pleasedontcode

Obstacle Avoidance rev_01

Feb 19th, 2024
85
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: Obstacle Avoidance
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-02-20 03:45:25
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The humanoid robot should be able to detect */
  21.     /* obstacles using the HC-SR04 ultrasonic sensor and */
  22.     /* avoid them by changing its path */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Ultrasonic.h>
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. void avoidObstacles();
  32.  
  33. /****** SYSTEM REQUIREMENTS *****/
  34. /****** SYSTEM REQUIREMENT 1 *****/
  35. /* The humanoid robot should be able to detect */
  36. /* obstacles using the HC-SR04 ultrasonic sensor and */
  37. /* avoid them by changing its path */
  38. /****** END SYSTEM REQUIREMENTS *****/
  39.  
  40. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  41. const uint8_t HC_SR04_Echo_Pin = 3;
  42.  
  43. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  44. const uint8_t HC_SR04_Trigger_Pin = 2;
  45.  
  46. /***** INSTANCE OF ULTRASONIC 1 *****/
  47. Ultrasonic ultrasonic1(HC_SR04_Echo_Pin, HC_SR04_Trigger_Pin);
  48.  
  49. void setup(void)
  50. {
  51.   // Put your setup code here, to run once:
  52.  
  53.   pinMode(HC_SR04_Echo_Pin, INPUT);
  54.   pinMode(HC_SR04_Trigger_Pin, OUTPUT);
  55. }
  56.  
  57. void loop(void)
  58. {
  59.   // Put your main code here, to run repeatedly:
  60.  
  61.   avoidObstacles();
  62. }
  63.  
  64. void avoidObstacles()
  65. {
  66.   // Read the distance measured by the ultrasonic sensor
  67.   unsigned int distanceCm = ultrasonic1.read();
  68.  
  69.   // Check if there is an obstacle within a certain range
  70.   if (distanceCm <= 10) {
  71.     // If obstacle detected, change robot's path to avoid it
  72.     // Add your code here for changing the path or avoiding the obstacle
  73.   }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement