-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathE1_100_12.2.ino
89 lines (82 loc) · 1.66 KB
/
E1_100_12.2.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <Wire.h>
#include <stdlib.h>
#define aPulse1 2
#define aPulse2 3
volatile double pulse1, count = 0, pulse2, count2;
long interval;
int flag = 1, flag1 = 0,i=0,a,d;
long int distance1 = 0, distance2 = 0;
unsigned long t;
float r = 4.3;
int dist[5] = {0 , 136, 120, 165, 58};
void setup() {
Wire.begin(8);
Serial.begin(9600);// join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
pinMode(aPulse1, INPUT_PULLUP);
pinMode(aPulse2, INPUT_PULLUP);
}
void encoderA()
{
pulse1++;
}
void encoderB()
{
pulse2++;
}
void d1()
{
attachInterrupt(digitalPinToInterrupt(aPulse1), encoderA, RISING);
count = pulse1;
distance1 = 2 * 3.14 * r * count / 600;
d = distance1;
}
void d2()
{
attachInterrupt(digitalPinToInterrupt(aPulse2), encoderB, RISING);
count2 = pulse2;
distance2 = 2 * 3.14 * r * count2 / 600;
d = distance2;
}
void loop() {
Serial.print(distance1);
Serial.print("\t");
Serial.println(distance2);
if ((d > dist[i]) || (d == dist[i]))
{
i++;
if (( i % 2) == 0 )
a = 2 ;
else
a = 1;
}
if (a == 1)
{
distance2 = 0;
pulse2 = 0;
flag = 1;
detachInterrupt(digitalPinToInterrupt(aPulse2));
d1();
}
{
if (a == 2)
{
pulse1 = 0;
distance1 = 0;
detachInterrupt(digitalPinToInterrupt(aPulse1));
d2();
}
}
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
if (a == 1)
{
Wire.write(distance1);
}
if (a ==2)
{
Wire.write(distance2);
}
}