Advertisement
pleasedontcode

"Arduino Ultrasonic Control" rev_02

Dec 28th, 2023
91
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: "Arduino Ultrasonic Control"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-28 23:28:52
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Si la distancia del sensor ultra1 es menor que 10 */
  21.     /* entonces coloque el servo1 en 90°, si no coloque */
  22.     /* el servo1 en 0° */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Arduino.h>
  27. #include <Ultrasonic.h>
  28. #include <Servo.h>
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t Ultra1_HC_SR04_Echo_PIN_D3 = 3;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t Ultra1_HC_SR04_Trigger_PIN_D2 = 2;
  39.  
  40. /***** DEFINITION OF PWM OUTPUT PINS *****/
  41. const uint8_t Servo1_Servomotor_PWMSignal_PIN_D5 = 5;
  42.  
  43. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  44. Ultrasonic ultrasonic(Ultra1_HC_SR04_Trigger_PIN_D2, Ultra1_HC_SR04_Echo_PIN_D3);
  45. Servo servo;
  46.  
  47. void setup(void)
  48. {
  49.   // put your setup code here, to run once:
  50.  
  51.   pinMode(Ultra1_HC_SR04_Echo_PIN_D3, INPUT);
  52.   pinMode(Ultra1_HC_SR04_Trigger_PIN_D2, OUTPUT);
  53.   pinMode(Servo1_Servomotor_PWMSignal_PIN_D5, OUTPUT);
  54.  
  55.   servo.attach(Servo1_Servomotor_PWMSignal_PIN_D5); // Attach the servo to the PWM pin
  56. }
  57.  
  58. void loop(void)
  59. {
  60.   // put your main code here, to run repeatedly:
  61.  
  62.   // Reading the distance from the ultrasonic sensor
  63.   unsigned int distance = ultrasonic.read();
  64.  
  65.   // Checking if the distance is less than 10
  66.   if (distance < 10)
  67.   {
  68.     // If the distance is less than 10, set the servo angle to 90 degrees
  69.     servo.write(90);
  70.   }
  71.   else
  72.   {
  73.     // If the distance is greater or equal to 10, set the servo angle to 0 degrees
  74.     servo.write(0);
  75.   }
  76.  
  77.   delay(100); // Add a delay for stability
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement