Advertisement
pleasedontcode

**Distance Communication** rev_01

May 21st, 2025
158
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 Communication**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-05-21 17:46:04
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* generate code for reading values of ultrasonic */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <SPI.h>
  27. #include <LoRa.h>   //https://github.com/sandeepmistry/arduino-LoRa
  28. #include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void updateOutputs();
  34. void readUltrasonicDistance(); // New function prototype
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t piyush_SX127x_DIO0_PIN_D3     = 3;
  38. const uint8_t piyush_HC-SR04_Echo_PIN_D5        = 5;
  39.  
  40. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  41. const uint8_t piyush_SX127x_NRESET_PIN_D2       = 2;
  42. const uint8_t piyush_HC-SR04_Trigger_PIN_D4     = 4;
  43.  
  44. /***** DEFINITION OF SPI PINS *****/
  45. const uint8_t piyush_SX127x_SPI_PIN_MOSI_D11        = 11;
  46. const uint8_t piyush_SX127x_SPI_PIN_MISO_D12        = 12;
  47. const uint8_t piyush_SX127x_SPI_PIN_SCLK_D13        = 13;
  48. const uint8_t piyush_SX127x_SPI_PIN_CS_D10      = 10;
  49.  
  50. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  51. /***** used to store raw data *****/
  52. bool    piyush_SX127x_NRESET_PIN_D2_rawData     = 0;
  53. bool    piyush_HC-SR04_Trigger_PIN_D4_rawData       = 0;
  54.  
  55. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  56. /***** used to store data after characteristic curve transformation *****/
  57. float   piyush_SX127x_NRESET_PIN_D2_phyData     = 0.0;
  58. float   piyush_HC-SR04_Trigger_PIN_D4_phyData       = 0.0;
  59. float ultrasonicDistance = 0.0; // Variable to store ultrasonic distance
  60.  
  61. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  62. // Create an instance of the Ultrasonic class
  63. Ultrasonic ultrasonic(piyush_HC-SR04_Trigger_PIN_D4, piyush_HC-SR04_Echo_PIN_D5);
  64.  
  65. void setup(void)
  66. {
  67.     // put your setup code here, to run once:
  68.  
  69.     pinMode(piyush_SX127x_DIO0_PIN_D3,  INPUT);
  70.     pinMode(piyush_HC-SR04_Echo_PIN_D5, INPUT);
  71.  
  72.     pinMode(piyush_SX127x_NRESET_PIN_D2,     OUTPUT);
  73.     pinMode(piyush_HC-SR04_Trigger_PIN_D4,   OUTPUT);
  74.  
  75.     pinMode(piyush_SX127x_SPI_PIN_CS_D10,    OUTPUT);
  76.     // start the SPI library:
  77.     SPI.begin();
  78. }
  79.  
  80. void loop(void)
  81. {
  82.     // put your main code here, to run repeatedly:
  83.  
  84.     updateOutputs(); // Refresh output data
  85.     readUltrasonicDistance(); // Read distance from ultrasonic sensor
  86. }
  87.  
  88. void updateOutputs()
  89. {
  90.     digitalWrite(piyush_SX127x_NRESET_PIN_D2, piyush_SX127x_NRESET_PIN_D2_rawData);
  91.     digitalWrite(piyush_HC-SR04_Trigger_PIN_D4, piyush_HC-SR04_Trigger_PIN_D4_rawData);
  92. }
  93.  
  94. void readUltrasonicDistance() {
  95.     // Read the distance from the ultrasonic sensor
  96.     ultrasonicDistance = ultrasonic.read(); // Get distance in cm
  97.     // You can add code here to process or display the distance
  98. }
  99.  
  100. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement