Advertisement
pleasedontcode

Arduino Sensor rev_01

Feb 14th, 2024
76
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: Arduino Sensor
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-02-14 14:57:44
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* As a user, I want to be able to measure distance */
  21.     /* using the HC-SR04 ultrasonic sensor and display */
  22.     /* the results on an LCD screen. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Wire.h>        // Add the Wire library for I2C communication
  27. #include <LiquidCrystal_I2C.h>  // Add the LiquidCrystal_I2C library for LCD display
  28. #include <Ultrasonic.h>  // Add the Ultrasonic library for distance measurement
  29. #include <DHT.h>         // Add the DHT library for temperature and humidity measurement
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35. void displayData(float distance, float temperature, float humidity);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t DistanceBuddy_HC_SR04_Echo_PIN = 14;
  39. const uint8_t DHT11_DHT11_DOUT_PIN = 4;
  40.  
  41. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  42. const uint8_t DistanceBuddy_HC_SR04_Trigger_PIN = 13;
  43. const uint8_t HelloRGB_LEDRGB_Red_PIN = 19;
  44. const uint8_t HelloRGB_LEDRGB_Green_PIN = 20;
  45.  
  46. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  47. bool DistanceBuddy_HC_SR04_Trigger_PIN_rawData = 0;
  48. bool HelloRGB_LEDRGB_Red_PIN_rawData = 0;
  49. bool HelloRGB_LEDRGB_Green_PIN_rawData = 0;
  50.  
  51. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  52. float DistanceBuddy_HC_SR04_Trigger_PIN_phyData = 0.0;
  53. float HelloRGB_LEDRGB_Red_PIN_phyData = 0.0;
  54. float HelloRGB_LEDRGB_Green_PIN_phyData = 0.0;
  55.  
  56. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  57. Ultrasonic ultrasonic(DistanceBuddy_HC_SR04_Trigger_PIN, DistanceBuddy_HC_SR04_Echo_PIN); // Create an instance of the Ultrasonic class
  58. DHT dht(DHT11_DHT11_DOUT_PIN, DHT11); // Create an instance of the DHT class
  59. LiquidCrystal_I2C lcd(0x27, 16, 2);  // Create an instance of the LiquidCrystal_I2C class for LCD display
  60.  
  61. void setup(void)
  62. {
  63.   // put your setup code here, to run once:
  64.   pinMode(DistanceBuddy_HC_SR04_Echo_PIN, INPUT);
  65.   pinMode(DistanceBuddy_HC_SR04_Trigger_PIN, OUTPUT);
  66.   pinMode(HelloRGB_LEDRGB_Red_PIN, OUTPUT);
  67.   pinMode(HelloRGB_LEDRGB_Green_PIN, OUTPUT);
  68.  
  69.   ultrasonic.setTimeout(2000); // Set the timeout for Ultrasonic sensor
  70.   dht.begin(); // Initialize the DHT sensor
  71.   lcd.begin(16, 2);  // Initialize the LCD display
  72.   lcd.backlight();  // Turn on the LCD backlight
  73. }
  74.  
  75. void loop(void)
  76. {
  77.   // put your main code here, to run repeatedly:
  78.   updateOutputs(); // Refresh output data
  79.  
  80.   float distance = ultrasonic.read(); // Read distance from Ultrasonic sensor
  81.   float temperature = dht.readTemperature(); // Read temperature from DHT sensor
  82.   float humidity = dht.readHumidity(); // Read humidity from DHT sensor
  83.  
  84.   displayData(distance, temperature, humidity); // Display the data on the LCD
  85. }
  86.  
  87. void updateOutputs()
  88. {
  89.   digitalWrite(DistanceBuddy_HC_SR04_Trigger_PIN, DistanceBuddy_HC_SR04_Trigger_PIN_rawData);
  90.   digitalWrite(HelloRGB_LEDRGB_Red_PIN, HelloRGB_LEDRGB_Red_PIN_rawData);
  91.   digitalWrite(HelloRGB_LEDRGB_Green_PIN, HelloRGB_LEDRGB_Green_PIN_rawData);
  92. }
  93.  
  94. void displayData(float distance, float temperature, float humidity)
  95. {
  96.   lcd.clear(); // Clear the LCD display
  97.   lcd.setCursor(0, 0);  // Set the cursor to the first line
  98.  
  99.   // Display the distance on the LCD
  100.   lcd.print("Distance: ");
  101.   lcd.print(distance);
  102.   lcd.print(" cm");
  103.  
  104.   lcd.setCursor(0, 1);  // Set the cursor to the second line
  105.  
  106.   // Display the temperature on the LCD
  107.   lcd.print("Temp: ");
  108.   lcd.print(temperature);
  109.   lcd.print(" C");
  110.  
  111.   // Display the humidity on the LCD
  112.   lcd.print(" Humidity: ");
  113.   lcd.print(humidity);
  114.   lcd.print(" %");
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement