Advertisement
pleasedontcode

"Conditional Stopping" rev_01

Jun 4th, 2024
449
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: "Conditional Stopping"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-06-04 09:27:23
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* move wiper motor using esp32 and motor driver */
  21.     /* bts7960, and when ultrasonic sensor detect */
  22.     /* something, turn the motor off */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Ultrasonic.h>    //https://github.com/ErickSimoes/Ultrasonic
  27. #include <BTS7960.h>       //https://github.com/luisllamasbinaburo/Arduino-BTS7960
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateOutputs(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t sensor_ultrasonik_HC_SR04_Echo_PIN_D13 = 13;
  36. const uint8_t sensor_ultrasonik_HC_SR04_Echo_PIN_D16 = 16;
  37.  
  38. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  39. const uint8_t sensor_ultrasonik_HC_SR04_Trigger_PIN_D4 = 4;
  40. const uint8_t sensor_ultrasonik_HC_SR04_Trigger_PIN_D14 = 14;
  41.  
  42. /***** DEFINITION OF MOTOR CONTROL PINS *****/
  43. const uint8_t motor_EN_PIN = 8;
  44. const uint8_t motor_L_PWM_PIN = 9;
  45. const uint8_t motor_R_PWM_PIN = 10;
  46.  
  47. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  48. /***** used to store raw data *****/
  49. bool sensor_ultrasonik_HC_SR04_Trigger_PIN_D4_rawData = 0;
  50. bool sensor_ultrasonik_HC_SR04_Trigger_PIN_D14_rawData = 0;
  51.  
  52. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  53. /***** used to store data after characteristic curve transformation *****/
  54. float sensor_ultrasonik_HC_SR04_Trigger_PIN_D4_phyData = 0.0;
  55. float sensor_ultrasonik_HC_SR04_Trigger_PIN_D14_phyData = 0.0;
  56.  
  57. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  58. Ultrasonic ultrasonic1(sensor_ultrasonik_HC_SR04_Trigger_PIN_D4, sensor_ultrasonik_HC_SR04_Echo_PIN_D13); // Initialize Ultrasonic object for first sensor
  59. Ultrasonic ultrasonic2(sensor_ultrasonik_HC_SR04_Trigger_PIN_D14, sensor_ultrasonik_HC_SR04_Echo_PIN_D16); // Initialize Ultrasonic object for second sensor
  60.  
  61. // Initialize BTS7960 motor controller object
  62. BTS7960 motorController(motor_EN_PIN, motor_L_PWM_PIN, motor_R_PWM_PIN);
  63.  
  64. void setup(void)
  65. {
  66.     // put your setup code here, to run once:
  67.     Serial.begin(9600); // Initialize serial communication
  68.  
  69.     pinMode(sensor_ultrasonik_HC_SR04_Echo_PIN_D13, INPUT);
  70.     pinMode(sensor_ultrasonik_HC_SR04_Echo_PIN_D16, INPUT);
  71.  
  72.     pinMode(sensor_ultrasonik_HC_SR04_Trigger_PIN_D4, OUTPUT);
  73.     pinMode(sensor_ultrasonik_HC_SR04_Trigger_PIN_D14, OUTPUT);
  74.  
  75.     // Enable the motor controller
  76.     motorController.Enable();
  77. }
  78.  
  79. void loop(void)
  80. {
  81.     // put your main code here, to run repeatedly:
  82.     updateOutputs(); // Refresh output data
  83.  
  84.     // Read distance from the first ultrasonic sensor
  85.     int distance1 = ultrasonic1.read();
  86.     Serial.print("Distance from sensor 1 in CM: ");
  87.     Serial.println(distance1);
  88.  
  89.     // Read distance from the second ultrasonic sensor
  90.     int distance2 = ultrasonic2.read();
  91.     Serial.print("Distance from sensor 2 in CM: ");
  92.     Serial.println(distance2);
  93.  
  94.     // Control the motor based on the distance from the ultrasonic sensors
  95.     if (distance1 < 20 || distance2 < 20) {
  96.         // If the distance from any sensor is less than 20 cm, stop the motor
  97.         motorController.Stop();
  98.     } else {
  99.         // Otherwise, turn the motor to the left
  100.         motorController.TurnLeft(100); // Example speed value
  101.     }
  102.  
  103.     delay(1000); // Wait for 1 second before the next reading
  104. }
  105.  
  106. void updateOutputs()
  107. {
  108.     digitalWrite(sensor_ultrasonik_HC_SR04_Trigger_PIN_D4, sensor_ultrasonik_HC_SR04_Trigger_PIN_D4_rawData);
  109.     digitalWrite(sensor_ultrasonik_HC_SR04_Trigger_PIN_D14, sensor_ultrasonik_HC_SR04_Trigger_PIN_D14_rawData);
  110. }
  111.  
  112. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement