Advertisement
pleasedontcode

"Distance Relay" rev_01

Jan 16th, 2024
97
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 Relay"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-01-17 01:42:13
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Relay module, */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Arduino.h>
  25. #include <Ultrasonic.h>
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30.  
  31. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  32. const uint8_t Ultrasonic_HC_SR04_Echo_PIN_D3 = 3;
  33.  
  34. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  35. const uint8_t Ultrasonic_HC_SR04_Trigger_PIN_D2 = 2;
  36.  
  37. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  38. Ultrasonic ultrasonic(Ultrasonic_HC_SR04_Trigger_PIN_D2, Ultrasonic_HC_SR04_Echo_PIN_D3);
  39.  
  40. /****** SYSTEM REQUIREMENTS *****/
  41. /****** SYSTEM REQUIREMENT 1 *****/
  42. /* Relay module */
  43. const uint8_t Relay_PIN_D4 = 4;
  44.  
  45. /****** END SYSTEM REQUIREMENTS *****/
  46.  
  47. void setup(void)
  48. {
  49.   // put your setup code here, to run once:
  50.   pinMode(Ultrasonic_HC_SR04_Echo_PIN_D3, INPUT);
  51.   pinMode(Ultrasonic_HC_SR04_Trigger_PIN_D2, OUTPUT);
  52.  
  53.   pinMode(Relay_PIN_D4, OUTPUT);
  54. }
  55.  
  56. void loop(void)
  57. {
  58.   // put your main code here, to run repeatedly:
  59.  
  60.   // Read distance from ultrasonic sensor
  61.   unsigned int distance = ultrasonic.read();
  62.  
  63.   // Check if distance is less than 30 cm
  64.   if(distance < 30)
  65.   {
  66.     // Activate relay module
  67.     digitalWrite(Relay_PIN_D4, HIGH);
  68.   }
  69.   else
  70.   {
  71.     // Deactivate relay module
  72.     digitalWrite(Relay_PIN_D4, LOW);
  73.   }
  74.  
  75.   delay(1000);
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement