#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Keypad.h>
const int trigPin1 = 41;
const int echoPin1 = 43;
const int redPin1 = 7;
const int greenPin1 = 6;
const int bluePin1 = 5;
Servo servo1;
int lastRotatedPin1 = -1;
const int trigPin2 = 47;
const int echoPin2 = 49;
const int redPin2 = 25;
const int greenPin2 = 27;
const int bluePin2 = 29;
Servo servo2;
int lastRotatedPin2 = -1;
const int trigPin3 = 51;
const int echoPin3 = 53;
const int redPin3 = 33;
const int greenPin3 = 35;
const int bluePin3 = 37;
Servo servo3;
int lastRotatedPin3 = -1;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {A3, A2, A1, A0};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Payment code (replace with your desired code)
const char correctPaymentCode[] = "666666";
void setup() {
Serial.begin(9600);
lcd.begin(16, 2); // Initialize the LCD
lcd.backlight();
servo1.attach(13);
servo1.write(0);
servo2.attach(12);
servo2.write(0);
servo3.attach(11);
servo3.write(0);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(redPin1, OUTPUT);
pinMode(greenPin1, OUTPUT);
pinMode(bluePin1, OUTPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(redPin2, OUTPUT);
pinMode(greenPin2, OUTPUT);
pinMode(bluePin2, OUTPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(redPin3, OUTPUT);
pinMode(greenPin3, OUTPUT);
pinMode(bluePin3, OUTPUT);
}
void loop() {
int distance1 = getDistance(trigPin1, echoPin1);
int distance2 = getDistance(trigPin2, echoPin2);
int distance3 = getDistance(trigPin3, echoPin3);
handleKeypadInput(distance1, distance2, distance3); // Handle keypad input
Serial.print("Distance 1: ");
Serial.println(distance1);
Serial.print("Distance 2: ");
Serial.println(distance2);
Serial.print("Distance 3: ");
Serial.println(distance3);
setColorBasedOnDistance(distance1, servo1, redPin1, greenPin1, bluePin1, lastRotatedPin1);
setColorBasedOnDistance(distance2, servo2, redPin2, greenPin2, bluePin2, lastRotatedPin2);
setColorBasedOnDistance(distance3, servo3, redPin3, greenPin3, bluePin3, lastRotatedPin3);
delay(1000); // Tambahkan penundaan kecil di sini
}
void handleKeypadInput(int distance1, int distance2, int distance3) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter code:");
char enteredCode[5];
int i = 0;
while (1) {
char key = keypad.getKey();
if (key) {
lcd.setCursor(i, 1);
lcd.print('*'); // Display asterisks instead of the actual digits
if (key == '#') {
enteredCode[i] = '\0'; // Null-terminate the entered code
break;
} else {
enteredCode[i++] = key;
}
}
}
if (strcmp(enteredCode, correctPaymentCode) == 0) {
lcd.clear();
lcd.print("Code Accepted!");
// Check if no object is detected by the ultrasonic sensor
if (distance1 > 7 && distance2 > 7 && distance3 > 7) {
rotateServoTo90(servo1);
// Add more servos if needed
}
} else {
lcd.clear();
lcd.print("Invalid Code!");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter code:");
}
}
void rotateServoTo90(Servo servo) {
Serial.println("Moving Servo to 90 degrees");
for (int angle = 0; angle <= 90; angle++) {
servo.write(angle);
delay(50); // Adjust the delay to make the servo move faster
}
}
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;
Serial.print("Duration: ");
Serial.print(duration);
Serial.print(" microseconds. Distance: ");
Serial.println(distance);
return distance;
}
void setColorBasedOnDistance(int distance, Servo servo, int redPin, int greenPin, int bluePin, int& lastRotatedPin) {
if (distance > 7) {
if (lastRotatedPin != servo.read()) {
Serial.println("Turning LED to red");
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
delay(5000); // Adjust the delay to control how long the LED stays on
Serial.println("Moving Servo from 0 to 90 degrees");
for (int angle = 0; angle <= 90; angle++) {
servo.write(angle);
delay(50); // Adjust the delay to make the servo move faster
}
delay(5000); // Wait for 5 seconds before moving the servo back
Serial.println("Moving Servo from 90 to 0 degrees");
for (int angle = 90; angle >= 0; angle--) {
servo.write(angle);
delay(50); // Adjust the delay to make the servo move faster
}
Serial.println("Turning LED to blue");
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
delay(5000); // Adjust the delay to control how long the LED stays on
lastRotatedPin = servo.read();
}
} else {
if (lastRotatedPin != -1) {
Serial.println("Turning LED to blue");
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
delay(5000); // Adjust the delay to control how long the LED stays on
lastRotatedPin = -1;
}
}
}