Advertisement
pleasedontcode

**Sensor Data** rev_54

Oct 9th, 2024
59
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 NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-09 23:03:18
  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>    // DS18B20 temperature sensor library
  37. #include <LiquidCrystal.h>    // LiquidCrystal library for LCD control
  38. #include <DHT.h>    // DHT sensor library
  39. #include <MAX30100_PulseOximeter.h>    // MAX30100 pulse oximeter library
  40.  
  41. /****** FUNCTION PROTOTYPES *****/
  42. void setup(void);
  43. void loop(void);
  44. void readTemperatureAndDisplay();
  45. void countdown(int seconds);
  46.  
  47. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  48. const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9 = 9;
  49. const uint8_t PulseOximeter_MAX30100_INT_PIN_D2 = 2;
  50. const uint8_t temperatureSensor_DS18B20_DQ_PIN_D4 = 4;
  51.  
  52. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  53. const uint8_t myLCD_LCD1602_RS_PIN_D3 = 3;
  54. const uint8_t myLCD_LCD1602_D4_PIN_D5 = 5;
  55. const uint8_t myLCD_LCD1602_D5_PIN_D6 = 6;
  56. const uint8_t myLCD_LCD1602_D6_PIN_D7 = 7;
  57. const uint8_t myLCD_LCD1602_D7_PIN_D8 = 8;
  58.  
  59. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  60. // Create instances for the sensors and LCD
  61. DS18B20 tempSensor(temperatureSensor_DS18B20_DQ_PIN_D4);
  62. 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);
  63. DHT dht(temperatureSensor_DHT22_DOUT_PIN_D9, DHT22);
  64. PulseOximeter pox;
  65.  
  66. // Countdown variables
  67. unsigned long previousMillis = 0;
  68. int countdownTime = 10;
  69. bool countingDown = false;
  70.  
  71. void setup(void)
  72. {
  73.     // Initialize serial communication
  74.     Serial.begin(9600);
  75.  
  76.     // Initialize the LCD
  77.     lcd.begin(16, 2);
  78.     lcd.print("Initializing...");
  79.  
  80.     // Initialize the DHT sensor
  81.     dht.begin();
  82.  
  83.     // Initialize the pulse oximeter
  84.     if (!pox.begin()) {
  85.         Serial.println("Failed to initialize pulse oximeter");
  86.         while (1);
  87.     }
  88.  
  89.     // Set up the pulse oximeter
  90.     pox.setOnBeatDetectedCallback([]() {
  91.         Serial.println("Beat detected!");
  92.     });
  93.  
  94.     // Set pin modes
  95.     pinMode(temperatureSensor_DHT22_DOUT_PIN_D9, INPUT_PULLUP);
  96.     pinMode(PulseOximeter_MAX30100_INT_PIN_D2, INPUT_PULLUP);
  97.     pinMode(temperatureSensor_DS18B20_DQ_PIN_D4, INPUT);
  98. }
  99.  
  100. void loop(void)
  101. {
  102.     // Read temperature and display it on the LCD
  103.     readTemperatureAndDisplay();
  104.  
  105.     // Perform a countdown of 10 seconds if not already counting down
  106.     if (!countingDown) {
  107.         countingDown = true;
  108.         previousMillis = millis();
  109.         countdownTime = 10; // Reset countdown
  110.     }
  111.  
  112.     // Non-blocking countdown logic
  113.     if (countingDown) {
  114.         unsigned long currentMillis = millis();
  115.         if (currentMillis - previousMillis >= 1000) { // Check if 1 second has passed
  116.             previousMillis = currentMillis;
  117.             if (countdownTime > 0) {
  118.                 lcd.setCursor(0, 1); // Set cursor to the second line
  119.                 lcd.print("Countdown: ");
  120.                 lcd.print(countdownTime);
  121.                 countdownTime--;
  122.             } else {
  123.                 countingDown = false; // Stop countdown when it reaches 0
  124.             }
  125.         }
  126.     }
  127. }
  128.  
  129. void readTemperatureAndDisplay()
  130. {
  131.     // Read temperature from DS18B20
  132.     float temperature = tempSensor.getTempC();
  133.     lcd.clear();
  134.     lcd.print("Temp: ");
  135.     lcd.print(temperature);
  136.     lcd.print(" C");
  137. }
  138.  
  139. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement