I am curious if there is a way to detect if a user of a website is climbing up or descending stairs using only html5 and JavaScript and mobile device sensors? I am not sure how a function could interpret accelerometer and orientation data and extrapolate that the user is walking up or down a flight of stairs. The article linked below does show how to get sensor data from the mobile device using JavaScript and HTML, but I am looking for a function that can detect stair climbing. Below is the related question: JavaScript tilt sensor
This is a general question, I don't know where to start for a minimal function example.
When ascending stairs, vertical acceleration (Z-axis) shows a repeating up-down spike pattern every step, usually with increasing Z acceleration.
Descending also shows spikes, but with slightly different force and timing.
Orientation (pitch/roll) may help to determine if the phone is tilted forward while climbing, or downward when descending. HTML:
<script>
window.addEventListener('devicemotion', function(event) {
const acc = event.accelerationIncludingGravity;
console.log(`X: ${acc.x}, Y: ${acc.y}, Z: ${acc.z}`);
});
</script>
JS:
let accZHistory = [];
window.addEventListener('devicemotion', function(event) {
const accZ = event.accelerationIncludingGravity.z;
accZHistory.push(accZ);
if (accZHistory.length > 50) accZHistory.shift();
// Check for repeating peaks (simple peak detection)
let peaks = 0;
for (let i = 1; i < accZHistory.length - 1; i++) {
if (accZHistory[i] > accZHistory[i - 1] && accZHistory[i] > accZHistory[i + 1]) {
peaks++;
}
}
if (peaks >= 3) {
console.log("Possible stair movement detected!");
}
});