Advertisement
pleasedontcode

**Countdown Display** rev_55

Oct 9th, 2024
66
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: **Countdown Display**
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-09 23:09:33
  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.  
  48. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  49. const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9       = 9;
  50. const uint8_t PulseOximeter_MAX30100_INT_PIN_D2     = 2;
  51. const uint8_t temperatureSensor_DS18B20_DQ_PIN_D4       = 4;
  52.  
  53. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  54. const uint8_t myLCD_LCD1602_RS_PIN_D3       = 3;
  55. const uint8_t myLCD_LCD1602_D4_PIN_D5       = 5;
  56. const uint8_t myLCD_LCD1602_D5_PIN_D6       = 6;
  57. const uint8_t myLCD_LCD1602_D6_PIN_D7       = 7;
  58. const uint8_t myLCD_LCD1602_D7_PIN_D8       = 8;
  59.  
  60. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  61. bool    myLCD_LCD1602_RS_PIN_D3_rawData     = 0;
  62. bool    myLCD_LCD1602_D4_PIN_D5_rawData     = 0;
  63. bool    myLCD_LCD1602_D5_PIN_D6_rawData     = 0;
  64. bool    myLCD_LCD1602_D6_PIN_D7_rawData     = 0;
  65. bool    myLCD_LCD1602_D7_PIN_D8_rawData     = 0;
  66.  
  67. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  68. float   myLCD_LCD1602_RS_PIN_D3_phyData     = 0.0;
  69. float   myLCD_LCD1602_D4_PIN_D5_phyData     = 0.0;
  70. float   myLCD_LCD1602_D5_PIN_D6_phyData     = 0.0;
  71. float   myLCD_LCD1602_D6_PIN_D7_phyData     = 0.0;
  72. float   myLCD_LCD1602_D7_PIN_D8_phyData     = 0.0;
  73.  
  74. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  75. // Instantiate the DS18B20 sensor
  76. DS18B20 ds(temperatureSensor_DS18B20_DQ_PIN_D4);
  77. // Instantiate the LiquidCrystal display
  78. 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);
  79. // Instantiate the PulseOximeter
  80. PulseOximeter pulseOximeter;
  81.  
  82. /****** VARIABLES FOR COUNTDOWN TIMER *****/
  83. unsigned long previousMillis = 0; // Store last time countdown was updated
  84. int countdownTime = 10; // Countdown time in seconds
  85. bool countdownActive = false;
  86.  
  87. void setup(void)
  88. {
  89.     // put your setup code here, to run once:
  90.     pinMode(temperatureSensor_DHT22_DOUT_PIN_D9, INPUT_PULLUP);
  91.     pinMode(PulseOximeter_MAX30100_INT_PIN_D2, INPUT_PULLUP);
  92.     pinMode(temperatureSensor_DS18B20_DQ_PIN_D4, INPUT);
  93.  
  94.     pinMode(myLCD_LCD1602_RS_PIN_D3, OUTPUT);
  95.     pinMode(myLCD_LCD1602_D4_PIN_D5, OUTPUT);
  96.     pinMode(myLCD_LCD1602_D5_PIN_D6, OUTPUT);
  97.     pinMode(myLCD_LCD1602_D6_PIN_D7, OUTPUT);
  98.     pinMode(myLCD_LCD1602_D7_PIN_D8, OUTPUT);
  99.  
  100.     // Initialize the LCD
  101.     lcd.begin(16, 2);
  102.     // Initialize the Pulse Oximeter
  103.     pulseOximeter.begin();
  104. }
  105.  
  106. void loop(void)
  107. {
  108.     // put your main code here, to run repeatedly:
  109.     updateOutputs(); // Refresh output data
  110.     readTemperature(); // Read temperature from DS18B20
  111.  
  112.     // Update pulse oximeter readings
  113.     pulseOximeter.update();
  114.  
  115.     // Check if countdown is active
  116.     if (countdownActive) {
  117.         unsigned long currentMillis = millis();
  118.         if (currentMillis - previousMillis >= 1000) { // Check if a second has passed
  119.             previousMillis = currentMillis;
  120.             countdownTime--;
  121.             if (countdownTime <= 0) {
  122.                 countdownActive = false; // Stop countdown
  123.             }
  124.         }
  125.     }
  126.  
  127.     // Display countdown if active
  128.     if (countdownActive) {
  129.         displayCountdown(countdownTime);
  130.     }
  131. }
  132.  
  133. void updateOutputs()
  134. {
  135.     digitalWrite(myLCD_LCD1602_RS_PIN_D3, myLCD_LCD1602_RS_PIN_D3_rawData);
  136.     digitalWrite(myLCD_LCD1602_D4_PIN_D5, myLCD_LCD1602_D4_PIN_D5_rawData);
  137.     digitalWrite(myLCD_LCD1602_D5_PIN_D6, myLCD_LCD1602_D5_PIN_D6_rawData);
  138.     digitalWrite(myLCD_LCD1602_D6_PIN_D7, myLCD_LCD1602_D6_PIN_D7_rawData);
  139.     digitalWrite(myLCD_LCD1602_D7_PIN_D8, myLCD_LCD1602_D7_PIN_D8_rawData);
  140. }
  141.  
  142. void readTemperature()
  143. {
  144.     // Start temperature conversion
  145.     ds.doConversion();
  146.     // Read temperature in Celsius
  147.     float temperature = ds.getTempC();
  148.     // Display temperature on LCD
  149.     lcd.clear();
  150.     lcd.print("Temp: ");
  151.     lcd.print(temperature);
  152.     lcd.print(" C");
  153.     // Start countdown after reading temperature
  154.     countdownTime = 10; // Reset countdown time
  155.     countdownActive = true; // Start countdown
  156. }
  157.  
  158. void displayCountdown(int seconds)
  159. {
  160.     lcd.setCursor(0, 1); // Set cursor to the second line
  161.     lcd.print("Countdown: ");
  162.     lcd.print(seconds);
  163. }
  164.  
  165. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement