-
Notifications
You must be signed in to change notification settings - Fork 4
/
water_pump.c
96 lines (87 loc) · 1.74 KB
/
water_pump.c
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
90
91
92
93
94
95
96
/*
RB0 is Trig Pin
RB1 is Echo pin
RB2 is green led pin
RB3 is yellow led pin
RB4 is red led pin
RB5 is for motor
*/
int findDistance()
{
int distance1 = 0;
TMR1L = 0x0;
TMR1H = 0x0;
RB0_BIT = 1;
delay_us(10);
RB0_BIT = 0;
T1CON.F0 = 1;
while(RB1_BIT);
T1CON.F0 = 0;
distance1 = (TMR1L | (TMR1H<<8));
distance1 = (distance1*0.034)/2;
return distance1;
}
void main() {
int distance = 0;
TRISB = 0x02;
PORTB = 0x0;
TMR1L = 0x0;
TMR1H = 0x0;
while(1)
{
distance = findDistance();
if(distance >= 30)
{
RB2_BIT = 0;
RB3_BIT = 0;
RB4_BIT = 1;
RB5_BIT = 1;
while(distance > 10)
{
if(distance >= 30)
{
RB2_BIT = 0;
RB3_BIT = 0;
RB4_BIT = 1;
RB5_BIT = 1;
}
else if(distance >= 20 && distance <= 29)
{
RB2_BIT = 0;
RB3_BIT = 1;
RB4_BIT = 1;
RB5_BIT = 1;
}
else if(distance >=11 && distance <=19)
{
RB2_BIT = 1;
RB3_BIT = 1;
RB4_BIT = 1;
RB5_BIT = 1;
}
else if(distance <=10 )
{
RB2_BIT = 1;
RB3_BIT = 1;
RB4_BIT = 1;
RB5_BIT = 0;
}
distance = findDistance();
}
}
else if(distance >= 20 && distance <= 29)
{
RB2_BIT = 0;
RB3_BIT = 1;
RB4_BIT = 1;
RB5_BIT = 0;
}
else if(distance >=10 && distance <=19)
{
RB2_BIT = 1;
RB3_BIT = 1;
RB4_BIT = 1;
RB5_BIT = 0;
}
}
}