//Keypad
#include <Keypad.h>
const int ROW_NUM = 4;
const int COLUMN_NUM = 4;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {11, 10, 9, 8};
byte pin_column[COLUMN_NUM] = {7, 6, 5, 4};
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
//Servo
#include <Servo.h>
Servo ProServo;
//OLED
//PIR
#define PIRIN 2
#define PIROUT 3
int In_Val = 0;
int Out_Val = 0;
//IR
#define IRIN 12
#define IROUT 13
//Visitor
volatile int visitors = 0;
//Password
String passNorm = "0000";
String passOpen = "1111";
String passClose = "2222";
//Input
String Input;
/*--------------------------------------------*/
void setup()
{
Serial.begin(9600);
//OLED
//Servo
ProServo.attach(A1);
ProServo.write(0);
//PIR
pinMode(PIRIN, INPUT);
pinMode(PIROUT, INPUT);
//IR
pinMode(IRIN, INPUT);
pinMode(IROUT, INPUT);
//Interrupts
attachInterrupt(digitalPinToInterrupt(PIRIN), carEntering, RISING);
attachInterrupt(digitalPinToInterrupt(PIROUT), carLeaving, RISING);
//Input
Input.reserve(4);
}
/*--------------------------------------------*/
void loop()
{
char key = keypad.getKey();
if (key)
{
Serial.print(key);
if (key == '*')
{
Input = "";
}
else if (key == '#')
{
if (Input == passClose) //gate is always closed
{
closeGate();
}
else if (Input == passOpen) //gate is always open
{
openGate();
}
else if (Input == passNorm) //normal gate
{
NormalGate();
}
}
else
{
Input += key;
}
}
else
{
NormalGate();
}
}
/*--------------------------------------------*/
void NormalGate() //normal gate
{
/*PRO_OLED.setCursor(0, 0);
PRO_OLED.print("CLOSE");
PRO_OLED.setCursor(0, 1);
PRO_OLED.print(visitors);*/
ProServo.write(0);
delay(10000);
}
/*--------------------------------------------*/
void openGate() // open gate
{
for (int i = 0; i <= 90; i++) {
ProServo.write(i);
}
/*PRO_OLED.clearDisplay();
PRO_OLED.setCursor(0, 0);
PRO_OLED.print("Always Opened");
PRO_OLED.setCursor(0, 1);
PRO_OLED.print(visitors);*/
if (digitalRead(IRIN) == HIGH)
{
visitors++;
}
else if (digitalRead(IROUT) == HIGH)
{
visitors--;
}
}
/*--------------------------------------------*/
void closeGate() //close gate
{
ProServo.write(0);
/*PRO_OLED.clearDisplay();
PRO_OLED.setCursor(0, 0);
PRO_OLED.print("Always Closed");
PRO_OLED.setCursor(0, 1);
PRO_OLED.print(visitors);*/
}
/*--------------------------------------------*/
void carEntering() // interrupt 1
{
ProServo.write(90);
if (digitalRead(IRIN) == HIGH)
{
visitors++;
}
}
/*--------------------------------------------*/
void carLeaving() //interrupt 2
{
ProServo.write(90);
if (digitalRead(IROUT) == HIGH)
{
visitors--;
}
}