Advertisement
pleasedontcode

**Servo Control** rev_01

Nov 9th, 2024
81
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: **Servo Control**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-11-09 06:36:25
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* smart trash bin using esp32 and ultrasonic sensor */
  21.     /* and servo motor */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  28. #include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic
  29. #include <Servo.h>       // Include Servo library for controlling servo motors
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t servomotor_PushButton_PIN_D4      = 4;
  37. const uint8_t servomotor_HC-SR04_Echo_PIN_D14       = 14;
  38.  
  39. // Define pins for HC-SR04 sensor based on your setup
  40. const int trigPin = 19;  // Trigger pin connected to GPIO 19
  41. const int echoPin = 18;  // Echo pin connected to GPIO 18
  42.  
  43. // Define the pin for the servo motor
  44. const int servoPin = 27;  // Servo signal pin connected to GPIO 27
  45.  
  46. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  47. const uint8_t servomotor_HC-SR04_Trigger_PIN_D13        = 13;
  48.  
  49. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  50. /***** used to store raw data *****/
  51. bool    servomotor_HC-SR04_Trigger_PIN_D13_rawData      = 0;
  52.  
  53. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  54. /***** used to store data after characteristic curve transformation *****/
  55. float   servomotor_HC-SR04_Trigger_PIN_D13_phyData      = 0.0;
  56.  
  57. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  58. // Create a Servo object
  59. Servo myServo;
  60.  
  61. // Create an Ultrasonic object
  62. Ultrasonic ultrasonic(trigPin, echoPin);
  63.  
  64. // Variables to store the pulse duration and distance calculated
  65. long duration;
  66. float distance;
  67.  
  68. void setup(void)
  69. {
  70.     // put your setup code here, to run once:
  71.  
  72.     pinMode(servomotor_PushButton_PIN_D4,   INPUT_PULLUP);
  73.     pinMode(servomotor_HC-SR04_Echo_PIN_D14,    INPUT);
  74.  
  75.     pinMode(servomotor_HC-SR04_Trigger_PIN_D13,  OUTPUT);
  76.  
  77.     // Initialize serial communication at a baud rate of 115200
  78.     Serial.begin(115200);
  79.  
  80.     // Attach the servo on GPIO 27
  81.     myServo.attach(servoPin);
  82.  
  83.     // Set initial servo position
  84.     myServo.write(0);  // Start at 0 degrees (initial position)
  85.     delay(1000); // Wait for servo to move to 0 degrees
  86.     Serial.println("Servo initialized and set to 0 degrees");
  87. }
  88.  
  89. void loop(void)
  90. {
  91.     // put your main code here, to run repeatedly:
  92.  
  93.     updateOutputs(); // Refresh output data
  94.  
  95.     // Get the distance from the ultrasonic sensor
  96.     distance = ultrasonic.read(); // Read distance in cm
  97.  
  98.     // Check if the distance is within the sensor's range (2 cm to 400 cm)
  99.     if (distance > 400 || distance < 2) {
  100.         Serial.println("Out of range");
  101.     } else {
  102.         // Display the distance in the Serial Monitor
  103.         Serial.print("Distance: ");
  104.         Serial.print(distance);
  105.         Serial.println(" cm");
  106.  
  107.         // Check if the distance is less than 10 cm (object very close)
  108.         if (distance < 10) {
  109.             Serial.println("Object very close, moving servo to 180 degrees");
  110.             myServo.write(180);  // Move servo to 180 degrees
  111.             delay(1000);          // Wait for 1 second to allow the servo to move
  112.         }
  113.         // Check if the distance is between 10-50 cm (object in medium range)
  114.         else if (distance < 50) {
  115.             Serial.println("Object in medium range, moving servo to 90 degrees");
  116.             myServo.write(90);   // Move servo to 90 degrees
  117.             delay(1000);          // Wait for 1 second to allow the servo to move
  118.         }
  119.         // If the object is far (greater than 50 cm)
  120.         else {
  121.             Serial.println("Object far away, moving servo to 0 degrees");
  122.             myServo.write(0);    // Move servo to 0 degrees
  123.             delay(1000);          // Wait for 1 second to allow the servo to move
  124.         }
  125.     }
  126.  
  127.     // Wait 1 second before taking the next measurement
  128.     delay(1000);
  129. }
  130.  
  131. void updateOutputs()
  132. {
  133.     digitalWrite(servomotor_HC-SR04_Trigger_PIN_D13, servomotor_HC-SR04_Trigger_PIN_D13_rawData);
  134. }
  135.  
  136. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement