Advertisement
pleasedontcode

Ultrasonic LED Control rev_01

Mar 13th, 2024
79
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 LED Control
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-03-14 04:41:46
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Connect two esp8266 in espnow prototype .each */
  21.     /* esp8266 has its own ultrasonic sensor and led.if */
  22.     /* distance in ultrasonic sensor is less than 10cm */
  23.     /* then the led in another esp8266 has to glow */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateOutputs(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t HC_SR04_Echo_PIN_D14 = 14;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t Led_LED_PIN_D4 = 4;
  39. const uint8_t HC_SR04_Trigger_PIN_D13 = 13;
  40.  
  41. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  42. /***** used to store raw data *****/
  43. bool Led_LED_PIN_D4_rawData = 0;
  44. bool HC_SR04_Trigger_PIN_D13_rawData = 0;
  45.  
  46. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  47. /***** used to store data after characteristic curve transformation *****/
  48. float Led_LED_PIN_D4_phyData = 0.0;
  49. float HC_SR04_Trigger_PIN_D13_phyData = 0.0;
  50.  
  51. /****** DEFINITION OF LIBRARY CLASS INSTANCES*****/
  52. Ultrasonic ultrasonic(HC_SR04_Trigger_PIN_D13, HC_SR04_Echo_PIN_D14);
  53.  
  54. void setup(void)
  55. {
  56.     // put your setup code here, to run once:
  57.     pinMode(HC_SR04_Echo_PIN_D14, INPUT);
  58.  
  59.     pinMode(Led_LED_PIN_D4, OUTPUT);
  60.     pinMode(HC_SR04_Trigger_PIN_D13, OUTPUT);
  61.  
  62.     Serial.begin(9600); // Initialize serial communication
  63.  
  64. }
  65.  
  66. void loop(void)
  67. {
  68.     // put your main code here, to run repeatedly:
  69.     unsigned int distance = ultrasonic.read(); // Read the distance measured by the ultrasonic sensor
  70.  
  71.     if (distance < 10)
  72.     {
  73.         Led_LED_PIN_D4_rawData = 1; // Turn on the LED if distance is less than 10 cm
  74.     }
  75.     else
  76.     {
  77.         Led_LED_PIN_D4_rawData = 0; // Turn off the LED if distance is greater than or equal to 10 cm
  78.     }
  79.  
  80.     updateOutputs(); // Refresh output data
  81.  
  82.     Serial.print("Distance: ");
  83.     Serial.print(distance);
  84.     Serial.println(" cm");
  85.  
  86.     delay(1000); // Wait for 1 second before taking the next measurement
  87.  
  88. }
  89.  
  90. void updateOutputs()
  91. {
  92.     digitalWrite(Led_LED_PIN_D4, Led_LED_PIN_D4_rawData);
  93.     digitalWrite(HC_SR04_Trigger_PIN_D13, HC_SR04_Trigger_PIN_D13_rawData);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement