/*
NO SLEEP TIL DEMO
Detects the absence of movent (user fell asleep)
then blinks and sends vibration signal to
*/
// When acceleration change goes beyond this threshold, the blink.
#define THRESHOLD 50
AccelerationReading previousAccel;
void setup() {
// Turn off the Bean's LED
Bean.setLed(0, 0, 0);
// Initial reading
previousAccel = Bean.getAcceleration();
}
// This function calculates the difference between two acceleration readings
int getAccelDifference(AccelerationReading readingOne, AccelerationReading readingTwo){
int deltaX = abs(readingTwo.xAxis - readingOne.xAxis);
int deltaY = abs(readingTwo.yAxis - readingOne.yAxis);
int deltaZ = abs(readingTwo.zAxis - readingOne.zAxis);
// Return the magnitude
return deltaX + deltaY + deltaZ;
}
void loop() {
// Get the current acceleration with a conversion of 3.9110-3 g/unit.
AccelerationReading currentAccel = Bean.getAcceleration();
// Find the difference between the current acceleration and that of 200ms ago.
int accelDifference = getAccelDifference(previousAccel, currentAccel);
// Update previousAccel for the next loop.
previousAccel = currentAccel;
// Check if the Bean has been moved beyond our threshold.
if(accelDifference > THRESHOLD){
// Blink the LED
Bean.sleep(1900);
}
else{
Bean.setLed(255,0,0);
Bean.sleep(10);
Bean.setLed(0,255,0);
Bean.sleep(10);
Bean.setLed(0,0,255);
Bean.sleep(10);
Bean.setLed(255,100,100);
Bean.sleep(10);
Bean.setLed(0,0,0);
Bean.sleep(10);
}
}
Comments