-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Breathing Rate Monitor</title> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
text-align: center; | ||
padding: 20px; | ||
} | ||
#rate { | ||
font-size: 2em; | ||
margin: 20px 0; | ||
} | ||
#startStopBtn { | ||
padding: 10px 20px; | ||
font-size: 1em; | ||
cursor: pointer; | ||
} | ||
#advice { | ||
margin-top: 20px; | ||
font-size: 1.2em; | ||
color: #2d89ef; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Breathing Rate Monitor</h1> | ||
<p id="rate">Breaths per Minute: 0</p> | ||
<button id="startStopBtn">Start</button> | ||
<p id="advice"></p> | ||
|
||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
|
||
let isMonitoring = false; | ||
let breathCount = 0; | ||
let lastBreathTime = Date.now(); | ||
let breathRates = []; | ||
|
||
const startStopBtn = document.getElementById('startStopBtn'); | ||
const rateDisplay = document.getElementById('rate'); | ||
const adviceDisplay = document.getElementById('advice'); | ||
|
||
function startMonitoring() { | ||
breathCount = 0; | ||
breathRates = []; | ||
lastBreathTime = Date.now(); | ||
window.addEventListener('devicemotion', detectBreathing); | ||
} | ||
|
||
function stopMonitoring() { | ||
window.removeEventListener('devicemotion', detectBreathing); | ||
calculateBreathingRate(); | ||
} | ||
|
||
function detectBreathing(event) { | ||
const acceleration = event.acceleration.y; // Assuming the phone is vertical in the pocket | ||
const threshold = 0.5; // Adjust this value based on testing | ||
|
||
if (Math.abs(acceleration) > threshold) { | ||
const currentTime = Date.now(); | ||
const timeDiff = (currentTime - lastBreathTime) / 1000 / 60; // time difference in minutes | ||
breathRates.push(1 / timeDiff); | ||
breathCount++; | ||
lastBreathTime = currentTime; | ||
calculateBreathingRate(); | ||
} | ||
} | ||
|
||
function calculateBreathingRate() { | ||
if (breathRates.length > 0) { | ||
const averageBreathingRate = breathRates.reduce((a, b) => a + b, 0) / breathRates.length; | ||
rateDisplay.textContent = `Breaths per Minute: ${Math.round(averageBreathingRate)}`; | ||
giveAdvice(averageBreathingRate); | ||
} | ||
} | ||
|
||
function giveAdvice(breathingRate) { | ||
let advice = ""; | ||
if (breathingRate > 20) { | ||
advice = "Your breathing seems rapid. Try taking deep, slow breaths."; | ||
} else if (breathingRate < 12) { | ||
advice = "Your breathing is quite slow. Consider taking deeper breaths."; | ||
} else { | ||
advice = "Your breathing rate is normal."; | ||
} | ||
adviceDisplay.textContent = advice; | ||
} | ||
|
||
startStopBtn.addEventListener('click', () => { | ||
isMonitoring = !isMonitoring; | ||
startStopBtn.textContent = isMonitoring ? "Stop" : "Start"; | ||
if (isMonitoring) { | ||
startMonitoring(); | ||
} else { | ||
stopMonitoring(); | ||
} | ||
}); |