1#include <LiquidCrystal.h>
2const int rs = 13, en = 6, d4 = 4, d5 = 10, d6 = 9, d7 = 7;
3int trigPin1 = 2;
4int echoPin1 = 3;
5int led1 = 8;
6int trigPin2 = 12;
7int echoPin2 = 11;
8int loop_count = 0;
9int counter1 = 0;
10int counter2 = 0;
11int people_count = 0;
12int s1 = 0 , s2 = 0;
13int s1_prev = 0 , s2_prev = 0;
14int led2 = 5;
15
16void setup() {
17 Serial.begin(9600);
18 pinMode(led1, OUTPUT);
19 pinMode(trigPin1, OUTPUT);
20 pinMode(echoPin1, INPUT);
21 pinMode(trigPin2, OUTPUT);
22 pinMode(echoPin2, INPUT);
23 pinMode(led2, OUTPUT);
24
25 lcd.begin(16, 2);
26
27 lcd.print("No of People :");
28
29}
30
31void loop()
32{
33 long duration1, distance1, distance2, duration2;
34 digitalWrite(trigPin1,HIGH);
35 delayMicroseconds(1000);
36 digitalWrite(trigPin1, LOW);
37 duration1=pulseIn(echoPin1, HIGH);
38 distance1 =(duration1/2)/29.1;
39
40
41
42 delay(10);
43 digitalWrite(trigPin2,HIGH);
44 delayMicroseconds(1000);
45 digitalWrite(trigPin2, LOW);
46 duration2=pulseIn(echoPin2, HIGH);
47 distance2 =(duration2/2)/29.1;
48
49
50
51 delay(10);
52
53 if(distance1<=50)
54 {
55 digitalWrite(led1, HIGH);
56 s1 = 1;
57 }
58 else if(distance1>50)
59 {
60 digitalWrite(led1, LOW);
61 s1 = 0;
62 }
63 if (distance2<=50)
64 {
65 digitalWrite(led1, HIGH);
66 s2 = 1;
67 }
68 else if(distance2>50 )
69 {
70 digitalWrite(led1, LOW);
71 s2 = 0;
72 }
73 loop_count++;
74 if ((s1_prev ==0) && (s1 == 1))
75 {
76 counter1 = loop_count;
77 }
78 if ((s2_prev == 0) && (s2 == 1))
79 {
80 counter2 = loop_count;
81 }
82 s1_prev = s1;
83 s2_prev = s2;
84
85 if ((counter1>0) && (counter2>0))
86 {
87 if (counter1>counter2)
88 {
89 people_count ++;
90 }
91 else
92 {
93 people_count --;
94 }
95 if (people_count < 0)
96 {
97 people_count = 0;
98 }
99 if (people_count>0)
100 {
101 digitalWrite(led2, HIGH);
102 }
103 else
104 {
105 digitalWrite(led2, LOW);
106 }
107
108 Serial.print("Counter 1 :");
109 Serial.println(counter1);
110 Serial.print("Counter 2 :");
111 Serial.println(counter2);
112 Serial.print("Number of people : ");
113 Serial.println(people_count);
114 counter1 = 0;
115 counter2 = 0;
116 }
117
118 lcd.setCursor(0, 1);
119
120 lcd.print(people_count);
121}
122