Advertisement
pleasedontcode

Human Detection rev_01

Apr 1st, 2024
101
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: Human Detection
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-04-01 21:42:32
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* activates a relay when the presence of a human is */
  21.     /* detected */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Ultrasonic.h>  // Include the Ultrasonic library
  26. #include <Wire.h>       // Include the Wire library for relay control
  27.  
  28. /****** SYSTEM REQUIREMENTS *****/
  29. /****** SYSTEM REQUIREMENT 1 *****/
  30. /* activates a relay when the presence of a human is */
  31. /* detected */
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void activateRelay(void);
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t ultrasonicEchoPin = 3;   // Define the Echo pin
  40.  
  41. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  42. const uint8_t ultrasonicTriggerPin = 2;   // Define the Trigger pin
  43. const uint8_t relayPin = 4;                // Define the relay pin
  44.  
  45. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  46. /***** used to store raw data *****/
  47. bool ultrasonicTriggerPinRawData = 0;   // Define variable to store raw data
  48.  
  49. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  50. /***** used to store data after characteristic curve transformation *****/
  51. float ultrasonicTriggerPinPhyData = 0.0;   // Define variable to store transformed data
  52.  
  53. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  54. Ultrasonic ultrasonic(ultrasonicTriggerPin, ultrasonicEchoPin);   // Create an Ultrasonic object
  55.  
  56. void setup(void)
  57. {
  58.   // put your setup code here, to run once:
  59.  
  60.   pinMode(ultrasonicEchoPin, INPUT);   // Set Echo pin as input
  61.  
  62.   pinMode(ultrasonicTriggerPin, OUTPUT);   // Set Trigger pin as output
  63.  
  64.   pinMode(relayPin, OUTPUT);   // Set relay pin as output
  65.  
  66.   Serial.begin(9600);   // Start the serial communication
  67.  
  68. }
  69.  
  70. void loop(void)
  71. {
  72.   // put your main code here, to run repeatedly:
  73.  
  74.   int distance = ultrasonic.read();   // Read the distance in centimeters
  75.  
  76.   Serial.print("Distance in CM: ");
  77.   Serial.println(distance);
  78.  
  79.   if (distance < 50)   // Check if the distance is less than 50cm
  80.   {
  81.     activateRelay();   // Call the function to activate the relay
  82.   }
  83.   else
  84.   {
  85.     digitalWrite(relayPin, LOW);   // Turn off the relay
  86.   }
  87.  
  88.   delay(1000);   // Wait for 1 second
  89. }
  90.  
  91. void activateRelay(void)
  92. {
  93.   digitalWrite(relayPin, HIGH);   // Turn on the relay
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement