peony3000

Gadgeteer Accelerometer Slap Detect

Apr 10th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using Microsoft.SPOT;
  4. using Microsoft.SPOT.Presentation;
  5. using Microsoft.SPOT.Presentation.Controls;
  6. using Microsoft.SPOT.Presentation.Media;
  7. using Microsoft.SPOT.Touch;
  8.  
  9. using Gadgeteer.Networking;
  10. using GT = Gadgeteer;
  11. using GTM = Gadgeteer.Modules;
  12. using Gadgeteer.Modules.GHIElectronics;
  13. using Gadgeteer.Modules.Seeed;
  14.  
  15. namespace Gadgeteer_Accelerometer_01
  16. {
  17.  
  18.     public partial class Program
  19.     {
  20.  
  21.         GT.Timer timer = new GT.Timer(1000, GT.Timer.BehaviorType.RunOnce);
  22.  
  23.         void ProgramStarted()
  24.         {
  25.             //
  26.             accelerometer.ThresholdExceeded += new GTM.Seeed.Accelerometer.ThresholdExceededEventHandler(accelerometer_ThresholdExceeded);
  27.             accelerometer.MeasurementRange = GTM.Seeed.Accelerometer.Range.TwoG;
  28.            
  29.             // steup a timer to wait for the reset action
  30.             timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
  31.  
  32.             // set the calibration point (the device shouldn't be moving!)
  33.             accelerometer.Calibrate();
  34.             //accelerometer.SaveCalibration();
  35.             //accelerometer.LoadCalibration();
  36.  
  37.             accelerometer.EnableThresholdDetection(-3.0, false, false, true, true, false, false);
  38.  
  39.             // continuous accelerometer measuring
  40.             //accelerometer.MeasurementComplete += new Accelerometer.MeasurementCompleteEventHandler(accelerometer_MeasurementComplete);
  41.             //accelerometer.ContinuousMeasurementInterval = new TimeSpan(0, 0, 0, 0, 300); // 300 ms
  42.             //accelerometer.StartContinuousMeasurements();
  43.  
  44.             // let everyone know
  45.             Debug.Print("Program Started");    
  46.         }
  47.  
  48.         void accelerometer_ThresholdExceeded(GTM.Seeed.Accelerometer sender)
  49.         {
  50.             Debug.Print("Threshold exceeded!");
  51.             // start the timer to reset the accelerometer when it has settled again
  52.             timer.Start();
  53.         }
  54.        
  55.         void timer_Tick(GT.Timer timer)
  56.         {
  57.             accelerometer.ResetThresholdDetection();
  58.             Debug.Print("Threshold reset and waiting....");
  59.             // stop the timer just in case
  60.             timer.Stop();
  61.         }
  62.  
  63.         // use this event handeler for continuous readings
  64.         void accelerometer_MeasurementComplete(GTM.Seeed.Accelerometer sender, GTM.Seeed.Accelerometer.Acceleration acceleration)
  65.         {
  66.             double mag = acceleration.X * acceleration.X + acceleration.Y * acceleration.Y + acceleration.Z * acceleration.Z;
  67.             Debug.Print(acceleration.ToString() + " " + mag.ToString());          
  68.         }
  69.  
  70.        
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment