//Welcome in the project Moch Rijki Supriyatna:("MMRS_Iky")
//Sistem parkir otomatis
#include <IRremote.h>
#include <TM1637Display.h>
#include <Servo.h>
// Pin setup
#define IR_PIN 2
#define CLK 3
#define DIO 4
#define TRIG_PIN 5
#define ECHO_PIN 6
#define BUZZER_PIN 7
#define SERVO_PIN 9
const int maxSlot = 5; // Jumlah slot parkir
int occupiedSlot = 0;
TM1637Display display(CLK, DIO);
Servo palang;
IRrecv irrecv(IR_PIN);
decode_results results;
unsigned long lastDetectTime = 0;
bool accessGranted = false;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
palang.attach(SERVO_PIN);
palang.write(0); // palang tertutup
display.setBrightness(0x0f);
display.showNumberDec(maxSlot - occupiedSlot, true);
}
void loop() {
if (irrecv.decode(&results)) {
unsigned long irCode = results.value;
// Ganti kode ini sesuai tombol remote Anda
if (irCode == 0xFFA25D) { // contoh tombol POWER
if (occupiedSlot < maxSlot) {
accessGranted = true;
bukaPalang();
} else {
bunyiBuzzer(); // slot penuh
}
} else {
bunyiBuzzer(); // akses ditolak
}
irrecv.resume(); // siap baca berikutnya
}
if (accessGranted) {
long duration, distance;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2;
if (distance < 15 && millis() - lastDetectTime > 1000) {
occupiedSlot++;
if (occupiedSlot > maxSlot) occupiedSlot = maxSlot;
display.showNumberDec(maxSlot - occupiedSlot, true);
palang.write(0); // tutup palang
accessGranted = false;
lastDetectTime = millis();
}
}
}
void bukaPalang() {
palang.write(90);
}
void bunyiBuzzer() {
tone(BUZZER_PIN, 1000, 300);
}