CCCPaste

**Sensor Data** rev_57

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: **Sensor Data**
	- Source Code NOT compiled for: Arduino Nano ESP32
	- Source Code created on: 2024-10-09 23:23:56

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Read temperature from DS18B20 sensor and display */
	/* on LCD screen. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* Perform a countdown of 10 seconds after each */
	/* temperature reading using LiquidCrystal library */
	/* for display control. */
/****** SYSTEM REQUIREMENT 3 *****/
	/* use max pulse oximeter */
/****** SYSTEM REQUIREMENT 4 *****/
	/* use non blocking code. */
/****** END SYSTEM REQUIREMENTS *****/

/* START CODE */

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 	//https://github.com/matmunk/DS18B20
#include 	//https://github.com/arduino-libraries/LiquidCrystal
#include 	//https://github.com/adafruit/DHT-sensor-library
#include 	//https://github.com/gabriel-milan/Arduino-MAX30100

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs();
void readTemperature(); // Function to read temperature
void performCountdown(); // Function to perform countdown
void readPulseOximeter(); // Function to read pulse oximeter data

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t temperatureSensor_DHT22_DOUT_PIN_D9		= 9;
const uint8_t PulseOximeter_MAX30100_INT_PIN_D2		= 2;
const uint8_t temperatureSensor_DS18B20_DQ_PIN_D4		= 4;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t myLCD_LCD1602_RS_PIN_D3		= 3;
const uint8_t myLCD_LCD1602_D4_PIN_D5		= 5;
const uint8_t myLCD_LCD1602_D5_PIN_D6		= 6;
const uint8_t myLCD_LCD1602_D6_PIN_D7		= 7;
const uint8_t myLCD_LCD1602_D7_PIN_D8		= 8;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
bool	myLCD_LCD1602_RS_PIN_D3_rawData		= 0;
bool	myLCD_LCD1602_D4_PIN_D5_rawData		= 0;
bool	myLCD_LCD1602_D5_PIN_D6_rawData		= 0;
bool	myLCD_LCD1602_D6_PIN_D7_rawData		= 0;
bool	myLCD_LCD1602_D7_PIN_D8_rawData		= 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
float temperature = 0.0; // Variable to store temperature
unsigned long countdownStartTime = 0; // Variable to track countdown start time
bool countdownActive = false; // Flag to check if countdown is active
unsigned long pulseOximeterReadTime = 0; // Variable to track pulse oximeter read time

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Instantiate library objects
DS18B20 ds18b20(temperatureSensor_DS18B20_DQ_PIN_D4);
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);
PulseOximeter pulseOximeter;

void setup(void)
{
	// put your setup code here, to run once:
	pinMode(temperatureSensor_DHT22_DOUT_PIN_D9, INPUT_PULLUP);
	pinMode(PulseOximeter_MAX30100_INT_PIN_D2, INPUT_PULLUP);
	pinMode(temperatureSensor_DS18B20_DQ_PIN_D4, INPUT);

	pinMode(myLCD_LCD1602_RS_PIN_D3, OUTPUT);
	pinMode(myLCD_LCD1602_D4_PIN_D5, OUTPUT);
	pinMode(myLCD_LCD1602_D5_PIN_D6, OUTPUT);
	pinMode(myLCD_LCD1602_D6_PIN_D7, OUTPUT);
	pinMode(myLCD_LCD1602_D7_PIN_D8, OUTPUT);

	lcd.begin(16, 2); // Initialize LCD
	pulseOximeter.begin(); // Initialize Pulse Oximeter
}

void loop(void)
{
	// put your main code here, to run repeatedly:
	readTemperature(); // Read temperature from DS18B20
	if (countdownActive) {
		performCountdown(); // Perform countdown if active
	}
	readPulseOximeter(); // Read pulse oximeter data
	updateOutputs(); // Refresh output data
}

void readTemperature() {
	// Read temperature from DS18B20 sensor
	temperature = ds18b20.getTempC();
	lcd.clear();
	lcd.print("Temp: ");
	lcd.print(temperature);
	lcd.print(" C");
	countdownStartTime = millis(); // Start countdown timer
	countdownActive = true; // Activate countdown
}

void performCountdown() {
	// Perform a countdown of 10 seconds
	unsigned long currentTime = millis();
	if (currentTime - countdownStartTime < 10000) { // 10 seconds countdown
		int remainingTime = 10 - (currentTime - countdownStartTime) / 1000; // Calculate remaining time
		lcd.setCursor(0, 1); // Move to the second line
		lcd.print("Countdown: ");
		lcd.print(remainingTime);
		lcd.print(" s");
	} else {
		countdownActive = false; // Deactivate countdown after 10 seconds
		lcd.clear(); // Clear LCD
		lcd.print("Done!");
		countdownStartTime = 0; // Reset countdown start time
	}
}

void readPulseOximeter() {
	// Read data from the pulse oximeter
	if (millis() - pulseOximeterReadTime > 1000) { // Read every second
		pulseOximeter.update();
		float heartRate = pulseOximeter.getHeartRate();
		uint8_t spo2 = pulseOximeter.getSpO2();
		lcd.setCursor(0, 0);
		lcd.print("HR: ");
		lcd.print(heartRate);
		lcd.print(" bpm");
		lcd.setCursor(0, 1);
		lcd.print("SpO2: ");
		lcd.print(spo2);
		lcd.print(" %");
		pulseOximeterReadTime = millis(); // Update read time
	}
}

void updateOutputs()
{
	digitalWrite(myLCD_LCD1602_RS_PIN_D3, myLCD_LCD1602_RS_PIN_D3_rawData);
	digitalWrite(myLCD_LCD1602_D4_PIN_D5, myLCD_LCD1602_D4_PIN_D5_rawData);
	digitalWrite(myLCD_LCD1602_D5_PIN_D6, myLCD_LCD1602_D5_PIN_D6_rawData);
	digitalWrite(myLCD_LCD1602_D6_PIN_D7, myLCD_LCD1602_D6_PIN_D7_rawData);
	digitalWrite(myLCD_LCD1602_D7_PIN_D8, myLCD_LCD1602_D7_PIN_D8_rawData);
}

/* END CODE */