Advertisement
pleasedontcode

Ultrasonic Buzzer rev_19

Dec 19th, 2023
105
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: Ultrasonic Buzzer
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-19 22:33:27
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* if distance is less than 20cm so start to beep the */
  21.     /* buzzer. */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Arduino.h>
  26. #include <Ultrasonic.h>
  27.  
  28. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  29. const uint8_t frontSensor_HC_SR04_Echo_PIN_D4 = 4;
  30.  
  31. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  32. const uint8_t alarm_ActiveBuzzer_output_PIN_D2 = 2;
  33. const uint8_t frontSensor_HC_SR04_Trigger_PIN_D3 = 3;
  34.  
  35. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  36. Ultrasonic ultrasonic(frontSensor_HC_SR04_Trigger_PIN_D3, frontSensor_HC_SR04_Echo_PIN_D4);
  37.  
  38. void setup(void)
  39. {
  40.   // put your setup code here, to run once:
  41.   pinMode(frontSensor_HC_SR04_Echo_PIN_D4, INPUT);
  42.   pinMode(alarm_ActiveBuzzer_output_PIN_D2, OUTPUT);
  43.   pinMode(frontSensor_HC_SR04_Trigger_PIN_D3, OUTPUT);
  44. }
  45.  
  46. void loop(void)
  47. {
  48.   // put your main code here, to run repeatedly:
  49.  
  50.   // Read the distance measured by the ultrasonic sensor in centimeters
  51.   unsigned int distance = ultrasonic.read();
  52.  
  53.   // Check if the distance is less than 20cm
  54.   if (distance < 20) {
  55.     // Activate the buzzer
  56.     digitalWrite(alarm_ActiveBuzzer_output_PIN_D2, HIGH);
  57.   } else {
  58.     // Deactivate the buzzer
  59.     digitalWrite(alarm_ActiveBuzzer_output_PIN_D2, LOW);
  60.   }
  61.  
  62.   // Delay for a short time before taking the next measurement
  63.   delay(100);
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement