Advertisement
pchestna

L3D Cube Mic and Accel test

Dec 20th, 2014
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #define X 1
  2. #define Y 2
  3. #define Z 3
  4. //#define FACEPLANT 2300    // Values from SparkCore demo
  5. //#define UPSIDE_DOWN 1850
  6. //#define RIGHTSIDE_UP 2400
  7. //#define LEFT_SIDE 1800
  8. //#define RIGHT_SIDE 2400
  9. #define FACEPLANT 600        // Observed from Ardiono Mega
  10. #define UPSIDE_DOWN 410
  11. #define RIGHTSIDE_UP 600
  12. #define LEFT_SIDE 400
  13. #define RIGHT_SIDE 600
  14. #define BACK_SIDE 400
  15. #define FLIP_TIMEOUT 3000
  16. #define FLIP_DEBOUNCE 250
  17. #define MICROPHONE 0
  18. #define GAIN_CONTROL 3
  19. #define THRESHOLD 512
  20.  
  21. typedef enum {NOTKNOWN, FACEUP, LSD, RSD, BSD, USD, FSD} ORIENTATION;
  22. ORIENTATION currentOrientation = NOTKNOWN;
  23. long lastFaceplant=-1*FLIP_TIMEOUT;
  24. bool upsideDown=false;
  25. bool sideways=false;
  26. int upsideDownTime=-1*FLIP_TIMEOUT;
  27. int lastLeft=-1*FLIP_TIMEOUT;
  28. int lastRight=-1*FLIP_TIMEOUT;
  29. int accelerometer[3];
  30. long lastChange=0;
  31.  
  32. void initMicrophone()
  33. {
  34.   pinMode(GAIN_CONTROL, OUTPUT);
  35.   digitalWrite(GAIN_CONTROL, LOW);
  36.   pinMode(MICROPHONE,INPUT);
  37. }
  38.  
  39. void initAccelerometer()
  40. {
  41.   pinMode(X,INPUT);
  42.   pinMode(Y,INPUT);
  43.   pinMode(Z,INPUT);
  44.   delay(50);
  45.   updateAccelerometer(); // First read appears to be dirty, discard
  46.   delay(50);
  47. }
  48.  
  49. void checkMicrophone()
  50. {
  51.     int mic=analogRead(MICROPHONE);
  52.     if (mic > THRESHOLD)
  53.     {
  54.       Serial.print("Heard something on the mic: ");
  55.       Serial.println(mic);
  56.     }
  57. }
  58.  
  59. void updateAccelerometer()
  60. {
  61.     for(int i=0;i<3;i++)
  62.     {
  63.         accelerometer[i]=analogRead(X+i);
  64.     }
  65.  
  66. //    Serial.print("Accel [");
  67. //    for(int i=0;i<3;i++)
  68. //    {
  69. //        Serial.print(accelerometer[i]);
  70. //        if (i<2) Serial.print(", ");
  71. //    }
  72. //    Serial.println("]");
  73. }
  74.  
  75. void printOrientation(ORIENTATION state);
  76. void printOrientation(ORIENTATION state)
  77. {
  78.   switch (state)
  79.   {
  80.     case NOTKNOWN: Serial.print("Unknown"); break;
  81.     case FACEUP: Serial.print("Right side up"); break;
  82.     case LSD: Serial.print("Left side down"); break;
  83.     case RSD: Serial.print("Right side down"); break;
  84.     case BSD: Serial.print("Back side down"); break;
  85.     case FSD: Serial.print("Front side down"); break;
  86.     case USD: Serial.print("Upside down"); break;
  87.     default: Serial.print("Illegal state"); break;
  88.   }
  89. }
  90.  
  91. void checkFlipState()
  92.  {
  93.     updateAccelerometer();
  94.     ORIENTATION newState = NOTKNOWN;
  95.    
  96.     if(accelerometer[0]>FACEPLANT)  
  97.     {
  98.         lastFaceplant=millis();
  99.         newState = FSD;
  100.     }
  101.     if(accelerometer[1]<LEFT_SIDE)
  102.     {
  103.         lastLeft=millis();
  104.         newState = LSD;
  105.     }
  106.     if(accelerometer[1]>RIGHT_SIDE)
  107.     {
  108.         lastRight=millis();
  109.         newState = RSD;
  110.     }
  111.     if(accelerometer[2]<UPSIDE_DOWN)
  112.     {
  113.         newState = USD;
  114.     }
  115.     if(accelerometer[0]<BACK_SIDE)
  116.     {
  117.         newState = BSD;
  118.     }
  119.  
  120.     if(accelerometer[2]>RIGHTSIDE_UP)
  121.     {
  122.         newState = FACEUP;
  123.         if(((millis()-lastFaceplant)<FLIP_TIMEOUT)&&(millis()-lastFaceplant>FLIP_DEBOUNCE))
  124.         {
  125.             lastFaceplant=millis()-FLIP_TIMEOUT;
  126.         }
  127.         if(((millis()-lastLeft)<FLIP_TIMEOUT)&&(millis()-lastChange>FLIP_DEBOUNCE))
  128.         {
  129.             //Serial.println("turned to the left and back");
  130.             lastChange=millis();
  131.             lastLeft=millis()-FLIP_TIMEOUT;
  132.         }
  133.         if(((millis()-lastRight)<FLIP_TIMEOUT)&&(millis()-lastChange>FLIP_DEBOUNCE))
  134.         {
  135.             //Serial.println("turned to the right and back");
  136.             lastChange=millis();
  137.             lastRight=millis()-FLIP_TIMEOUT;
  138.         }
  139.     }
  140.     if ((newState != currentOrientation) && (newState != NOTKNOWN))
  141.     {
  142.       if (currentOrientation != NOTKNOWN) // Should only happen first time
  143.       {
  144.         Serial.print("Orientation state change from: ");
  145.         printOrientation(currentOrientation);
  146.         Serial.print(" to: ");
  147.         printOrientation(newState);
  148.         Serial.println();
  149.       }
  150.       currentOrientation=newState;
  151.     }
  152.  }
  153.  
  154. void setup() {
  155.   Serial.begin(9600);          //  setup serial
  156.   delay(100);
  157.   initAccelerometer();
  158.   initMicrophone();
  159. }
  160.  
  161. void loop() {
  162.   checkFlipState();
  163.   checkMicrophone();
  164.   delay(20);
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement