#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define buzzer 7
#define in 8
#define out 9
int count = 0;
int maxPerson = 6;
byte smiley[] = {
B00000,
B00000,
B01010,
B01010,
B00000,
B10001,
B01110,
B00000
};
void setup() {
lcd.begin(16,2);
pinMode(in, INPUT);
pinMode(out, INPUT);
pinMode(buzzer, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(2,0);
lcd.print("WELCOME TO");
lcd.setCursor(1,1);
lcd.print("HUMAN COUNTER");
lcd.createChar(1, smiley);
lcd.setCursor(14,1);
lcd.write(1);
delay(1500);
}
void loop() {
while (count >= 0) { //para tuloy tuloy ang counting. nag lo-loop sya.
int in_value = digitalRead(in);
int out_value = digitalRead(out);
if (in_value == LOW) { //if statement tells that hanggat low yung value sa in, naka stay put lang sya hanggang sa tumaas
count++; // pag tumaas ang value, add 1
tone(buzzer, 1030); // Then buzzer plays On
delay(100);
noTone(7);
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Person In Room:");
lcd.setCursor(7,1);
lcd.print(count);
delay(1000);
}
if (out_value == LOW && count > 0) { //if statement tells that hanggat low yung value sa out, naka stay put lang sya hanggang sa tumaas | greater than 0 para hindi mag negative
count--; // pag tumaas ang value, minus 1
tone(buzzer, 50); // Then buzzer plays On
delay(100);
noTone(7);
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Person In Room:");
lcd.setCursor(7,1);
lcd.print(count);
delay(1000);
}
if (count == 0) { //if statement tells that pag equal to 0 yung value
lcd.clear();
lcd.setCursor(3,0);
lcd.print("NOBODY IS"); // this text displayed
lcd.setCursor(2,1);
lcd.print("IN THE ROOM"); // same as this one
delay(200);
}
if (count >= maxPerson) { //if statement tells that pag yung count ay mataas or katulad ng declared value ng maxPerson which is (6)
tone(buzzer, 1030); // Then buzzer plays On
delay(100);
noTone(7); // Then Off (it plays On and off in loop)
lcd.clear();
lcd.setCursor(0,0);
lcd.print("CAPACITY REACHED"); // this text displayed
lcd.setCursor(2,1);
lcd.print("WAIT OUTSIDE"); // same as this one
}
else{
noTone(7); // if it is less than 6 (buzzer Off)
}
}
}