Advertisement
pleasedontcode

"Ultrasonic Control" rev_01

Jan 1st, 2024
96
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 Control"
  13.     - Source Code compiled for: Arduino Mega
  14.     - Source Code created on: 2024-01-02 02:01:05
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Measure la distance à droite et à gauche */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Arduino.h>
  25. #include <Ultrasonic.h>
  26. #include <Servo.h>
  27.  
  28. /********** FUNCTION PROTOTYPES **********/
  29. void setup(void);
  30. void loop(void);
  31.  
  32. /********** DEFINITION OF DIGITAL INPUT PINS **********/
  33. const uint8_t Ultrasonic_HCSR04_Echo_PIN_RIGHT = 3;
  34. const uint8_t Ultrasonic_HCSR04_Echo_PIN_LEFT = 4;
  35.  
  36. /********** DEFINITION OF DIGITAL OUTPUT PINS **********/
  37. const uint8_t Ultrasonic_HCSR04_Trigger_PIN = 2;
  38.  
  39. /********** DEFINITION OF PWM OUTPUT PINS **********/
  40. const uint8_t Servo_Servomotor_PWMSignal_PIN = 5;
  41.  
  42. /********** DEFINITION OF LIBRARY CLASS INSTANCES **********/
  43. Ultrasonic ultrasonicRight(Ultrasonic_HCSR04_Echo_PIN_RIGHT);
  44. Ultrasonic ultrasonicLeft(Ultrasonic_HCSR04_Echo_PIN_LEFT);
  45. Servo servoMotor;
  46.  
  47. void setup(void)
  48. {
  49.   // put your setup code here, to run once:
  50.   pinMode(Ultrasonic_HCSR04_Echo_PIN_RIGHT, INPUT);
  51.   pinMode(Ultrasonic_HCSR04_Echo_PIN_LEFT, INPUT);
  52.   pinMode(Ultrasonic_HCSR04_Trigger_PIN, OUTPUT);
  53.   pinMode(Servo_Servomotor_PWMSignal_PIN, OUTPUT);
  54.  
  55.   servoMotor.attach(Servo_Servomotor_PWMSignal_PIN);
  56.  
  57.   Serial.begin(9600);
  58. }
  59.  
  60. void loop(void)
  61. {
  62.   // Measure the distance to the right
  63.   unsigned int distanceRight = ultrasonicRight.read();
  64.   Serial.print("Distance to the right: ");
  65.   Serial.print(distanceRight);
  66.   Serial.println(" cm");
  67.  
  68.   // Measure the distance to the left
  69.   unsigned int distanceLeft = ultrasonicLeft.read();
  70.   Serial.print("Distance to the left: ");
  71.   Serial.print(distanceLeft);
  72.   Serial.println(" cm");
  73.  
  74.   delay(1000);
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement