Advertisement
pleasedontcode

"Distance Measurement" rev_01

Jul 6th, 2024
833
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 Measurement"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-07-06 10:33:59
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Develop an Arduino project using the Ultrasonic */
  21.     /* library to measure distance with the HC-SR04 */
  22.     /* sensor. The Echo pin is connected to D3 and the */
  23.     /* Trigger pin to D2. Implement functions to read */
  24.     /* sensor data and update output variables. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Ultrasonic.h>  //https://github.com/ErickSimoes/Ultrasonic
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void updateOutputs(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t HC_SR04_Echo_PIN_D3 = 3;
  37.  
  38. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  39. const uint8_t HC_SR04_Trigger_PIN_D2 = 2;
  40.  
  41. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  42. /***** used to store raw data *****/
  43. bool HC_SR04_Trigger_PIN_D2_rawData = 0;
  44.  
  45. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  46. /***** used to store data after characteristic curve transformation *****/
  47. float HC_SR04_Trigger_PIN_D2_phyData = 0.0;
  48.  
  49. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  50. Ultrasonic ultrasonic(HC_SR04_Trigger_PIN_D2, HC_SR04_Echo_PIN_D3); // Initialize Ultrasonic object
  51.  
  52. void setup(void)
  53. {
  54.   // put your setup code here, to run once:
  55.   Serial.begin(9600); // Initialize serial communication
  56.   pinMode(HC_SR04_Echo_PIN_D3, INPUT);
  57.   pinMode(HC_SR04_Trigger_PIN_D2, OUTPUT);
  58. }
  59.  
  60. void loop(void)
  61. {
  62.   // put your main code here, to run repeatedly:
  63.   updateOutputs(); // Refresh output data
  64.   delay(1000); // Delay to avoid flooding the serial monitor
  65. }
  66.  
  67. void updateOutputs()
  68. {
  69.   HC_SR04_Trigger_PIN_D2_phyData = ultrasonic.read(); // Read distance from ultrasonic sensor
  70.   Serial.print("Distance in CM: ");
  71.   Serial.println(HC_SR04_Trigger_PIN_D2_phyData); // Print distance to serial monitor
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement