Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define X 1
- #define Y 2
- #define Z 3
- //#define FACEPLANT 2300 // Values from SparkCore demo
- //#define UPSIDE_DOWN 1850
- //#define RIGHTSIDE_UP 2400
- //#define LEFT_SIDE 1800
- //#define RIGHT_SIDE 2400
- #define FACEPLANT 600 // Observed from Ardiono Mega
- #define UPSIDE_DOWN 410
- #define RIGHTSIDE_UP 600
- #define LEFT_SIDE 400
- #define RIGHT_SIDE 600
- #define BACK_SIDE 400
- #define FLIP_TIMEOUT 3000
- #define FLIP_DEBOUNCE 250
- #define MICROPHONE 0
- #define GAIN_CONTROL 3
- #define THRESHOLD 512
- typedef enum {NOTKNOWN, FACEUP, LSD, RSD, BSD, USD, FSD} ORIENTATION;
- ORIENTATION currentOrientation = NOTKNOWN;
- long lastFaceplant=-1*FLIP_TIMEOUT;
- bool upsideDown=false;
- bool sideways=false;
- int upsideDownTime=-1*FLIP_TIMEOUT;
- int lastLeft=-1*FLIP_TIMEOUT;
- int lastRight=-1*FLIP_TIMEOUT;
- int accelerometer[3];
- long lastChange=0;
- void initMicrophone()
- {
- pinMode(GAIN_CONTROL, OUTPUT);
- digitalWrite(GAIN_CONTROL, LOW);
- pinMode(MICROPHONE,INPUT);
- }
- void initAccelerometer()
- {
- pinMode(X,INPUT);
- pinMode(Y,INPUT);
- pinMode(Z,INPUT);
- delay(50);
- updateAccelerometer(); // First read appears to be dirty, discard
- delay(50);
- }
- void checkMicrophone()
- {
- int mic=analogRead(MICROPHONE);
- if (mic > THRESHOLD)
- {
- Serial.print("Heard something on the mic: ");
- Serial.println(mic);
- }
- }
- void updateAccelerometer()
- {
- for(int i=0;i<3;i++)
- {
- accelerometer[i]=analogRead(X+i);
- }
- // Serial.print("Accel [");
- // for(int i=0;i<3;i++)
- // {
- // Serial.print(accelerometer[i]);
- // if (i<2) Serial.print(", ");
- // }
- // Serial.println("]");
- }
- void printOrientation(ORIENTATION state);
- void printOrientation(ORIENTATION state)
- {
- switch (state)
- {
- case NOTKNOWN: Serial.print("Unknown"); break;
- case FACEUP: Serial.print("Right side up"); break;
- case LSD: Serial.print("Left side down"); break;
- case RSD: Serial.print("Right side down"); break;
- case BSD: Serial.print("Back side down"); break;
- case FSD: Serial.print("Front side down"); break;
- case USD: Serial.print("Upside down"); break;
- default: Serial.print("Illegal state"); break;
- }
- }
- void checkFlipState()
- {
- updateAccelerometer();
- ORIENTATION newState = NOTKNOWN;
- if(accelerometer[0]>FACEPLANT)
- {
- lastFaceplant=millis();
- newState = FSD;
- }
- if(accelerometer[1]<LEFT_SIDE)
- {
- lastLeft=millis();
- newState = LSD;
- }
- if(accelerometer[1]>RIGHT_SIDE)
- {
- lastRight=millis();
- newState = RSD;
- }
- if(accelerometer[2]<UPSIDE_DOWN)
- {
- newState = USD;
- }
- if(accelerometer[0]<BACK_SIDE)
- {
- newState = BSD;
- }
- if(accelerometer[2]>RIGHTSIDE_UP)
- {
- newState = FACEUP;
- if(((millis()-lastFaceplant)<FLIP_TIMEOUT)&&(millis()-lastFaceplant>FLIP_DEBOUNCE))
- {
- lastFaceplant=millis()-FLIP_TIMEOUT;
- }
- if(((millis()-lastLeft)<FLIP_TIMEOUT)&&(millis()-lastChange>FLIP_DEBOUNCE))
- {
- //Serial.println("turned to the left and back");
- lastChange=millis();
- lastLeft=millis()-FLIP_TIMEOUT;
- }
- if(((millis()-lastRight)<FLIP_TIMEOUT)&&(millis()-lastChange>FLIP_DEBOUNCE))
- {
- //Serial.println("turned to the right and back");
- lastChange=millis();
- lastRight=millis()-FLIP_TIMEOUT;
- }
- }
- if ((newState != currentOrientation) && (newState != NOTKNOWN))
- {
- if (currentOrientation != NOTKNOWN) // Should only happen first time
- {
- Serial.print("Orientation state change from: ");
- printOrientation(currentOrientation);
- Serial.print(" to: ");
- printOrientation(newState);
- Serial.println();
- }
- currentOrientation=newState;
- }
- }
- void setup() {
- Serial.begin(9600); // setup serial
- delay(100);
- initAccelerometer();
- initMicrophone();
- }
- void loop() {
- checkFlipState();
- checkMicrophone();
- delay(20);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement