#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Constants for pins
const int thermistorPin = A0; // Analog pin for thermistor
const int pwmPin = 11; // PWM pin to MOSFET gate
const int trigPin = 9; // Trigger pin for ultrasonic sensor
const int echoPin = 10; // Echo pin for ultrasonic sensor
const int relay1Pin = 6; // Relay 1 pin
const int relay2Pin = 8; // Relay 2 pin
const int buzzerPin = 7; // Buzzer pin
const int switchbuttonPin = 5; // Switch button pin
// Temperature range
const float tempMin = 27.0;
const float tempMax = 34.0;
// LCD setup
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Variables
float babyTemp = 0.0;
float babyHeight = 0.0;
bool relay1On = false;
bool relay2On = false;
unsigned long lightOnStartTime = 0;
unsigned long lastInteractionTime = 0;
unsigned long currentTime = 0;
const unsigned long timeoutPeriod = 30000; // 30 seconds of inactivity
const unsigned long shuffleInterval = 10000; // 10 seconds for shuffling
bool inDefaultMode = false;
unsigned long lastShuffleTime = 0;
bool showTime = true;
// Button state tracking
int buttonState = 0;
int lastButtonState = 0;
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize serial communication
Serial.begin(9600);
// Initialize pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(switchbuttonPin, INPUT);
pinMode(pwmPin, OUTPUT);
// Set relays off initially
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, LOW);
lcd.setCursor(0, 0);
lcd.print("Phototherapy System");
delay(1000);
lastInteractionTime = millis(); // Initialize last interaction time
}
void loop() {
currentTime = millis();
// Check if user has interacted recently
if (currentTime - lastInteractionTime < timeoutPeriod) {
// User has interacted recently, normal operation
inDefaultMode = false;
} else {
// No user interaction, switch to default mode
inDefaultMode = true;
displayDefault();
}
// Dosing functionality based on switch
dosing();
// Check temperature range and alert
babyTemp = readTemperature();
if (babyTemp < tempMin || babyTemp > tempMax) {
buzzer();
} else {
digitalWrite(buzzerPin, LOW);
}
// Display all parameters live
displayAllParameters();
}
void dosing() {
buttonState = digitalRead(switchbuttonPin);
// Check if the button transitioned from LOW to HIGH (pressed to released)
if (buttonState == HIGH && lastButtonState == LOW) {
if (!relay1On && !relay2On) {
relay1On = true;
digitalWrite(relay1Pin, HIGH);
digitalWrite(relay2Pin, LOW);
Serial.println("Relay 1 ON");
} else if (relay1On && !relay2On) {
relay2On = true;
relay1On = false;
digitalWrite(relay2Pin, HIGH);
digitalWrite(relay1Pin, LOW);
lightOnStartTime = millis(); // Update start time for light duration
Serial.println("Relay 2 ON");
} else {
relay1On = false;
relay2On = false;
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, LOW);
Serial.println("Both relays OFF");
}
lastInteractionTime = millis(); // Reset interaction timer
}
lastButtonState = buttonState; // Remember the value for the next time
}
void displayintensity() {
// Placeholder for intensity display if needed in the future
}
void displaytime() {
// Display on-time on LCD
unsigned long currentTime = millis();
if (relay2On) { // Check if light is on to calculate duration
unsigned long lightOnDuration = (currentTime - lightOnStartTime) / 1000; // Duration in seconds
int hours = lightOnDuration / 3600;
int minutes = (lightOnDuration % 3600) / 60;
int seconds = lightOnDuration % 60;
lcd.setCursor(0, 2);
lcd.print("Time: ");
lcd.print(hours);
lcd.print(":");
if (minutes < 10) lcd.print("0");
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) lcd.print("0");
lcd.print(seconds);
lcd.print(" ");
} else {
lcd.setCursor(0, 2);
lcd.print("Time: 00:00:00 "); // Display 00:00:00 if light is off
}
}
// Function to read temperature from thermistor
float readTemperature() {
int analogValue = analogRead(thermistorPin);
float voltage = analogValue * (5.0 / 1023.0);
float resistance = (5.0 - voltage) * 10000 / voltage;
float temperature = 1 / (log(resistance / 10000) / 3950 + 1 / 298.15) - 273.15;
return temperature;
}
// Function to display temperature from thermistor
void displaytemp() {
// Display baby's temperature
babyTemp = readTemperature();
lcd.setCursor(0, 0);
lcd.print("BabyTemp: ");
lcd.print(babyTemp, 1);
lcd.print(" C ");
Serial.print("BabyTemp: ");
Serial.print(babyTemp, 1);
Serial.println(" C ");
}
// Function to read height from ultrasonic sensor
float readHeight() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = (duration / 2.0) * 0.0343;
return distance;
}
// Function to display height from ultrasonic sensor
void displayheight() {
babyHeight = readHeight();
lcd.setCursor(0, 1);
lcd.print("Height: ");
lcd.print(babyHeight, 0);
lcd.print("cm ");
Serial.print("Height: ");
Serial.print(babyHeight, 1);
Serial.println("cm ");
}
// Function to alert using buzzer
void buzzer() {
tone(buzzerPin, 1000); // Send 1KHz sound signal
delay(500); // ...for 500 ms
noTone(buzzerPin); // Stop sound
}
// Function to display default screen
void displayDefault() {
displaytemp();
if (currentTime - lastShuffleTime > shuffleInterval) {
lastShuffleTime = currentTime;
showTime = !showTime;
}
if (showTime) {
displaytime();
} else {
// Placeholder for intensity display if needed in the future
}
delay(700); // Add a delay to slow down the shuffling
}
// Function to display all parameters live
void displayAllParameters() {
displaytemp();
displayheight();
displaytime();
}