Advertisement
pleasedontcode

Ultrasonic Setup rev_01

Jan 13th, 2024
76
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: Ultrasonic Setup
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-01-13 12:34:59
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* when any object will come in front of ultrasonic */
  21.     /* sensor then the car motor will stop automatically */
  22.     /* in this relay battery and arduino and ultrasonic */
  23.     /* sensor will use */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Arduino.h>
  28. #include <Ultrasonic.h>
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t ultrasonicdistance_HC_SR04_Echo_PIN_D3 = 3;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t ultrasonicdistance_HC_SR04_Trigger_PIN_D2 = 2;
  39.  
  40. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  41. Ultrasonic ultrasonic(ultrasonicdistance_HC_SR04_Trigger_PIN_D2, ultrasonicdistance_HC_SR04_Echo_PIN_D3);
  42.  
  43. void setup(void)
  44. {
  45.   // put your setup code here, to run once:
  46.  
  47.   // Set the ultrasonic echo pin as INPUT
  48.   pinMode(ultrasonicdistance_HC_SR04_Echo_PIN_D3, INPUT);
  49.  
  50.   // Set the ultrasonic trigger pin as OUTPUT
  51.   pinMode(ultrasonicdistance_HC_SR04_Trigger_PIN_D2, OUTPUT);
  52. }
  53.  
  54. void loop(void)
  55. {
  56.   // put your main code here, to run repeatedly:
  57.  
  58.   // Implement system requirement 1: Stop the car motor when an object is detected by the ultrasonic sensor
  59.   unsigned int distance = ultrasonic.read(); // Read the distance measured by the ultrasonic sensor
  60.   if (distance < 10) // If the distance is less than 10 cm (adjust this threshold according to your requirements)
  61.   {
  62.     // Stop the car motor (Add your code here to stop the motor)
  63.   }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement