df

Dependencies:   MPU6050 mbed

main.cpp

Committer:
mcthemax
Date:
2016-01-25
Revision:
0:34bcf968b641

File content as of revision 0:34bcf968b641:

#include "mbed.h"
#include "MPU6050.h"

//Serial pc(p28, p27);
Serial pc(USBTX, USBRX);
DigitalOut buzzer(p18);

int main() {
    
    pc.baud(57600);
    
    int16_t acc[3];
    int presum,sum;
    int trigger = 0;
     int trigger2 = 0;
    
    unsigned char value = 0;
    
    buzzer = 0;
    
    MPU6050 mpu;
    mpu.init();
    
    while(1) {
     
        wait(0.05);
        
        pc.printf("%c%c%c%c%c",0xff,0xff,0x00,value,0xfe);
        mpu.readAccelData(acc);
        rand();
        acc[0]/=100;
        acc[1]/=100;
        acc[2]/=100;
        
        sum = (acc[0]*acc[0] + acc[1]*acc[1] + acc[2]*acc[2])/100000;
        
        if(sum>0)
            trigger++;
        else
            trigger = 0;
            
        if(trigger == 6)
        {
            //pc.printf("%10d%10d%10d%10d  \n\r",acc[0],acc[1],acc[2],sum);
        pc.printf("%c%c%c%c%c",0xff,0xff,0x01,value,0xfe);
        buzzer = 1;
        wait(3);
        
         while(1) {
      
        wait(0.05);
        mpu.readAccelData(acc);
        rand();
        acc[0]/=100;
        acc[1]/=100;
        acc[2]/=100;
        
        presum = sum;
        sum = (acc[0]*acc[0] + acc[1]*acc[1] + acc[2]*acc[2]);
        
        if(abs(presum-sum)<1000)
            trigger2++;
         else
            trigger2 = 0;   
        
        if(trigger2 == 10)
        {
            value = 0;
            if(acc[0]>100)
                value += 0x01;
            else if(acc[0]<-100)
                value += 0x06;
            else if(acc[1]>100)
                value += 0x02;
            else if(acc[1]<-100)
                value += 0x05;
            else if(acc[2]>100)
                value += 0x03;
            else if(acc[2]<-100)
                value += 0x04;
            if(rand()%2==1)
                value += 0x10;

           pc.printf("%c%c%c%c%c",0xff,0xff,0x01,value,0xfe);     
           // pc.printf("value = %d\n\r",value);
            buzzer = 0;
            break;
        }
         }
        
       }
        
    }
}