From 4c63a9907a7b61299bbf4ae1f2bb95881704173a Mon Sep 17 00:00:00 2001 From: Oneal_PK Date: Thu, 8 Aug 2024 12:38:02 +0700 Subject: [PATCH] Upload files to "/" --- elevator.ino | 316 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 316 insertions(+) create mode 100644 elevator.ino diff --git a/elevator.ino b/elevator.ino new file mode 100644 index 0000000..84fd64b --- /dev/null +++ b/elevator.ino @@ -0,0 +1,316 @@ +#include // Library untuk komunikasi I2C +#include // Library untuk LCD I2C +#include // Library untuk load cell HX711 +#include // Library untuk konektivitas WiFi +#include // Library untuk HTTP Client +#include // Library untuk EEPROM + +// Pin definitions for load cell and I2C +const int dt = 14; // Pin data untuk HX711 +const int sck = 12; // Pin clock untuk HX711 +HX711_ADC ld(dt, sck); // Inisialisasi objek HX711 +#define SDA_PIN 13 // Pin SDA untuk I2C +#define SCL_PIN 33 // Pin SCL untuk I2C +#define BUZZER_PIN 26 // Pin untuk buzzer + +// WiFi and server configuration +#define _SSID "ragawalang" // Nama jaringan WiFi +#define _PASSWORD "ragawalang" // Password WiFi +const char* serverName = "http://192.168.118.204:8080/lift/get_jumlah_orang.php"; // URL server + +// LCD +LiquidCrystal_I2C lcd(0x27, 16, 2); // Inisialisasi LCD I2C +bool buzzerState = false; // Status buzzer (true: aktif, false: tidak aktif) + +// Pin definitions for elevator control +#define PB1_PIN 5 // Pin untuk tombol lantai 1 +#define PB2_PIN 18 // Pin untuk tombol lantai 2 +#define PB3_PIN 19 // Pin untuk tombol lantai 3 +#define LS1_PIN 21 // Pin untuk limit switch lantai 1 +#define LS2_PIN 4 // Pin untuk limit switch lantai 2 +#define LS3_PIN 15 // Pin untuk limit switch lantai 3 +#define MOTOR_PIN_A 22 // Pin motor A +#define MOTOR_PIN_B 32 // Pin motor B +#define MOTOR_PIN_ENA 23 // Pin enable motor + +// EEPROM address for storing floor position +#define FLOOR_EEPROM_ADDR 0 // Alamat EEPROM untuk menyimpan posisi lantai + +// Elevator state +enum ElevatorState { // Definisi enum untuk status elevator + IDLE, // Status IDLE (diam) + MOVING_UP, // Status bergerak ke atas + MOVING_DOWN // Status bergerak ke bawah +}; + +ElevatorState elevatorState = IDLE; // Status awal elevator adalah IDLE +int currentFloor; // Variabel untuk menyimpan lantai saat ini +int targetFloor; // Variabel untuk menyimpan lantai tujuan +unsigned long lastDebounceTimeLS1 = 0; // Waktu debounce terakhir untuk limit switch lantai 1 +unsigned long lastDebounceTimeLS2 = 0; // Waktu debounce terakhir untuk limit switch lantai 2 +unsigned long lastDebounceTimeLS3 = 0; // Waktu debounce terakhir untuk limit switch lantai 3 +unsigned long debounceDelay = 50; // Waktu debounce dalam milidetik +unsigned long timeLcd = 0; // Waktu terakhir LCD diperbarui +bool kapasitasTerlampaui = false; // Status kapasitas terlampaui (true: terlampaui, false: tidak terlampaui) + +// Definisi karakter khusus untuk panah naik +byte arrowUp[8] = { + 0b00100, + 0b01110, + 0b11111, + 0b00100, + 0b00100, + 0b00100, + 0b00100, + 0b00100 +}; + +// Definisi karakter khusus untuk panah turun +byte arrowDown[8] = { + 0b00100, + 0b00100, + 0b00100, + 0b00100, + 0b00100, + 0b11111, + 0b01110, + 0b00100 +}; + +// Fungsi untuk menyimpan posisi lantai ke EEPROM +void saveFloorToEEPROM(int floor) { + EEPROM.write(FLOOR_EEPROM_ADDR, floor); // Tulis posisi lantai ke alamat EEPROM + EEPROM.commit(); // Pastikan data ditulis ke EEPROM +} + +// Fungsi untuk memuat posisi lantai dari EEPROM +int loadFloorFromEEPROM() { + return EEPROM.read(FLOOR_EEPROM_ADDR); // Baca posisi lantai dari alamat EEPROM +} + +void setup() { + // Initialize serial communication + Serial.begin(115200); // Inisialisasi komunikasi serial + + // Initialize EEPROM + EEPROM.begin(512); // Inisialisasi EEPROM dengan 512 byte ruang + + // Load last known floor from EEPROM + currentFloor = loadFloorFromEEPROM(); // Muat lantai terakhir dari EEPROM + targetFloor = currentFloor; // Set target lantai sama dengan lantai saat ini + + // Initialize push button pins + pinMode(PB1_PIN, INPUT_PULLUP); // Set pin tombol lantai 1 sebagai input pullup + pinMode(PB2_PIN, INPUT_PULLUP); // Set pin tombol lantai 2 sebagai input pullup + pinMode(PB3_PIN, INPUT_PULLUP); // Set pin tombol lantai 3 sebagai input pullup + + // Initialize limit switch pins + pinMode(LS1_PIN, INPUT_PULLUP); // Set pin limit switch lantai 1 sebagai input pullup + pinMode(LS2_PIN, INPUT_PULLUP); // Set pin limit switch lantai 2 sebagai input pullup + pinMode(LS3_PIN, INPUT_PULLUP); // Set pin limit switch lantai 3 sebagai input pullup + + // Initialize motor pins + pinMode(MOTOR_PIN_A, OUTPUT); // Set pin motor A sebagai output + pinMode(MOTOR_PIN_B, OUTPUT); // Set pin motor B sebagai output + pinMode(MOTOR_PIN_ENA, OUTPUT); // Set pin enable motor sebagai output + + // Enable motor driver + digitalWrite(MOTOR_PIN_ENA, HIGH); // Aktifkan driver motor + + // Initialize LCD + Wire.begin(SDA_PIN, SCL_PIN); // Inisialisasi I2C dengan pin SDA dan SCL + lcd.init(); // Inisialisasi LCD + //lcd.backlight(); // Nyalakan backlight LCD + lcd.createChar(0, arrowUp); // Buat karakter khusus untuk panah naik + lcd.createChar(1, arrowDown); // Buat karakter khusus untuk panah turun + + // Initialize load cell + ld.begin(); // Mulai load cell + ld.start(2000); // Tunggu 2 detik untuk inisialisasi + ld.setCalFactor(100.0); // Set faktor kalibrasi untuk load cell + pinMode(BUZZER_PIN, OUTPUT); // Set pin buzzer sebagai output + + // Connect to WiFi + WiFi.begin(_SSID, _PASSWORD); // Mulai koneksi WiFi + while (WiFi.status() != WL_CONNECTED) { // Tunggu hingga terhubung ke WiFi + delay(500); + Serial.print("."); + } + Serial.println("\nTerhubung ke WiFi"); // Cetak pesan jika sudah terhubung + + stopMotor(); // Hentikan motor saat inisialisasi +} + +void loop() { + // Membaca berat dari load cell + ld.update(); // Perbarui pembacaan load cell + float weight = ld.getData(); // Dapatkan data berat dari load cell + if (weight < 0) { weight = 0; } // Jika berat negatif, set ke 0 + + // Membaca jumlah orang dari server + int jumlah_orang = 0; + double delayLcd = (millis() - timeLcd); // Hitung waktu sejak LCD terakhir diperbarui + + if (delayLcd > 500) { // Perbarui LCD setiap 500ms + HTTPClient http; // Inisialisasi HTTPClient + http.begin(serverName); // Mulai HTTP request ke server + int httpCode = http.GET(); // Dapatkan kode respons HTTP + + if (httpCode > 0) { // Jika respons berhasil + String payload = http.getString(); // Dapatkan payload dari server + jumlah_orang = payload.toInt(); // Konversi payload ke integer + } else { + Serial.println("Error in HTTP request"); // Cetak pesan error jika gagal + } + + // Logika untuk mengontrol buzzer berdasarkan berat dan jumlah orang + kapasitasTerlampaui = (weight >= 500 || jumlah_orang > 3); // Cek apakah kapasitas terlampaui + if (kapasitasTerlampaui) { // Jika kapasitas terlampaui + if (!buzzerState) { // Jika buzzer belum aktif + digitalWrite(BUZZER_PIN, HIGH); // Aktifkan buzzer + buzzerState = true; // Set status buzzer ke aktif + + lcd.clear(); // Bersihkan LCD + lcd.setCursor(0, 0); // Set kursor LCD ke baris pertama + if (weight >= 500) { // Jika berat terlampaui + lcd.print("Mohon "); + lcd.setCursor(0, 1); + lcd.print("kurangi beban!"); + } else if (jumlah_orang > 3) { // Jika jumlah orang terlampaui + lcd.print("Mohon kurangi"); + lcd.setCursor(0, 1); + lcd.print("jumlah orang!"); + } + } + } else { // Jika kapasitas tidak terlampaui + if (buzzerState) { // Jika buzzer aktif + digitalWrite(BUZZER_PIN, LOW); // Matikan buzzer + buzzerState = false; // Set status buzzer ke tidak aktif + } + + updateLcd(weight); // Perbarui tampilan LCD dengan berat saat ini + } + + timeLcd = millis(); // Set waktu terakhir LCD diperbarui ke waktu saat ini + } + + // Membaca state tombol dengan debouncing + if (!kapasitasTerlampaui) { // Jika kapasitas tidak terlampaui + if (digitalRead(PB1_PIN) == LOW) { // Jika tombol lantai 1 ditekan + targetFloor = 1; // Set lantai tujuan ke 1 + moveToFloor(targetFloor); // Panggil fungsi untuk bergerak ke lantai tujuan + } else if (digitalRead(PB2_PIN) == LOW) { // Jika tombol lantai 2 ditekan + targetFloor = 2; // Set lantai tujuan ke 2 + moveToFloor(targetFloor); // Panggil fungsi untuk bergerak ke lantai tujuan + } else if (digitalRead(PB3_PIN) == LOW) { // Jika tombol lantai 3 ditekan + targetFloor = 3; // Set lantai tujuan ke 3 + moveToFloor(targetFloor); // Panggil fungsi untuk bergerak ke lantai tujuan + } + } + + // Check limit switches with debounce + unsigned long currentMillis = millis(); // Dapatkan waktu saat ini + if ((currentMillis - lastDebounceTimeLS1) > debounceDelay) { // Jika waktu debounce untuk limit switch 1 sudah lewat + if (digitalRead(LS1_PIN) == LOW && targetFloor == 1) { // Jika limit switch 1 aktif dan lantai tujuan adalah 1 + currentFloor = 1; // Set lantai saat ini ke 1 + saveFloorToEEPROM(currentFloor); // Simpan lantai saat ini ke EEPROM + stopMotor(); // Hentikan motor + elevatorState = IDLE; // Set status elevator ke IDLE + Serial.println("Reached floor 1"); // Cetak pesan telah sampai di lantai 1 + } + lastDebounceTimeLS1 = currentMillis; // Set waktu debounce terakhir untuk limit switch 1 + } + if ((currentMillis - lastDebounceTimeLS2) > debounceDelay) { // Jika waktu debounce untuk limit switch 2 sudah lewat + if (digitalRead(LS2_PIN) == LOW) { // Jika limit switch 2 aktif + if (targetFloor == 2) { // Jika lantai tujuan adalah 2 + currentFloor = 2; // Set lantai saat ini ke 2 + saveFloorToEEPROM(currentFloor); // Simpan lantai saat ini ke EEPROM + stopMotor(); // Hentikan motor + elevatorState = IDLE; // Set status elevator ke IDLE + Serial.println("Reached floor 2"); // Cetak pesan telah sampai di lantai 2 + } else { + currentFloor = 2; // Set lantai saat ini ke 2 (untuk tampilan di LCD) + updateLcd(weight); // Perbarui tampilan LCD + } + } + lastDebounceTimeLS2 = currentMillis; // Set waktu debounce terakhir untuk limit switch 2 + } + if ((currentMillis - lastDebounceTimeLS3) > debounceDelay) { // Jika waktu debounce untuk limit switch 3 sudah lewat + if (digitalRead(LS3_PIN) == LOW && targetFloor == 3) { // Jika limit switch 3 aktif dan lantai tujuan adalah 3 + currentFloor = 3; // Set lantai saat ini ke 3 + saveFloorToEEPROM(currentFloor); // Simpan lantai saat ini ke EEPROM + stopMotor(); // Hentikan motor + elevatorState = IDLE; // Set status elevator ke IDLE + Serial.println("Reached floor 3"); // Cetak pesan telah sampai di lantai 3 + } + lastDebounceTimeLS3 = currentMillis; // Set waktu debounce terakhir untuk limit switch 3 + } +} + +// Fungsi untuk bergerak ke lantai tujuan +void moveToFloor(int floor) { + if (elevatorState == IDLE && targetFloor != currentFloor) { // Jika elevator dalam keadaan diam dan lantai tujuan berbeda dengan lantai saat ini + if (floor > currentFloor) { // Jika lantai tujuan lebih tinggi dari lantai saat ini + elevatorState = MOVING_UP; // Set status elevator ke bergerak ke atas + moveUp(); // Panggil fungsi untuk bergerak ke atas + } else if (floor < currentFloor) { // Jika lantai tujuan lebih rendah dari lantai saat ini + elevatorState = MOVING_DOWN; // Set status elevator ke bergerak ke bawah + moveDown(); // Panggil fungsi untuk bergerak ke bawah + } + Serial.print("Moving to floor: "); // Cetak pesan tujuan lantai + Serial.println(floor); + } +} + +// Fungsi untuk bergerak ke atas +void moveUp() { + digitalWrite(MOTOR_PIN_A, HIGH); // Set pin motor A ke HIGH (bergerak ke atas) + digitalWrite(MOTOR_PIN_B, LOW); // Set pin motor B ke LOW + Serial.println("Moving up"); // Cetak pesan bergerak ke atas + printMotorStatus(); // Cetak status motor +} + +// Fungsi untuk bergerak ke bawah +void moveDown() { + digitalWrite(MOTOR_PIN_A, LOW); // Set pin motor A ke LOW + digitalWrite(MOTOR_PIN_B, HIGH); // Set pin motor B ke HIGH (bergerak ke bawah) + Serial.println("Moving down"); // Cetak pesan bergerak ke bawah + printMotorStatus(); // Cetak status motor +} + +// Fungsi untuk menghentikan motor +void stopMotor() { + digitalWrite(MOTOR_PIN_A, LOW); // Set pin motor A ke LOW (motor berhenti) + digitalWrite(MOTOR_PIN_B, LOW); // Set pin motor B ke LOW + Serial.println("Motor stopped"); // Cetak pesan motor berhenti + printMotorStatus(); // Cetak status motor +} + +// Fungsi untuk mencetak status motor +void printMotorStatus() { + Serial.print("Motor PIN A: "); // Cetak status pin motor A + Serial.println(digitalRead(MOTOR_PIN_A)); + Serial.print("Motor PIN B: "); // Cetak status pin motor B + Serial.println(digitalRead(MOTOR_PIN_B)); +} + +// Fungsi untuk memperbarui tampilan LCD +void updateLcd(float weight) { + lcd.clear(); // Bersihkan LCD + lcd.setCursor(0, 0); // Set kursor ke baris pertama + lcd.print("Berat: "); // Cetak teks "Berat: " + lcd.print(weight); // Cetak berat saat ini + lcd.print(" gram"); // Cetak satuan berat + + lcd.setCursor(0, 1); // Set kursor ke baris kedua + lcd.print("Lantai: "); // Cetak teks "Lantai: " + lcd.print(currentFloor); // Cetak lantai saat ini + + if (elevatorState == MOVING_UP) { // Jika elevator bergerak ke atas + lcd.setCursor(15, 1); // Set kursor ke kolom ke-15 baris kedua + lcd.write(byte(0)); // Tampilkan karakter panah ke atas + } else if (elevatorState == MOVING_DOWN) { // Jika elevator bergerak ke bawah + lcd.setCursor(15, 1); // Set kursor ke kolom ke-15 baris kedua + lcd.write(byte(1)); // Tampilkan karakter panah ke bawah + } +} \ No newline at end of file