Advertisement
pleasedontcode

Distance Control rev_01

Oct 21st, 2024
84
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: Distance Control
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-10-21 11:09:18
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* led turn on when object is near */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic
  25.  
  26. /****** FUNCTION PROTOTYPES *****/
  27. void setup(void);
  28. void loop(void);
  29.  
  30. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  31. const uint8_t USSENSOR1_HC-SR04_Echo_PIN_D3     = 3;
  32. const uint8_t USSENSOR1_HC-SR04_Echo_PIN_D5     = 5;
  33.  
  34. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  35. const uint8_t USSENSOR1_HC-SR04_Trigger_PIN_D2      = 2;
  36. const uint8_t USSENSOR1_HC-SR04_Trigger_PIN_D4      = 4;
  37. const uint8_t led1_LED_PIN_D6       = 6;
  38.  
  39. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  40. /***** used to store raw data *****/
  41. bool    USSENSOR1_HC-SR04_Trigger_PIN_D2_rawData        = 0;
  42. bool    USSENSOR1_HC-SR04_Trigger_PIN_D4_rawData        = 0;
  43. bool    led1_LED_PIN_D6_rawData     = 0;
  44.  
  45. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  46. /***** used to store data after characteristic curve transformation *****/
  47. float   USSENSOR1_HC-SR04_Trigger_PIN_D2_phyData        = 0.0;
  48. float   USSENSOR1_HC-SR04_Trigger_PIN_D4_phyData        = 0.0;
  49. float   led1_LED_PIN_D6_phyData     = 0.0;
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  52. // Initialize the Ultrasonic sensor object with Trigger and Echo pins
  53. Ultrasonic ultrasonic1(USSENSOR1_HC-SR04_Trigger_PIN_D2, USSENSOR1_HC-SR04_Echo_PIN_D3);
  54. Ultrasonic ultrasonic2(USSENSOR1_HC-SR04_Trigger_PIN_D4, USSENSOR1_HC-SR04_Echo_PIN_D5);
  55.  
  56. void setup(void)
  57. {
  58.     // put your setup code here, to run once:
  59.  
  60.     pinMode(USSENSOR1_HC-SR04_Echo_PIN_D3,  INPUT);
  61.     pinMode(USSENSOR1_HC-SR04_Echo_PIN_D5,  INPUT);
  62.  
  63.     pinMode(USSENSOR1_HC-SR04_Trigger_PIN_D2,    OUTPUT);
  64.     pinMode(USSENSOR1_HC-SR04_Trigger_PIN_D4,    OUTPUT);
  65.     pinMode(led1_LED_PIN_D6,     OUTPUT);
  66.    
  67.     Serial.begin(9600); // Initialize serial communication for debugging
  68. }
  69.  
  70. void loop(void)
  71. {
  72.     // put your main code here, to run repeatedly:
  73.  
  74.     updateOutputs(); // Refresh output data
  75.  
  76.     // Read distances from the ultrasonic sensors
  77.     int distance1 = ultrasonic1.read(); // Read distance from the first sensor
  78.     int distance2 = ultrasonic2.read(); // Read distance from the second sensor
  79.    
  80.     // Print the distances to the Serial Monitor
  81.     Serial.print("Distance from Sensor 1: ");
  82.     Serial.print(distance1);
  83.     Serial.print(" cm, Distance from Sensor 2: ");
  84.     Serial.println(distance2);
  85.    
  86.     // Check if the distance is less than a certain threshold (e.g., 10 cm)
  87.     if (distance1 < 10 || distance2 < 10) {
  88.         led1_LED_PIN_D6_rawData = HIGH; // Turn on the LED when an object is near
  89.     } else {
  90.         led1_LED_PIN_D6_rawData = LOW; // Turn off the LED
  91.     }
  92.    
  93.     delay(1000); // Wait for a second before the next reading
  94. }
  95.  
  96. void updateOutputs()
  97. {
  98.     digitalWrite(USSENSOR1_HC-SR04_Trigger_PIN_D2, USSENSOR1_HC-SR04_Trigger_PIN_D2_rawData);
  99.     digitalWrite(USSENSOR1_HC-SR04_Trigger_PIN_D4, USSENSOR1_HC-SR04_Trigger_PIN_D4_rawData);
  100.     digitalWrite(led1_LED_PIN_D6, led1_LED_PIN_D6_rawData); // Update LED state
  101. }
  102.  
  103. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement