df

Dependencies:   MPU6050 mbed

Committer:
mcthemax
Date:
Mon Jan 25 13:23:04 2016 +0000
Revision:
0:34bcf968b641
d

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mcthemax 0:34bcf968b641 1 #include "mbed.h"
mcthemax 0:34bcf968b641 2 #include "MPU6050.h"
mcthemax 0:34bcf968b641 3
mcthemax 0:34bcf968b641 4 //Serial pc(p28, p27);
mcthemax 0:34bcf968b641 5 Serial pc(USBTX, USBRX);
mcthemax 0:34bcf968b641 6 DigitalOut buzzer(p18);
mcthemax 0:34bcf968b641 7
mcthemax 0:34bcf968b641 8 int main() {
mcthemax 0:34bcf968b641 9
mcthemax 0:34bcf968b641 10 pc.baud(57600);
mcthemax 0:34bcf968b641 11
mcthemax 0:34bcf968b641 12 int16_t acc[3];
mcthemax 0:34bcf968b641 13 int presum,sum;
mcthemax 0:34bcf968b641 14 int trigger = 0;
mcthemax 0:34bcf968b641 15 int trigger2 = 0;
mcthemax 0:34bcf968b641 16
mcthemax 0:34bcf968b641 17 unsigned char value = 0;
mcthemax 0:34bcf968b641 18
mcthemax 0:34bcf968b641 19 buzzer = 0;
mcthemax 0:34bcf968b641 20
mcthemax 0:34bcf968b641 21 MPU6050 mpu;
mcthemax 0:34bcf968b641 22 mpu.init();
mcthemax 0:34bcf968b641 23
mcthemax 0:34bcf968b641 24 while(1) {
mcthemax 0:34bcf968b641 25
mcthemax 0:34bcf968b641 26 wait(0.05);
mcthemax 0:34bcf968b641 27
mcthemax 0:34bcf968b641 28 pc.printf("%c%c%c%c%c",0xff,0xff,0x00,value,0xfe);
mcthemax 0:34bcf968b641 29 mpu.readAccelData(acc);
mcthemax 0:34bcf968b641 30 rand();
mcthemax 0:34bcf968b641 31 acc[0]/=100;
mcthemax 0:34bcf968b641 32 acc[1]/=100;
mcthemax 0:34bcf968b641 33 acc[2]/=100;
mcthemax 0:34bcf968b641 34
mcthemax 0:34bcf968b641 35 sum = (acc[0]*acc[0] + acc[1]*acc[1] + acc[2]*acc[2])/100000;
mcthemax 0:34bcf968b641 36
mcthemax 0:34bcf968b641 37 if(sum>0)
mcthemax 0:34bcf968b641 38 trigger++;
mcthemax 0:34bcf968b641 39 else
mcthemax 0:34bcf968b641 40 trigger = 0;
mcthemax 0:34bcf968b641 41
mcthemax 0:34bcf968b641 42 if(trigger == 6)
mcthemax 0:34bcf968b641 43 {
mcthemax 0:34bcf968b641 44 //pc.printf("%10d%10d%10d%10d \n\r",acc[0],acc[1],acc[2],sum);
mcthemax 0:34bcf968b641 45 pc.printf("%c%c%c%c%c",0xff,0xff,0x01,value,0xfe);
mcthemax 0:34bcf968b641 46 buzzer = 1;
mcthemax 0:34bcf968b641 47 wait(3);
mcthemax 0:34bcf968b641 48
mcthemax 0:34bcf968b641 49 while(1) {
mcthemax 0:34bcf968b641 50
mcthemax 0:34bcf968b641 51 wait(0.05);
mcthemax 0:34bcf968b641 52 mpu.readAccelData(acc);
mcthemax 0:34bcf968b641 53 rand();
mcthemax 0:34bcf968b641 54 acc[0]/=100;
mcthemax 0:34bcf968b641 55 acc[1]/=100;
mcthemax 0:34bcf968b641 56 acc[2]/=100;
mcthemax 0:34bcf968b641 57
mcthemax 0:34bcf968b641 58 presum = sum;
mcthemax 0:34bcf968b641 59 sum = (acc[0]*acc[0] + acc[1]*acc[1] + acc[2]*acc[2]);
mcthemax 0:34bcf968b641 60
mcthemax 0:34bcf968b641 61 if(abs(presum-sum)<1000)
mcthemax 0:34bcf968b641 62 trigger2++;
mcthemax 0:34bcf968b641 63 else
mcthemax 0:34bcf968b641 64 trigger2 = 0;
mcthemax 0:34bcf968b641 65
mcthemax 0:34bcf968b641 66 if(trigger2 == 10)
mcthemax 0:34bcf968b641 67 {
mcthemax 0:34bcf968b641 68 value = 0;
mcthemax 0:34bcf968b641 69 if(acc[0]>100)
mcthemax 0:34bcf968b641 70 value += 0x01;
mcthemax 0:34bcf968b641 71 else if(acc[0]<-100)
mcthemax 0:34bcf968b641 72 value += 0x06;
mcthemax 0:34bcf968b641 73 else if(acc[1]>100)
mcthemax 0:34bcf968b641 74 value += 0x02;
mcthemax 0:34bcf968b641 75 else if(acc[1]<-100)
mcthemax 0:34bcf968b641 76 value += 0x05;
mcthemax 0:34bcf968b641 77 else if(acc[2]>100)
mcthemax 0:34bcf968b641 78 value += 0x03;
mcthemax 0:34bcf968b641 79 else if(acc[2]<-100)
mcthemax 0:34bcf968b641 80 value += 0x04;
mcthemax 0:34bcf968b641 81 if(rand()%2==1)
mcthemax 0:34bcf968b641 82 value += 0x10;
mcthemax 0:34bcf968b641 83
mcthemax 0:34bcf968b641 84 pc.printf("%c%c%c%c%c",0xff,0xff,0x01,value,0xfe);
mcthemax 0:34bcf968b641 85 // pc.printf("value = %d\n\r",value);
mcthemax 0:34bcf968b641 86 buzzer = 0;
mcthemax 0:34bcf968b641 87 break;
mcthemax 0:34bcf968b641 88 }
mcthemax 0:34bcf968b641 89 }
mcthemax 0:34bcf968b641 90
mcthemax 0:34bcf968b641 91 }
mcthemax 0:34bcf968b641 92
mcthemax 0:34bcf968b641 93 }
mcthemax 0:34bcf968b641 94 }