Advertisement
pleasedontcode

**Sensor Data** rev_49

Oct 8th, 2024
56
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: **Sensor Data**
  13.     - Source Code compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-08 15:12:31
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Read temperature from DS18B20 sensor and display */
  21.     /* on LCD screen. */
  22. /****** SYSTEM REQUIREMENT 2 *****/
  23.     /* Perform a countdown of 10 seconds after each */
  24.     /* temperature reading using LiquidCrystal library */
  25.     /* for display control. */
  26. /****** SYSTEM REQUIREMENT 3 *****/
  27.     /* use max pulse oximeter */
  28. /****** SYSTEM REQUIREMENT 4 *****/
  29.     /* use non blocking code. */
  30. /****** END SYSTEM REQUIREMENTS *****/
  31.  
  32. /* START CODE */
  33.  
  34. /****** DEFINITION OF LIBRARIES *****/
  35. #include <Wire.h>
  36. #include <DS18B20.h>    // https://github.com/matmunk/DS18B20
  37. #include <LiquidCrystal.h>    // https://github.com/arduino-libraries/LiquidCrystal
  38. #include <DHT.h>    // https://github.com/adafruit/DHT-sensor-library
  39. #include <MAX30100_PulseOximeter.h>    // https://github.com/gabriel-milan/Arduino-MAX30100
  40.  
  41. /****** FUNCTION PROTOTYPES *****/
  42. void setup(void);
  43. void loop(void);
  44. void updateOutputs();
  45. void readTemperature();
  46. void displayCountdown(int seconds);
  47. bool countdownActive = false; // Flag to indicate countdown status
  48. unsigned long countdownStartTime; // Start time for countdown
  49. int countdownDuration = 10; // Countdown duration in seconds
  50.  
  51. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  52. const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9 = 9;
  53. const uint8_t PulseOximeter_MAX30100_INT_PIN_D2 = 2;
  54. const uint8_t temperatureSensor_DS18B20_DQ_PIN_D4 = 4;
  55.  
  56. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  57. const uint8_t myLCD_LCD1602_RS_PIN_D3 = 3;
  58. const uint8_t myLCD_LCD1602_D4_PIN_D5 = 5;
  59. const uint8_t myLCD_LCD1602_D5_PIN_D6 = 6;
  60. const uint8_t myLCD_LCD1602_D6_PIN_D7 = 7;
  61. const uint8_t myLCD_LCD1602_D7_PIN_D8 = 8;
  62.  
  63. /***** DEFINITION OF OUTPUT VARIABLES *****/
  64. // Updated LiquidCrystal initialization to match the constructor requirements
  65. LiquidCrystal lcd(myLCD_LCD1602_RS_PIN_D3, myLCD_LCD1602_D4_PIN_D5, myLCD_LCD1602_D5_PIN_D6, myLCD_LCD1602_D6_PIN_D7, myLCD_LCD1602_D7_PIN_D8, 0); // 0 for RW pin
  66. DHT dht(temperatureSensor_DHT22_DOUT_PIN_D9, DHT22);
  67. DS18B20 ds18b20(temperatureSensor_DS18B20_DQ_PIN_D4);
  68. PulseOximeter pulseOximeter;
  69.  
  70. /****** SETUP FUNCTION *****/
  71. void setup(void)
  72. {
  73.     Serial.begin(115200);
  74.    
  75.     // Initialize LCD
  76.     lcd.begin(16, 2);
  77.     lcd.clear();
  78.    
  79.     // Initialize DHT sensor
  80.     dht.begin();
  81.    
  82.     // Initialize DS18B20 sensor
  83.     // Removed the begin() method as it does not exist in the library
  84.     // Initialize Pulse Oximeter
  85.     if (!pulseOximeter.begin()) {
  86.         Serial.println("Failed to initialize Pulse Oximeter!");
  87.         while (1);
  88.     }
  89. }
  90.  
  91. /****** LOOP FUNCTION *****/
  92. void loop(void)
  93. {
  94.     // Read temperature and display it
  95.     readTemperature();
  96.    
  97.     // Check if countdown is active
  98.     if (countdownActive) {
  99.         // Check if countdown is finished
  100.         if (millis() - countdownStartTime >= countdownDuration * 1000) {
  101.             countdownActive = false; // Stop countdown
  102.             lcd.clear(); // Clear the display after countdown
  103.         } else {
  104.             // Display remaining time
  105.             int remainingTime = countdownDuration - ((millis() - countdownStartTime) / 1000);
  106.             lcd.setCursor(0, 1);
  107.             lcd.print("Countdown: ");
  108.             lcd.print(remainingTime);
  109.         }
  110.     } else {
  111.         // Start countdown after reading temperature
  112.         countdownActive = true;
  113.         countdownStartTime = millis(); // Record start time
  114.     }
  115.    
  116.     // Update pulse oximeter readings
  117.     pulseOximeter.update();
  118.    
  119.     // Display heart rate and SpO2
  120.     Serial.print("Heart rate: ");
  121.     Serial.print(pulseOximeter.getHeartRate());
  122.     Serial.print(" bpm / SpO2: ");
  123.     Serial.print(pulseOximeter.getSpO2());
  124.     Serial.println(" %");
  125. }
  126.  
  127. /****** FUNCTION TO READ TEMPERATURE *****/
  128. void readTemperature() {
  129.     float temperature = dht.readTemperature();
  130.     if (isnan(temperature)) {
  131.         Serial.println("Failed to read from DHT sensor!");
  132.     } else {
  133.         lcd.setCursor(0, 0);
  134.         lcd.print("Temp: ");
  135.         lcd.print(temperature);
  136.         lcd.print(" C");
  137.     }
  138. }
  139.  
  140. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement