#include <Stepper.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 3,4,5,6);
#define PIN_TRIG_1 13
#define PIN_ECHO_1 12
#define PIN_TRIG_2 8
#define PIN_ECHO_2 7
#define MAX_ORANG 5
int counter=4,a=1,b=0,c=0,d=1;
void setup() {
Serial.begin(115200);
pinMode(PIN_TRIG_1, OUTPUT);
pinMode(PIN_ECHO_1, INPUT);
pinMode(PIN_TRIG_2, OUTPUT);
pinMode(PIN_ECHO_2, INPUT);
lcd.init();
lcd.backlight();
}
void opendoor() {
if(a == 1){
myStepper.step(stepsPerRevolution);
delay(500);
a = 0;
}
}
void closedoor() {
if(a==0){
myStepper.step(-stepsPerRevolution);
delay(500);
a = 1;
}
}
void loop() {
// Start a new measurement:
digitalWrite(PIN_TRIG_1, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG_1, LOW);
// Read the result:
int duration1 = pulseIn(PIN_ECHO_1, HIGH);
int distance1 = duration1 / 58.2;
//start measurment 2:
digitalWrite(PIN_TRIG_2, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG_2, LOW);
//read result 2
int duration2 = pulseIn(PIN_ECHO_2,HIGH);
int distance2 = duration2 / 58.2;
lcd.setCursor(2,0);
lcd.print("jumlah orang: ");
lcd.setCursor(8,1);
lcd.print(counter);
if(counter < MAX_ORANG){
if(distance2 < 20){
opendoor();
if(b==0){d=6/d;b=1;}
}
else if(distance1 < 20){
opendoor();
if(c==0){d=3/d;c=1;}
}
else
{
closedoor();
if(d == 0){counter++;}
else if(d == 2){counter--;}
b=0;
c=0;
d=1;
}
}
else
{
if(distance2 < 200){
if(b==0){d=6/d;b=1;}
}
else if(distance1 < 200){
opendoor();
if(c==0){d=3/d;c=1;}
}
else
{
closedoor();
if(d == 0){counter++;}
else if(d == 2){counter--;}
b=0;
c=0;
d=1;
}
}
}