Devices & Components
Arduino Uno Rev3
16x2 LCD display with I²C interface
Breadboard 100x160
connecting wires
12V DC Power Supply
IR Sensors
Software & Tools
Arduino IDE
Project description
Code
Bidirectional counter.ino
cpp
Complete Arduino Code
1#include <LiquidCrystal_I2C.h> 2// Initialize the LCD with I2C address 0x27 3LiquidCrystal_I2C lcd(0x27, 16, 2); 4const int irPin1 = 2; // IR sensor 1(Entrance Detector) connected to digital pin 2 5const int irPin2 = 3; // IR sensor 2(Exit Detector) connected to digital pin 3 6int in_count = 0; // Variable to store the number of people went inside 7int out_count = 0; // Variable to store the number of people went outside 8int current_count = 0; // Variable to store the current number of people present inside 9const unsigned long timeout = 50; // Object detection Timeout period in milliseconds 10void setup() { 11 /* Initialise LCD Display */ 12 lcd.init(); 13 lcd.backlight(); 14 lcd.setCursor(0, 0); 15 lcd.print("IN: 0 OUT: 0"); 16 lcd.setCursor(0, 1); 17 lcd.print("Current: 0"); 18 19 /* Pull up the input pins */ 20 pinMode(irPin1, INPUT_PULLUP); 21 pinMode(irPin2, INPUT_PULLUP); 22} 23void loop() { 24 /* Check if the first sensor is triggered */ 25 if (digitalRead(irPin1) == LOW) { 26 unsigned long startTime = millis(); 27 while ((millis() - startTime) < timeout) { 28 if(digitalRead(irPin2) == LOW){ 29 ++in_count; 30 updateDisplay(); 31 break; 32 } 33 } 34 //wait until both sensors return to a normal state 35 while(!digitalRead(irPin1) || !digitalRead(irPin2)); 36 } 37 38 /* Check if the second sensor is triggered */ 39 else if (digitalRead(irPin2) == LOW) { 40 unsigned long startTime = millis(); 41 while ((millis() - startTime) < timeout) { 42 if(digitalRead(irPin1) == LOW){ 43 if(out_count < in_count){ 44 ++out_count; 45 updateDisplay(); 46 break; 47 } 48 } 49 } 50 //wait until both sensors return to a normal state 51 while(!digitalRead(irPin1) || !digitalRead(irPin2)); 52 } 53 54} 55void updateDisplay() { 56 lcd.setCursor(4, 0); 57 lcd.print(" "); 58 lcd.setCursor(4, 0); 59 lcd.print(in_count); 60 lcd.setCursor(13, 0); 61 lcd.print(" "); 62 lcd.setCursor(13, 0); 63 lcd.print(out_count); 64 current_count = in_count - out_count; 65 lcd.setCursor(9, 1); 66 lcd.print(" "); 67 lcd.setCursor(9, 1); 68 lcd.print(current_count); 69}
Downloadable files
Bi directional counter code
code for Arduino
Bidirectional_counter-1.ino
Comments
Only logged in users can leave comments