Advertisement
pleasedontcode

**Temperature Sensors** rev_35

Oct 3rd, 2024
68
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: **Temperature Sensors**
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-03 13:48:31
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Initialize internal Bluetooth device as master, */
  21.     /* receive BLE data, and show it on a 16x2 LCD using */
  22.     /* LiquidCrystal library for Arduino. */
  23. /****** SYSTEM REQUIREMENT 2 *****/
  24.     /* read temperature and send it via bluetooth. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <LiquidCrystal.h>    // https://github.com/arduino-libraries/LiquidCrystal
  30. #include <DS18B20.h>          // https://github.com/matmunk/DS18B20
  31. #include <DHT.h>              // https://github.com/adafruit/DHT-sensor-library
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void updateOutputs(void);
  37. void readTemperatureAndSend(void);
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t temp_DS18B20_DQ_PIN_D8 = 8;
  41. const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9 = 9;
  42.  
  43. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  44. const uint8_t display_LCD1602_RS_PIN_D2 = 2;
  45. const uint8_t display_LCD1602_E_PIN_D3 = 3;
  46. const uint8_t display_LCD1602_D4_PIN_D4 = 4;
  47. const uint8_t display_LCD1602_D5_PIN_D5 = 5;
  48. const uint8_t display_LCD1602_D6_PIN_D6 = 6;
  49. const uint8_t display_LCD1602_D7_PIN_D7 = 7;
  50.  
  51. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  52. bool display_LCD1602_RS_PIN_D2_rawData = 0;
  53. bool display_LCD1602_E_PIN_D3_rawData = 0;
  54. bool display_LCD1602_D4_PIN_D4_rawData = 0;
  55. bool display_LCD1602_D5_PIN_D5_rawData = 0;
  56. bool display_LCD1602_D6_PIN_D6_rawData = 0;
  57. bool display_LCD1602_D7_PIN_D7_rawData = 0;
  58.  
  59. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  60. float display_LCD1602_RS_PIN_D2_phyData = 0.0;
  61. float display_LCD1602_E_PIN_D3_phyData = 0.0;
  62. float display_LCD1602_D4_PIN_D4_phyData = 0.0;
  63. float display_LCD1602_D5_PIN_D5_phyData = 0.0;
  64. float display_LCD1602_D6_PIN_D6_phyData = 0.0;
  65. float display_LCD1602_D7_PIN_D7_phyData = 0.0;
  66.  
  67. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  68. LiquidCrystal lcd(display_LCD1602_RS_PIN_D2, display_LCD1602_E_PIN_D3, display_LCD1602_D4_PIN_D4, display_LCD1602_D5_PIN_D5, display_LCD1602_D6_PIN_D6, display_LCD1602_D7_PIN_D7);
  69. DHT dht(temperatureSensor_DHT22_DOUT_PIN_D9, DHT22);
  70. DS18B20 tempSensor(temp_DS18B20_DQ_PIN_D8);
  71.  
  72. void setup(void)
  73. {
  74.     // Initialize the LCD
  75.     lcd.begin(16, 2);
  76.     lcd.clear();
  77.     lcd.print("Initializing...");
  78.  
  79.     // Initialize the DHT sensor
  80.     dht.begin();
  81.  
  82.     // Initialize the DS18B20 sensor
  83.     tempSensor.Update();
  84.  
  85.     // Set pin modes
  86.     pinMode(temp_DS18B20_DQ_PIN_D8, INPUT);
  87.     pinMode(temperatureSensor_DHT22_DOUT_PIN_D9, INPUT_PULLUP);
  88.     pinMode(display_LCD1602_RS_PIN_D2, OUTPUT);
  89.     pinMode(display_LCD1602_E_PIN_D3, OUTPUT);
  90.     pinMode(display_LCD1602_D4_PIN_D4, OUTPUT);
  91.     pinMode(display_LCD1602_D5_PIN_D5, OUTPUT);
  92.     pinMode(display_LCD1602_D6_PIN_D6, OUTPUT);
  93.     pinMode(display_LCD1602_D7_PIN_D7, OUTPUT);
  94. }
  95.  
  96. void loop(void)
  97. {
  98.     // Read temperature and send via Bluetooth
  99.     readTemperatureAndSend();
  100.  
  101.     // Refresh output data
  102.     updateOutputs();
  103. }
  104.  
  105. void readTemperatureAndSend()
  106. {
  107.     // Read temperature from DHT sensor
  108.     float temperature = dht.readTemperature();
  109.     if (isnan(temperature)) {
  110.         lcd.print("DHT Error");
  111.     } else {
  112.         lcd.print("Temp: ");
  113.         lcd.print(temperature);
  114.         lcd.print(" C");
  115.         // Here you can add code to send temperature via Bluetooth
  116.     }
  117.  
  118.     // Read temperature from DS18B20 sensor
  119.     float dsTemperature = tempSensor.GetTemperature<float>();
  120.     if (dsTemperature != NAN) {
  121.         lcd.print("DS Temp: ");
  122.         lcd.print(dsTemperature);
  123.         lcd.print(" C");
  124.         // Here you can add code to send temperature via Bluetooth
  125.     }
  126. }
  127.  
  128. void updateOutputs()
  129. {
  130.     digitalWrite(display_LCD1602_RS_PIN_D2, display_LCD1602_RS_PIN_D2_rawData);
  131.     digitalWrite(display_LCD1602_E_PIN_D3, display_LCD1602_E_PIN_D3_rawData);
  132.     digitalWrite(display_LCD1602_D4_PIN_D4, display_LCD1602_D4_PIN_D4_rawData);
  133.     digitalWrite(display_LCD1602_D5_PIN_D5, display_LCD1602_D5_PIN_D5_rawData);
  134.     digitalWrite(display_LCD1602_D6_PIN_D6, display_LCD1602_D6_PIN_D6_rawData);
  135.     digitalWrite(display_LCD1602_D7_PIN_D7, display_LCD1602_D7_PIN_D7_rawData);
  136. }
  137.  
  138. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement