#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
const int trigPin1 = 41;
const int echoPin1 = 43;
const int redPin1 = 6;
const int yellowPin1 = 5;
const int greenPin1 = 4;
Servo servo1;
int lastRotatedPin1 = -1;
const int trigPin2 = 47;
const int echoPin2 = 49;
const int redPin2 = 25;
const int yellowPin2 = 27;
const int greenPin2 = 29;
Servo servo2;
int lastRotatedPin2 = -1;
const int trigPin3 = 51;
const int echoPin3 = 53;
const int redPin3 = 33;
const int yellowPin3 = 35;
const int greenPin3 = 37;
Servo servo3;
int lastRotatedPin3 = -1;
const int trigPin4 = 24;
const int echoPin4 = 26;
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long previousMillis = 0;
const long interval = 5000; // Interval delay dalam milidetik (ms)
String expectedPaymentCode = "123456"; // Kode pembayaran yang diharapkan
void unlock() {
// Placeholder for unlock function
// Implement your actual unlock logic here (e.g., servo control)
}
void configureSystem() {
// Setup for Servo 1
servo1.attach(13);
servo1.write(0);
// Setup for Servo 2
servo2.attach(12);
servo2.write(0);
// Setup for Servo 3
servo3.attach(11);
servo3.write(0);
// Setup for LCD
lcd.begin(16, 2);
lcd.print("System Ready");
delay(2000);
lcd.clear();
}
void setup() {
Serial.begin(9600);
configureSystem();
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(redPin1, OUTPUT);
pinMode(yellowPin1, OUTPUT);
pinMode(greenPin1, OUTPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(redPin2, OUTPUT);
pinMode(yellowPin2, OUTPUT);
pinMode(greenPin2, OUTPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(redPin3, OUTPUT);
pinMode(yellowPin3, OUTPUT);
pinMode(greenPin3, OUTPUT);
pinMode(trigPin4, OUTPUT);
pinMode(echoPin4, INPUT);
lastRotatedPin3 = -1;
}
void loop() {
unsigned long currentMillis = millis();
int distance1 = getDistance(trigPin1, echoPin1);
int distance2 = getDistance(trigPin2, echoPin2);
int distance3 = getDistance(trigPin3, echoPin3);
int distance4 = getDistance(trigPin4, echoPin4);
setColorBasedOnDistance(distance1, servo1, redPin1, yellowPin1, greenPin1, lastRotatedPin1);
setColorBasedOnDistance(distance2, servo2, redPin2, yellowPin2, greenPin2, lastRotatedPin2);
setColorBasedOnDistance(distance3, servo3, redPin3, yellowPin3, greenPin3, lastRotatedPin3);
unlock(); // Call the unlock function
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
readSerialAndDisplay();
}
}
int getDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
return distance;
}
void setColorBasedOnDistance(int distance, Servo servo, int redPin, int yellowPin, int greenPin, int &lastRotatedPin) {
if (distance > 7) {
if (lastRotatedPin != servo.read()) {
digitalWrite(redPin, HIGH);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
delayWithMillis(3000);
for (int angle = 0; angle <= 90; angle++) {
servo.write(angle);
delayWithMillis(10);
}
delayWithMillis(2000);
for (int angle = 90; angle >= 0; angle--) {
servo.write(angle);
delayWithMillis(10);
}
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, HIGH);
digitalWrite(greenPin, LOW);
delayWithMillis(2000);
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
for (int angle = 0; angle <= 90; angle++) {
servo.write(angle);
delayWithMillis(10);
}
delayWithMillis(2000);
lastRotatedPin = servo.read();
}
} else {
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
delayWithMillis(2000);
if (lastRotatedPin != -1) {
lastRotatedPin = -1;
servo.write(0);
delayWithMillis(10);
}
}
}
void readSerialAndDisplay()
{
if (Serial.available() > 0)
{
String inputCode = Serial.readStringUntil('\n');
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Input: " + inputCode);
if (inputCode == expectedPaymentCode)
{
lcd.setCursor(0, 1);
lcd.print("Payment Successful");
delay(2000);
lcd.clear();
}
else
{
lcd.setCursor(0, 1);
lcd.print("Invalid Code. Retry");
delay(2000);
lcd.clear();
}
}
}
void delayWithMillis(unsigned long duration)
{
unsigned long startTime = millis();
while (millis() - startTime < duration)
{
}
}