From 754ef45cba863b6d088876fcb8abab725a504148 Mon Sep 17 00:00:00 2001 From: Ken Kahn Date: Fri, 30 Aug 2024 18:02:36 +0100 Subject: [PATCH] improved 4o breathing app --- apps/breathing/4o/script.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/apps/breathing/4o/script.js b/apps/breathing/4o/script.js index 10b9bb7..bd88e51 100644 --- a/apps/breathing/4o/script.js +++ b/apps/breathing/4o/script.js @@ -1,4 +1,3 @@ - let isMonitoring = false; let breathCount = 0; let lastBreathTime = Date.now(); @@ -22,12 +21,15 @@ function stopMonitoring() { 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 + const threshold = 1.0; // Increased threshold to reduce false positives + const minTimeBetweenBreaths = 2000; // Minimum time in milliseconds between breaths (e.g., 2 seconds) + + const currentTime = Date.now(); + const timeDiff = currentTime - lastBreathTime; - if (Math.abs(acceleration) > threshold) { - const currentTime = Date.now(); - const timeDiff = (currentTime - lastBreathTime) / 1000 / 60; // time difference in minutes - breathRates.push(1 / timeDiff); + if (Math.abs(acceleration) > threshold && timeDiff > minTimeBetweenBreaths) { + const timeDiffMinutes = timeDiff / 1000 / 60; // Convert time difference to minutes + breathRates.push(1 / timeDiffMinutes); breathCount++; lastBreathTime = currentTime; calculateBreathingRate();