Student starter code (30% baseline)
index.html- Main HTML pagescript.js- JavaScript logicstyles.css- Styling and layoutpackage.json- Dependenciessetup.sh- Setup scriptREADME.md- Instructions (below)💡 Download the ZIP, extract it, and follow the instructions below to get started!
Welcome to the Motion Fun discovery challenge! This is a working Expo app that's 70% complete and ready for you to explore and enhance with motion detection features.
By completing this activity, you will:
Total Time: 1.5-2 hours Difficulty: Intermediate Concept: Concept 13 - Device Motion and Sensors
Important: This activity requires a physical device with accelerometer and gyroscope sensors. Web preview will not work for motion features.
# Navigate to this folder
cd activity-13-motion-fun
# Install all packages (this will take ~30 seconds)
npm install --legacy-peer-deps
# Start Expo
npx expo start
# You should see:
# ✓ Metro bundler ready
# ✓ QR code in terminal
Discover what's built and what you'll create:
activity-13-motion-fun/
├── App.js # Main entry (YOU'LL EDIT: TODOs 1-2)
├── package.json # Dependencies (100% complete)
└── README.md # This file
File: App.js (line ~22)
Time: 30 minutes
Open: App.js
Find: Line ~22 in accelerometer listener
Add this logic:
// Inside Accelerometer.addListener callback
const accelerometerSubscription = Accelerometer.addListener(data => {
setAccelerometerData(data);
// Detect shake: check if any axis exceeds threshold
const threshold = 1.5;
const isShaking =
Math.abs(data.x) > threshold ||
Math.abs(data.y) > threshold ||
Math.abs(data.z) > threshold;
if (isShaking) {
setShakeCount(prev => prev + 1);
}
});
How it works:
Test:
Common Mistakes:
Math.abs() -> ✅ Use absolute valuesprev => prev + 1 patternFile: App.js (line ~34)
Time: 45 minutes
Open: App.js
Find: Line ~34 in gyroscope listener
Add this logic:
// Inside Gyroscope.addListener callback
const gyroscopeSubscription = Gyroscope.addListener(data => {
setGyroscopeData(data);
// Map tilt to colors
let newColor = '#1f2937'; // Default gray
if (data.x > 0.3) {
newColor = '#ef4444'; // Red - tilted right
} else if (data.x < -0.3) {
newColor = '#3b82f6'; // Blue - tilted left
} else if (data.y > 0.3) {
newColor = '#eab308'; // Yellow - tilted forward
} else if (data.y < -0.3) {
newColor = '#22c55e'; // Green - tilted backward
}
setBackgroundColor(newColor);
});
How it works:
Understanding the Axes:
Test:
Common Mistakes:
setBackgroundColor() -> ✅ Update stateYour activity is complete when:
Core Concepts:
Key Skills:
Completed all TODOs? Push your motion skills further:
This template connects to:
Documentation:
Tutorials:
Troubleshooting:
When you're done:
Required:
Optional (extra credit):
Issue: "Sensor unavailable" error Solution: Use physical device, not web simulator
Issue: Shake counter increments too quickly Solution: Increase threshold from 1.5 to 2.0 or add debouncing
Issue: Background color flickers rapidly Solution: Increase gyroscope threshold from 0.3 to 0.5
Issue: Battery drains quickly during testing Solution: Increase update interval to 200ms or higher
Issue: App crashes on sensor subscription
Solution: Check expo-sensors is installed: npm install expo-sensors
🎉 Congratulations!
You've mastered device motion sensors! You now understand:
Next Steps:
Activity 13 Template | M1: Mobile Foundations | Expo SDK 54 | React Native 0.79.5