💻 Kode Program Arduino
/*========================================= SMART PARKING SYSTEM MTs Negeri 8 Tangerang=========================================
Komponen:- Arduino UNO- 2 Sensor IR- Servo SG90- LCD I2C 16x2
IR Masuk : D2IR Keluar : D3Servo : D9
=========================================*/
#include <Wire.h>#include <LiquidCrystal_I2C.h>#include <Servo.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);Servo portal;
// ===========================// Konfigurasi Pin// ===========================const byte IR1 = 2; // Sensor sebelum portalconst byte IR2 = 3; // Sensor sesudah portalconst byte SERVO = 9;
// ===========================// Variabel// ===========================const int kapasitas = 3;int jumlahMobil = 0;
bool prosesMasuk = false;bool prosesKeluar = false;
void setup(){ pinMode(IR1, INPUT); pinMode(IR2, INPUT);
portal.attach(SERVO); portal.write(0);
lcd.init(); lcd.backlight();
tampilLCD();
Serial.begin(9600);}
void loop(){ bool s1 = digitalRead(IR1); bool s2 = digitalRead(IR2);
// ====================================== // MULAI PROSES MASUK // ====================================== if (!prosesMasuk && !prosesKeluar) { if (s1 == LOW) { if (jumlahMobil < kapasitas) { prosesMasuk = true; bukaPortal(); Serial.println("Calon Mobil Masuk"); } else { lcd.clear(); lcd.setCursor(0,0); lcd.print("PARKIR PENUH"); lcd.setCursor(0,1); lcd.print("Tidak Bisa Masuk"); delay(1000); tampilLCD(); } }
else if (s2 == LOW) { if (jumlahMobil > 0) { prosesKeluar = true; bukaPortal(); Serial.println("Calon Mobil Keluar"); } } }
// ====================================== // KONFIRMASI MASUK // ====================================== if (prosesMasuk) { if (s2 == LOW) { jumlahMobil++; if (jumlahMobil > kapasitas) jumlahMobil = kapasitas;
Serial.println("Mobil Masuk");
tutupPortal(); tampilLCD();
prosesMasuk = false;
while(digitalRead(IR2)==LOW); delay(200); } }
// ====================================== // KONFIRMASI KELUAR // ====================================== if (prosesKeluar) { if (s1 == LOW) { jumlahMobil--; if (jumlahMobil < 0) jumlahMobil = 0;
Serial.println("Mobil Keluar");
tutupPortal(); tampilLCD();
prosesKeluar = false;
while(digitalRead(IR1)==LOW); delay(200); } }}
//=====================================
void bukaPortal(){ portal.write(0);}
//=====================================
void tutupPortal(){ delay(1000); portal.write(90);}
//=====================================
void tampilLCD(){ lcd.clear();
lcd.setCursor(0,0); lcd.print("SIDELTA PARKING");
lcd.setCursor(0,1);
if(jumlahMobil >= kapasitas) { lcd.print("PENUH "); lcd.print(kapasitas); lcd.print("/"); lcd.print(kapasitas); } else { lcd.print("Sisa:"); lcd.print(kapasitas - jumlahMobil); lcd.print("/"); lcd.print(kapasitas); }}📖 Penjelasan Program
Berikut penjelasan dari code berikut.
1️⃣ Menentukan Library
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
Library digunakan agar Arduino dapat berkomunikasi dengan LCD I2C dan mengendalikan motor servo.
2️⃣ Menentukan Pin
const int irMasuk = 2;
const int irKeluar = 3;
const int servoPin = 9;
Bagian ini menentukan pin yang digunakan setiap komponen.
3️⃣ Variabel Counter
int jumlahMobil = 0;
Variabel ini berfungsi menghitung jumlah mobil yang sedang berada di area parkir.
4️⃣ Kapasitas Maksimum
const int kapasitasMaks = 5;
Nilai ini dapat diubah sesuai kebutuhan.
Misalnya
10
20
30
50
5️⃣ Fungsi Membuka Portal
bukaPortal();
Daripada menulis
portal.write(90);
delay(3000);
portal.write(0);
berulang kali,
lebih baik dibuat menjadi sebuah fungsi.
Ini merupakan contoh penerapan modular programming.
6️⃣ Fungsi LCD
tampilLCD();
Setiap kali jumlah mobil berubah,
LCD akan otomatis diperbarui.
7️⃣ Logika Counter
Jika
Sensor Masuk aktif
jumlahMobil++
Jika
Sensor Keluar aktif
jumlahMobil--
Konsep ini dinamakan Counter.
Counter adalah salah satu teknik pemrograman yang paling banyak digunakan dalam sistem otomatis.
0 Komentar