Advertisement
pleasedontcode

Distance Trigger rev_01

Apr 24th, 2024
44
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 Trigger
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-04-24 19:36:50
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* gives signal when the sensor senses there is */
  21.     /* something less then 5cm apart */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <HCSR04.h>  // Include the HCSR04 library
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30. void updateOutputs(void);  // Add function prototype for updateOutputs
  31.  
  32. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  33. const uint8_t us_HC_SR04_Echo_PIN_D3 = 3;
  34.  
  35. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  36. const uint8_t us_HC_SR04_Trigger_PIN_D2 = 2;
  37.  
  38. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  39. /***** used to store raw data *****/
  40. bool us_HC_SR04_Trigger_PIN_D2_rawData = 0;
  41.  
  42. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  43. /***** used to store data after characteristic curve transformation *****/
  44. float us_HC_SR04_Trigger_PIN_D2_phyData = 0.0;
  45.  
  46. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  47. UltraSonicDistanceSensor distanceSensor(us_HC_SR04_Trigger_PIN_D2, us_HC_SR04_Echo_PIN_D3);  // Initialize the sensor object
  48.  
  49. void setup(void)
  50. {
  51.   // put your setup code here, to run once:
  52.   pinMode(us_HC_SR04_Echo_PIN_D3, INPUT);
  53.   pinMode(us_HC_SR04_Trigger_PIN_D2, OUTPUT);
  54.   Serial.begin(9600);  // Initialize serial connection
  55. }
  56.  
  57. void loop(void)
  58. {
  59.   // put your main code here, to run repeatedly:
  60.   float distance = distanceSensor.measureDistanceCm();  // Measure distance using the sensor
  61.   if (distance < 5) {
  62.     updateOutputs();  // Call updateOutputs function if distance is less than 5cm
  63.   }
  64.   Serial.println(distance);  // Print the distance
  65.   delay(500);
  66. }
  67.  
  68. void updateOutputs()
  69. {
  70.   digitalWrite(us_HC_SR04_Trigger_PIN_D2, us_HC_SR04_Trigger_PIN_D2_rawData);
  71. }
  72.  
  73. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement