Skip to content

Commit

Permalink
breathing by 4o
Browse files Browse the repository at this point in the history
  • Loading branch information
ToonTalk committed Aug 30, 2024
1 parent 7d00ed4 commit 2907e93
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
38 changes: 38 additions & 0 deletions apps/breathing/4o/index.html
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>
65 changes: 65 additions & 0 deletions apps/breathing/4o/script.js
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();
}
});

0 comments on commit 2907e93

Please sign in to comment.