1#include<Wire.h>
2long accelx,accely,accelz;
3float gforcex,gforcey,gforcez;
4int irsensorbackward=A0;
5
6int pin13=12;
7int pin12=10;
8int pin11=8;
9int pin10=7;
10int value;
11
12void setup()
13{
14 Serial.begin(9600);
15 Wire.begin();
16
17 pinMode(irsensorbackward,INPUT);
18 pinMode(pin13,OUTPUT);
19 pinMode(pin12,OUTPUT);
20 pinMode(pin11,OUTPUT);
21 pinMode(pin10,OUTPUT);
22
23 setupmpu();
24}
25void loop()
26{
27 value=analogRead(irsensorbackward);
28 accelregistervalue();
29
30
31Serial.println(value);
32
33
34 if(value > 255)
35 {
36 if(gforcey>-0.39 || gforcey<0.39)
37 {
38 digitalWrite(pin13,LOW);
39 digitalWrite(pin12,HIGH);
40 digitalWrite(pin11,LOW);
41 digitalWrite(pin10,HIGH);
42 }
43 if(gforcey<-0.40)
44 {
45 digitalWrite(pin13,LOW);
46 digitalWrite(pin12,HIGH);
47 digitalWrite(pin11,HIGH);
48 digitalWrite(pin10,HIGH);
49 }
50 if(gforcey>0.40)
51 {
52
53 digitalWrite(pin13,HIGH);
54 digitalWrite(pin12,HIGH);
55 digitalWrite(pin11,LOW);
56 digitalWrite(pin10,HIGH);
57 }
58 }
59
60 if(value<255)
61 {
62
63 digitalWrite(pin13,HIGH);
64 digitalWrite(pin12,HIGH);
65 digitalWrite(pin11,HIGH);
66 digitalWrite(pin10,HIGH);
67
68 }
69}
70void setupmpu()
71{
72 Wire.beginTransmission(0b1101000);
73 Wire.write(0x6B);
74 Wire.write(0b00000000);
75 Wire.endTransmission();
76
77
78 Wire.beginTransmission(0b1101000);
79 Wire.write(0x1c);
80 Wire.write(0b00000000);
81 Wire.endTransmission();
82}
83void accelregistervalue()
84{
85 Wire.beginTransmission(0b1101000);
86 Wire.write(0x3B);
87 Wire.endTransmission();
88 Wire.requestFrom(0b1101000,6);
89 while(Wire.available()<6);
90 accelx=Wire.read()<<8| Wire.read();
91 accely=Wire.read()<<8 | Wire.read();
92 accelz=Wire.read()<<8 | Wire.read();
93 gforcex=accelx/16384.0;
94 gforcey=accely/16384.0;
95 gforcez=accelz/16384.0;
96}
97
98