Advertisement
pleasedontcode

**Sensor Data** rev_57

Oct 9th, 2024
75
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:23:56
  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(); // Function to read temperature
  46. void performCountdown(); // Function to perform countdown
  47. void readPulseOximeter(); // Function to read pulse oximeter data
  48.  
  49. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  50. const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9       = 9;
  51. const uint8_t PulseOximeter_MAX30100_INT_PIN_D2     = 2;
  52. const uint8_t temperatureSensor_DS18B20_DQ_PIN_D4       = 4;
  53.  
  54. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  55. const uint8_t myLCD_LCD1602_RS_PIN_D3       = 3;
  56. const uint8_t myLCD_LCD1602_D4_PIN_D5       = 5;
  57. const uint8_t myLCD_LCD1602_D5_PIN_D6       = 6;
  58. const uint8_t myLCD_LCD1602_D6_PIN_D7       = 7;
  59. const uint8_t myLCD_LCD1602_D7_PIN_D8       = 8;
  60.  
  61. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  62. bool    myLCD_LCD1602_RS_PIN_D3_rawData     = 0;
  63. bool    myLCD_LCD1602_D4_PIN_D5_rawData     = 0;
  64. bool    myLCD_LCD1602_D5_PIN_D6_rawData     = 0;
  65. bool    myLCD_LCD1602_D6_PIN_D7_rawData     = 0;
  66. bool    myLCD_LCD1602_D7_PIN_D8_rawData     = 0;
  67.  
  68. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  69. float temperature = 0.0; // Variable to store temperature
  70. unsigned long countdownStartTime = 0; // Variable to track countdown start time
  71. bool countdownActive = false; // Flag to check if countdown is active
  72. unsigned long pulseOximeterReadTime = 0; // Variable to track pulse oximeter read time
  73.  
  74. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  75. // Instantiate library objects
  76. DS18B20 ds18b20(temperatureSensor_DS18B20_DQ_PIN_D4);
  77. 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);
  78. PulseOximeter pulseOximeter;
  79.  
  80. void setup(void)
  81. {
  82.     // put your setup code here, to run once:
  83.     pinMode(temperatureSensor_DHT22_DOUT_PIN_D9, INPUT_PULLUP);
  84.     pinMode(PulseOximeter_MAX30100_INT_PIN_D2, INPUT_PULLUP);
  85.     pinMode(temperatureSensor_DS18B20_DQ_PIN_D4, INPUT);
  86.  
  87.     pinMode(myLCD_LCD1602_RS_PIN_D3, OUTPUT);
  88.     pinMode(myLCD_LCD1602_D4_PIN_D5, OUTPUT);
  89.     pinMode(myLCD_LCD1602_D5_PIN_D6, OUTPUT);
  90.     pinMode(myLCD_LCD1602_D6_PIN_D7, OUTPUT);
  91.     pinMode(myLCD_LCD1602_D7_PIN_D8, OUTPUT);
  92.  
  93.     lcd.begin(16, 2); // Initialize LCD
  94.     pulseOximeter.begin(); // Initialize Pulse Oximeter
  95. }
  96.  
  97. void loop(void)
  98. {
  99.     // put your main code here, to run repeatedly:
  100.     readTemperature(); // Read temperature from DS18B20
  101.     if (countdownActive) {
  102.         performCountdown(); // Perform countdown if active
  103.     }
  104.     readPulseOximeter(); // Read pulse oximeter data
  105.     updateOutputs(); // Refresh output data
  106. }
  107.  
  108. void readTemperature() {
  109.     // Read temperature from DS18B20 sensor
  110.     temperature = ds18b20.getTempC();
  111.     lcd.clear();
  112.     lcd.print("Temp: ");
  113.     lcd.print(temperature);
  114.     lcd.print(" C");
  115.     countdownStartTime = millis(); // Start countdown timer
  116.     countdownActive = true; // Activate countdown
  117. }
  118.  
  119. void performCountdown() {
  120.     // Perform a countdown of 10 seconds
  121.     unsigned long currentTime = millis();
  122.     if (currentTime - countdownStartTime < 10000) { // 10 seconds countdown
  123.         int remainingTime = 10 - (currentTime - countdownStartTime) / 1000; // Calculate remaining time
  124.         lcd.setCursor(0, 1); // Move to the second line
  125.         lcd.print("Countdown: ");
  126.         lcd.print(remainingTime);
  127.         lcd.print(" s");
  128.     } else {
  129.         countdownActive = false; // Deactivate countdown after 10 seconds
  130.         lcd.clear(); // Clear LCD
  131.         lcd.print("Done!");
  132.         countdownStartTime = 0; // Reset countdown start time
  133.     }
  134. }
  135.  
  136. void readPulseOximeter() {
  137.     // Read data from the pulse oximeter
  138.     if (millis() - pulseOximeterReadTime > 1000) { // Read every second
  139.         pulseOximeter.update();
  140.         float heartRate = pulseOximeter.getHeartRate();
  141.         uint8_t spo2 = pulseOximeter.getSpO2();
  142.         lcd.setCursor(0, 0);
  143.         lcd.print("HR: ");
  144.         lcd.print(heartRate);
  145.         lcd.print(" bpm");
  146.         lcd.setCursor(0, 1);
  147.         lcd.print("SpO2: ");
  148.         lcd.print(spo2);
  149.         lcd.print(" %");
  150.         pulseOximeterReadTime = millis(); // Update read time
  151.     }
  152. }
  153.  
  154. void updateOutputs()
  155. {
  156.     digitalWrite(myLCD_LCD1602_RS_PIN_D3, myLCD_LCD1602_RS_PIN_D3_rawData);
  157.     digitalWrite(myLCD_LCD1602_D4_PIN_D5, myLCD_LCD1602_D4_PIN_D5_rawData);
  158.     digitalWrite(myLCD_LCD1602_D5_PIN_D6, myLCD_LCD1602_D5_PIN_D6_rawData);
  159.     digitalWrite(myLCD_LCD1602_D6_PIN_D7, myLCD_LCD1602_D6_PIN_D7_rawData);
  160.     digitalWrite(myLCD_LCD1602_D7_PIN_D8, myLCD_LCD1602_D7_PIN_D8_rawData);
  161. }
  162.  
  163. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement