Advertisement
pleasedontcode

"Sensory Servo" rev_01

Feb 22nd, 2024
88
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: "Sensory Servo"
  13.     - Source Code compiled for: Arduino Mega
  14.     - Source Code created on: 2024-02-22 20:45:17
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* via the first sensor distance readings less than */
  21.     /* 20 cm move the servo to the angle 10 if no */
  22.     /* readings at 20 cm move the servo to 90 angle and */
  23.     /* if the second sensor reads an object less than 20 */
  24.     /* cm far move the servo to the angle 170 */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic
  29. #include <Servo.h>     //https://github.com/arduino-libraries/Servo
  30.  
  31. /****** SYSTEM REQUIREMENTS *****/
  32. /****** SYSTEM REQUIREMENT 1 *****/
  33. /* via the first sensor distance readings less than */
  34. /* 20 cm move the servo to the angle 10 if no */
  35. /* readings at 20 cm move the servo to 90 angle and */
  36. /* if the second sensor reads an object less than 20 */
  37. /* cm far move the servo to the angle 170 */
  38. /****** END SYSTEM REQUIREMENTS *****/
  39.  
  40. /****** FUNCTION PROTOTYPES *****/
  41. void setup(void);
  42. void loop(void);
  43. void updateOutputs(void);
  44.  
  45. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  46. const uint8_t s1_HC_SR04_Echo_PIN_D3 = 3;
  47. const uint8_t s2_HC_SR04_Echo_PIN_D6 = 6;
  48.  
  49. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  50. const uint8_t s1_HC_SR04_Trigger_PIN_D2 = 2;
  51. const uint8_t s2_HC_SR04_Trigger_PIN_D5 = 5;
  52.  
  53. /***** DEFINITION OF PWM OUTPUT PINS *****/
  54. const uint8_t servo_Servomotor_PWMSignal_PIN_D4 = 4;
  55.  
  56. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  57. /***** used to store raw data *****/
  58. bool s1_HC_SR04_Trigger_PIN_D2_rawData = 0;
  59. bool s2_HC_SR04_Trigger_PIN_D5_rawData = 0;
  60. uint8_t servo_Servomotor_PWMSignal_PIN_D4_rawData = 0;
  61.  
  62. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  63. /***** used to store data after characteristic curve transformation *****/
  64. float s1_HC_SR04_Trigger_PIN_D2_phyData = 0.0;
  65. float s2_HC_SR04_Trigger_PIN_D5_phyData = 0.0;
  66. float servo_Servomotor_PWMSignal_PIN_D4_phyData = 0.0;
  67.  
  68. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  69. Ultrasonic ultrasonic1(s1_HC_SR04_Trigger_PIN_D2, s1_HC_SR04_Echo_PIN_D3);
  70. Ultrasonic ultrasonic2(s2_HC_SR04_Trigger_PIN_D5, s2_HC_SR04_Echo_PIN_D6);
  71. Servo servo;
  72.  
  73. void setup(void)
  74. {
  75.   // put your setup code here, to run once:
  76.  
  77.   pinMode(s1_HC_SR04_Echo_PIN_D3, INPUT);
  78.   pinMode(s2_HC_SR04_Echo_PIN_D6, INPUT);
  79.  
  80.   pinMode(s1_HC_SR04_Trigger_PIN_D2, OUTPUT);
  81.   pinMode(s2_HC_SR04_Trigger_PIN_D5, OUTPUT);
  82.   pinMode(servo_Servomotor_PWMSignal_PIN_D4, OUTPUT);
  83.  
  84.   servo.attach(servo_Servomotor_PWMSignal_PIN_D4);
  85. }
  86.  
  87. void loop(void)
  88. {
  89.   // put your main code here, to run repeatedly:
  90.  
  91.   // Read distances from ultrasonic sensors
  92.   unsigned int distance1 = ultrasonic1.read();
  93.   unsigned int distance2 = ultrasonic2.read();
  94.  
  95.   // System Requirement 1: Move the servo based on distance readings
  96.   if (distance1 < 20)
  97.   {
  98.     servo.write(10); // Angle 10
  99.   }
  100.   else if (distance1 >= 20 && distance1 < 40)
  101.   {
  102.     servo.write(90); // Angle 90
  103.   }
  104.  
  105.   if (distance2 < 20)
  106.   {
  107.     servo.write(170); // Angle 170
  108.   }
  109.  
  110.   delay(100); // Add delay if needed
  111. }
  112.  
  113. void updateOutputs()
  114. {
  115.   digitalWrite(s1_HC_SR04_Trigger_PIN_D2, s1_HC_SR04_Trigger_PIN_D2_rawData);
  116.   digitalWrite(s2_HC_SR04_Trigger_PIN_D5, s2_HC_SR04_Trigger_PIN_D5_rawData);
  117.   servo.write(servo_Servomotor_PWMSignal_PIN_D4_rawData);
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement