Advertisement
pleasedontcode

Distance Measurement rev_01

Sep 4th, 2024
109
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-09-04 07:14:55
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* void setup() {    // put your setup code her */
  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 Sensor_HC_SR04_Echo_PIN_D6 = 6;
  32.  
  33. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  34. const uint8_t Red_LEDRGB_Red_PIN_D2 = 2;
  35. const uint8_t Red_LEDRGB_Green_PIN_D3 = 3;
  36. const uint8_t Buzzer_ActiveBuzzer_output_PIN_D4 = 4;
  37. const uint8_t Sensor_HC_SR04_Trigger_PIN_D5 = 5;
  38.  
  39. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  40. /***** used to store raw data *****/
  41. bool Red_LEDRGB_Red_PIN_D2_rawData = 0;
  42. bool Red_LEDRGB_Green_PIN_D3_rawData = 0;
  43. bool Buzzer_ActiveBuzzer_output_PIN_D4_rawData = 0;
  44. bool Sensor_HC_SR04_Trigger_PIN_D5_rawData = 0;
  45.  
  46. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  47. /***** used to store data after characteristic curve transformation *****/
  48. float Red_LEDRGB_Red_PIN_D2_phyData = 0.0;
  49. float Red_LEDRGB_Green_PIN_D3_phyData = 0.0;
  50. float Buzzer_ActiveBuzzer_output_PIN_D4_phyData = 0.0;
  51. float Sensor_HC_SR04_Trigger_PIN_D5_phyData = 0.0;
  52.  
  53. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  54. // Create an Ultrasonic object
  55. Ultrasonic ultrasonic(Sensor_HC_SR04_Trigger_PIN_D5, Sensor_HC_SR04_Echo_PIN_D6); // Initialize the ultrasonic object
  56.  
  57. void setup(void)
  58. {    
  59.     // Initialize serial communication
  60.     Serial.begin(9600); // Start serial communication at 9600 baud rate
  61.  
  62.     // Set pin modes for input and output
  63.     pinMode(Sensor_HC_SR04_Echo_PIN_D6, INPUT);
  64.     pinMode(Red_LEDRGB_Red_PIN_D2, OUTPUT);
  65.     pinMode(Red_LEDRGB_Green_PIN_D3, OUTPUT);
  66.     pinMode(Buzzer_ActiveBuzzer_output_PIN_D4, OUTPUT);
  67.     pinMode(Sensor_HC_SR04_Trigger_PIN_D5, OUTPUT);
  68.  
  69.     // Attach the ultrasonic sensor to the specified pins
  70.     // This is done through the constructor of the Ultrasonic object
  71. }
  72.  
  73. void loop(void)
  74. {
  75.     // Main code to run repeatedly
  76.     updateOutputs(); // Refresh output data
  77.  
  78.     // Read the distance from the ultrasonic sensor
  79.     int distance = ultrasonic.read(); // Get distance in centimeters
  80.  
  81.     // Print the distance to the Serial Monitor
  82.     Serial.print("Distance in CM: ");
  83.     Serial.println(distance);
  84.  
  85.     delay(1000); // Wait for 1 second before the next reading
  86. }
  87.  
  88. void updateOutputs()
  89. {
  90.     // Update the output pins based on the raw data
  91.     digitalWrite(Red_LEDRGB_Red_PIN_D2, Red_LEDRGB_Red_PIN_D2_rawData);
  92.     digitalWrite(Red_LEDRGB_Green_PIN_D3, Red_LEDRGB_Green_PIN_D3_rawData);
  93.     digitalWrite(Buzzer_ActiveBuzzer_output_PIN_D4, Buzzer_ActiveBuzzer_output_PIN_D4_rawData);
  94.     digitalWrite(Sensor_HC_SR04_Trigger_PIN_D5, Sensor_HC_SR04_Trigger_PIN_D5_rawData);
  95. }
  96.  
  97. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement