#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// --- Definisi Pin dan Konstanta ---
const int DIP_PIN_0 = 13;
const int DIP_PIN_1 = 14;
const int DIP_PIN_2 = 15;
const int DIP_PIN_3 = 27;
const int TEST_MODE_PIN = 35;
const int EMERGENCY_PIN = 23;
// Komunikasi serial 2 arah untuk pintu
const int SERIAL_DOOR_RX = 16; // UART2 RX
const int SERIAL_DOOR_TX = 17; // UART2 TX
// Komunikasi serial untuk notifikasi audio lantai
const int SERIAL_AUDIO_RX = 9; // UART1 RX
const int SERIAL_AUDIO_TX = 10; // UART1 TX
// Shift Register Input
const int SR_IN_LATCH = 32;
const int SR_IN_CLOCK = 33;
const int SR_IN_DATA = 34;
// Shift Register Output
const int SR_OUT_LATCH = 25;
const int SR_OUT_CLOCK = 26;
const int SR_OUT_DATA = 5;
// Pin relay untuk motor
const int RELAY_MOTOR_UP = 19;
const int RELAY_MOTOR_DOWN = 18;
const int RELAY_MOTOR_BRAKE = 4;
// OLED
const int SCREEN_WIDTH = 128;
const int SCREEN_HEIGHT = 64;
const int OLED_RESET = -1;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Durasi dan Timeout
const unsigned long DOOR_OPEN_DURATION_MS = 3000;
const unsigned long DOOR_CLOSE_DURATION_MS = 2000;
const unsigned long DOOR_OPEN_HOLD_TIME_MS = 5000;
const unsigned long MOTOR_TIMEOUT_MS = 10000;
const unsigned long MOTOR_DIRECTION_SWITCH_DELAY_MS = 200;
const unsigned long DEBOUNCE_DELAY_MS = 50;
const unsigned long LIMIT_DEBOUNCE_DELAY_MS = 100;
// --- Definisi Bit Shift Register Input 1 ---
const int COP_BUTTON_1_BIT = 0;
const int COP_BUTTON_2_BIT = 1;
const int COP_BUTTON_3_BIT = 2;
const int DOOR_OPEN_BUTTON_BIT = 3;
const int DOOR_CLOSE_BUTTON_BIT = 4;
const int CABIN_LIGHT_BUTTON_BIT = 5;
const int LOP_CALL_UP_BIT = 6;
const int LOP_CALL_DOWN_BIT = 7;
// --- Definisi Bit Shift Register Input 2 ---
const int LIMIT_FLOOR_1_BIT = 0;
const int LIMIT_FLOOR_2_BIT = 1;
const int LIMIT_FLOOR_3_BIT = 2;
const int LIMIT_FINAL_UP_BIT = 3;
const int LIMIT_FINAL_DOWN_BIT = 4;
// --- Definisi Bit Shift Register Output 1 (Output Shift Register Pertama) ---
const int CALL_LED_1_BIT = 0;
const int CALL_LED_2_BIT = 1;
const int CALL_LED_3_BIT = 2;
const int DOOR_OPEN_INDICATOR_BIT = 3;
const int DOOR_CLOSE_INDICATOR_BIT = 4;
const int CABIN_LIGHT_BIT = 5;
const int LOP_CALL_UP_LED_BIT = 6;
const int LOP_CALL_DOWN_LED_BIT = 7;
// --- Definisi Bit Shift Register Output 2 (Output Shift Register Kedua) ---
const int EMERGENCY_LED_BIT = 0;
const int BUZZER_BIT = 1;
// --- Variabel Global ---
int totalFloors = 3;
int currentFloor = 0;
bool cabinLightOn = false;
byte inputData1, inputData2;
byte outputData1, outputData2;
bool callUp[16] = {false};
bool callDown[16] = {false};
bool copCallActive[16] = {false};
enum ElevatorState {
IDLE, MOVING_UP, MOVING_DOWN, DOOR_OPENING, DOOR_CLOSING,
EMERGENCY_STOP, EMERGENCY_RESETTING, CALIBRATING, CALIBRATION_FAILED, DOOR_OPEN
};
ElevatorState currentState = CALIBRATING;
ElevatorState lastDirection = MOVING_UP;
enum OperationMode {
MODE_NORMAL, MODE_TEST, MODE_INSPECTION
};
OperationMode currentMode = MODE_NORMAL;
bool isEmergency = false;
unsigned long stateTimer = 0;
unsigned long motorStartTime = 0;
// Variabel untuk debounce
unsigned long lastButtonPressTime[16] = {0};
bool buttonState[16];
bool lastTestModePinState = HIGH;
unsigned long lastModeChangeTime = 0;
bool lastCabinLightButtonState = HIGH;
unsigned long lastCabinLightTime = 0;
bool lastLimitStatus[16];
unsigned long lastLimitDebounceTime[16] = {0};
unsigned long lastCallUpTime = 0;
unsigned long lastCallDownTime = 0;
// Variabel untuk tampilan dinamis
unsigned long lastBlink = 0;
bool blinkState = true;
unsigned long previousMillis = 0;
int dotCount = 0;
// --- Prototipe Fungsi ---
void readDipSwitch();
void controlMotor(ElevatorState state);
void stopMotor();
void displayStatus();
void displayCallQueue();
void processCallButtons();
void handleLimitSwitches();
void handleEmergency();
void calibrateLift();
void emergencyReset();
int findNextFloor();
void handleModeChange();
void handleNormalMode();
void handleTestMode();
void handleInspectionMode();
void drawArrowUp(int x, int y, int size);
void drawArrowDown(int x, int y, int size);
void readAllSRInputs();
void writeAllSROutputs();
void updateFloorLeds();
void processDoorAndLightButtons();
void handleMotorTimeout();
// =================================== SETUP ===================================
void setup() {
Serial.begin(115200);
Serial.println("Elevator Controller Starting...");
pinMode(DIP_PIN_0, INPUT_PULLUP);
pinMode(DIP_PIN_1, INPUT_PULLUP);
pinMode(DIP_PIN_2, INPUT_PULLUP);
pinMode(DIP_PIN_3, INPUT_PULLUP);
pinMode(TEST_MODE_PIN, INPUT_PULLUP);
pinMode(EMERGENCY_PIN, INPUT_PULLUP);
pinMode(SR_IN_DATA, INPUT);
pinMode(SR_IN_LATCH, OUTPUT);
pinMode(SR_IN_CLOCK, OUTPUT);
pinMode(SR_OUT_DATA, OUTPUT);
pinMode(SR_OUT_LATCH, OUTPUT);
pinMode(SR_OUT_CLOCK, OUTPUT);
digitalWrite(SR_IN_LATCH, HIGH);
digitalWrite(SR_OUT_LATCH, LOW);
pinMode(RELAY_MOTOR_UP, OUTPUT);
pinMode(RELAY_MOTOR_DOWN, OUTPUT);
pinMode(RELAY_MOTOR_BRAKE, OUTPUT);
stopMotor();
Serial2.begin(9600, SERIAL_8N1, SERIAL_DOOR_RX, SERIAL_DOOR_TX);
Serial1.begin(9600, SERIAL_8N1, SERIAL_AUDIO_RX, SERIAL_AUDIO_TX);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
readAllSRInputs();
for (int i = 0; i < totalFloors; i++) {
lastLimitStatus[i] = (bitRead(inputData2, i) == 0);
}
showStartup();
readDipSwitch();
calibrateLift();
if (currentState != CALIBRATION_FAILED) {
currentState = IDLE;
Serial.println("System is ready. State: IDLE");
}
}
// =================================== LOOP UTAMA ===================================
void loop() {
readAllSRInputs();
handleEmergency();
handleModeChange();
if (bitRead(inputData2, LIMIT_FINAL_UP_BIT) == 0 || bitRead(inputData2, LIMIT_FINAL_DOWN_BIT) == 0) {
if (!isEmergency) {
Serial.println("FINAL LIMIT SWITCH TERPICU! Lift dihentikan karena over-travel.");
isEmergency = true;
currentState = EMERGENCY_STOP;
stopMotor();
bitSet(outputData2, EMERGENCY_LED_BIT);
}
}
if (isEmergency || currentState == CALIBRATION_FAILED) {
displayStatus();
return;
}
switch (currentMode) {
case MODE_NORMAL:
handleNormalMode();
break;
case MODE_TEST:
handleTestMode();
break;
case MODE_INSPECTION:
handleInspectionMode();
break;
default:
currentMode = MODE_NORMAL;
break;
}
updateFloorLeds();
displayStatus();
}
// ================== Fungsi Mode Operasi ==================
void handleModeChange() {
bool currentTestModePinState = digitalRead(TEST_MODE_PIN);
unsigned long now = millis();
if (lastTestModePinState == HIGH && currentTestModePinState == LOW && (now - lastModeChangeTime) > DEBOUNCE_DELAY_MS) {
switch (currentMode) {
case MODE_NORMAL:
currentMode = MODE_TEST;
Serial.println("Mode changed to: TEST");
break;
case MODE_TEST:
currentMode = MODE_INSPECTION;
Serial.println("Mode changed to: INSPECTION");
break;
case MODE_INSPECTION:
currentMode = MODE_NORMAL;
Serial.println("Mode changed to: NORMAL");
break;
}
lastModeChangeTime = now;
stopMotor();
currentState = IDLE;
for (int i = 0; i < totalFloors; i++) {
callUp[i] = false;
callDown[i] = false;
copCallActive[i] = false;
}
}
lastTestModePinState = currentTestModePinState;
}
void handleNormalMode() {
if (Serial2.available() > 0) {
String command = Serial2.readStringUntil('\n');
command.trim();
if (command.startsWith("CALL")) {
int floor = command.substring(4).toInt();
if (floor >= 1 && floor <= totalFloors) {
if (floor > currentFloor) {
callUp[floor - 1] = true;
Serial.print("Panggilan UART ke Lantai "); Serial.print(floor); Serial.println(" (NAIK)");
} else if (floor < currentFloor) {
callDown[floor - 1] = true;
Serial.print("Panggilan UART ke Lantai "); Serial.print(floor); Serial.println(" (TURUN)");
} else {
if (currentState == IDLE || currentState == DOOR_OPEN) {
currentState = DOOR_OPENING;
stateTimer = millis();
Serial.println("Panggilan ke lantai saat ini. Memicu pembukaan pintu.");
}
}
}
}
}
processDoorAndLightButtons();
handleLimitSwitches();
handleMotorTimeout();
static ElevatorState lastState = currentState;
if (lastState != currentState) {
lastState = currentState;
Serial.print("State berubah menjadi: ");
switch (currentState) {
case IDLE: Serial.println("IDLE"); break;
case MOVING_UP: Serial.println("MOVING_UP"); break;
case MOVING_DOWN: Serial.println("MOVING_DOWN"); break;
case DOOR_OPENING: Serial.println("DOOR_OPENING"); break;
case DOOR_CLOSING: Serial.println("DOOR_CLOSING"); break;
case DOOR_OPEN: Serial.println("DOOR_OPEN"); break;
case CALIBRATING: Serial.println("CALIBRATING"); break;
case CALIBRATION_FAILED: Serial.println("CALIBRATION_FAILED"); break;
case EMERGENCY_STOP: Serial.println("EMERGENCY_STOP"); break;
case EMERGENCY_RESETTING: Serial.println("EMERGENCY_RESETTING"); break;
}
}
switch (currentState) {
case IDLE: {
int nextFloor = findNextFloor();
if (nextFloor != -1) {
if (nextFloor > currentFloor) {
currentState = MOVING_UP;
lastDirection = MOVING_UP;
} else {
currentState = MOVING_DOWN;
lastDirection = MOVING_DOWN;
}
motorStartTime = millis();
}
break;
}
case MOVING_UP:
case MOVING_DOWN:
controlMotor(currentState);
break;
case DOOR_OPENING:
Serial2.print("OPEN\n");
stopMotor();
if (millis() - stateTimer > DOOR_OPEN_DURATION_MS) {
currentState = DOOR_OPEN;
stateTimer = millis();
}
break;
case DOOR_OPEN:
if (millis() - stateTimer > DOOR_OPEN_HOLD_TIME_MS) {
currentState = DOOR_CLOSING;
stateTimer = millis();
}
break;
case DOOR_CLOSING: {
Serial2.print("CLOSE\n");
if (millis() - stateTimer > DOOR_CLOSE_DURATION_MS) {
int nextFloor = findNextFloor();
if (nextFloor != -1) {
if (nextFloor > currentFloor) {
currentState = MOVING_UP;
lastDirection = MOVING_UP;
} else {
currentState = MOVING_DOWN;
lastDirection = MOVING_DOWN;
}
motorStartTime = millis();
} else {
currentState = IDLE;
}
}
break;
}
case EMERGENCY_RESETTING:
emergencyReset();
break;
default:
break;
}
}
void handleTestMode() {
processCallButtons();
processDoorAndLightButtons();
handleLimitSwitches();
handleNormalMode();
}
void handleInspectionMode() {
if (Serial2.available() > 0) {
String command = Serial2.readStringUntil('\n');
command.trim();
if (command.equals("UP")) {
if (currentState == IDLE) {
currentState = MOVING_UP;
lastDirection = MOVING_UP;
motorStartTime = millis();
Serial.println("Perintah INSPEKSI: NAIK");
}
} else if (command.equals("DOWN")) {
if (currentState == IDLE) {
currentState = MOVING_DOWN;
lastDirection = MOVING_DOWN;
motorStartTime = millis();
Serial.println("Perintah INSPEKSI: TURUN");
}
} else if (command.equals("STOP")) {
stopMotor();
currentState = IDLE;
Serial.println("Perintah INSPEKSI: STOP");
}
}
switch (currentState) {
case MOVING_UP:
case MOVING_DOWN:
controlMotor(currentState);
break;
case IDLE:
break;
}
handleLimitSwitches();
handleMotorTimeout();
}
// =================================== Fungsi Tambahan ===================================
// Orch-CalibrationDisplay
void calibrateLift() {
Serial.println("Memulai kalibrasi/homing...");
unsigned long startTime = millis();
currentState = CALIBRATING;
readAllSRInputs();
bool limitFloor1 = (bitRead(inputData2, LIMIT_FLOOR_1_BIT) == 0);
if (limitFloor1) {
currentFloor = 1;
Serial.println("Kalibrasi OK. Lift sudah berada di lantai 1.");
return;
}
stopMotor();
delay(MOTOR_DIRECTION_SWITCH_DELAY_MS);
Serial.println("Homing: Bergerak turun ke limit switch Lantai 1...");
digitalWrite(RELAY_MOTOR_DOWN, HIGH);
digitalWrite(RELAY_MOTOR_BRAKE, LOW);
unsigned long animationStartTime = millis();
while (!limitFloor1 && millis() - startTime < MOTOR_TIMEOUT_MS) {
readAllSRInputs();
limitFloor1 = (bitRead(inputData2, LIMIT_FLOOR_1_BIT) == 0);
if (limitFloor1) break;
if (bitRead(inputData2, LIMIT_FINAL_DOWN_BIT) == 0) {
Serial.println("Final limit switch bawah terpicu saat kalibrasi. Kalibrasi gagal.");
stopMotor();
currentState = CALIBRATION_FAILED;
bitSet(outputData2, EMERGENCY_LED_BIT);
writeAllSROutputs();
return;
}
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 29);
display.println("Homing to Reference");
display.setCursor(0, 39);
display.print("Mohon tunggu...");
unsigned long elapsedTime = millis() - animationStartTime;
int barWidth = map(elapsedTime, 0, 10000, 0, SCREEN_WIDTH);
if (barWidth > SCREEN_WIDTH) barWidth = SCREEN_WIDTH;
display.drawRect(0, 50, SCREEN_WIDTH, 5, WHITE);
display.fillRect(0, 51, barWidth, 3, WHITE);
display.display();
delay(10);
}
stopMotor();
readAllSRInputs();
limitFloor1 = (bitRead(inputData2, LIMIT_FLOOR_1_BIT) == 0);
if (limitFloor1) {
currentFloor = 1;
Serial.println("Kalibrasi Berhasil. Posisi: Lantai 1");
} else {
currentState = CALIBRATION_FAILED;
bitSet(outputData2, EMERGENCY_LED_BIT);
bitSet(outputData2, BUZZER_BIT);
writeAllSROutputs();
Serial.println("Kalibrasi GAGAL! Periksa kabel atau limit switch.");
}
}
void emergencyReset() {
Serial.println("Lift mengalami keadaan darurat.");
Serial.println("Memulai kalibrasi/homing otomatis ke Lantai 1...");
bitClear(outputData2, EMERGENCY_LED_BIT);
bitClear(outputData2, BUZZER_BIT);
isEmergency = false;
calibrateLift();
if (currentState != CALIBRATION_FAILED) {
currentState = IDLE;
Serial.println("Reset darurat selesai. Sistem kembali IDLE.");
}
}
void readDipSwitch() {
int dipValue = 0;
dipValue |= (digitalRead(DIP_PIN_0) == LOW) ? 1 : 0;
dipValue |= (digitalRead(DIP_PIN_1) == LOW) ? 2 : 0;
dipValue |= (digitalRead(DIP_PIN_2) == LOW) ? 4 : 0;
dipValue |= (digitalRead(DIP_PIN_3) == LOW) ? 8 : 0;
totalFloors = dipValue;
if (totalFloors < 2 || totalFloors > 15) totalFloors = 3;
Serial.print("Total Floors: ");
Serial.println(totalFloors);
}
void stopMotor() {
digitalWrite(RELAY_MOTOR_UP, LOW);
digitalWrite(RELAY_MOTOR_DOWN, LOW);
delay(30);
digitalWrite(RELAY_MOTOR_BRAKE, HIGH);
}
void controlMotor(ElevatorState state) {
if (state == MOVING_UP && currentFloor >= totalFloors) {
Serial.println("DI ATAS BATAS! Menghentikan motor.");
stopMotor();
currentState = IDLE;
return;
}
if (state == MOVING_DOWN && currentFloor <= 1) {
Serial.println("DI BAWAH BATAS! Menghentikan motor.");
stopMotor();
currentState = IDLE;
return;
}
if (state == MOVING_UP) {
if (lastDirection == MOVING_DOWN) {
stopMotor();
delay(MOTOR_DIRECTION_SWITCH_DELAY_MS);
}
digitalWrite(RELAY_MOTOR_UP, HIGH);
digitalWrite(RELAY_MOTOR_BRAKE, LOW);
} else if (state == MOVING_DOWN) {
if (lastDirection == MOVING_UP) {
stopMotor();
delay(MOTOR_DIRECTION_SWITCH_DELAY_MS);
}
digitalWrite(RELAY_MOTOR_DOWN, HIGH);
digitalWrite(RELAY_MOTOR_BRAKE, LOW);
}
}
// Orch-StatusDisplay
void displayStatus() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
if (currentState == CALIBRATION_FAILED) {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(5, 0);
display.println("--- HOMING ERROR ---");
display.setTextSize(1);
display.setCursor(0, 40);
display.print("Segera periksa kabel atau limit switch");
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 500) {
previousMillis = currentMillis;
dotCount = (dotCount + 1) % 4;
}
int dotX = display.getCursorX();
int dotY = display.getCursorY();
for (int i = 0; i < dotCount; i++) {
display.setCursor(dotX + (i * 6), dotY);
display.print(".");
}
display.display();
return;
}
if (currentState == CALIBRATING) {
return;
}
if (currentState == EMERGENCY_STOP) {
if (millis() - lastBlink > 250) {
blinkState = !blinkState;
lastBlink = millis();
}
if (blinkState) {
display.clearDisplay();
display.fillRect(0, 0, SCREEN_WIDTH, 40, WHITE);
display.setTextColor(BLACK);
display.setCursor(17, 4);
display.setTextSize(2);
display.print("LANTAI ");
display.println(currentFloor);
display.setCursor(17, 21);
display.setTextSize(2);
display.println("DARURAT!");
} else {
display.clearDisplay();
}
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 47);
display.println("DO NOT MOVE!");
display.setCursor(0, 57);
display.println("CALLING MAINTENANCE.");
display.display();
return;
}
display.setCursor(0, 0);
display.println("Orch.Gen Elevator");
display.drawLine(0, 9, SCREEN_WIDTH - 1, 9, WHITE);
display.setCursor(0, 47);
display.print("MODE: ");
switch (currentMode) {
case MODE_NORMAL:
display.println("NORMAL/AUTO");
break;
case MODE_TEST:
display.println("TEST");
break;
case MODE_INSPECTION:
display.println("INSPEKSI");
break;
}
display.setTextSize(3);
display.setCursor(0, 18);
display.print("F:");
display.println(currentFloor);
display.setCursor(0, 57);
display.setTextSize(1);
if (currentState == DOOR_OPENING || currentState == DOOR_CLOSING) {
display.print("DOOR: ");
if (currentState == DOOR_OPENING) {
display.print("OPENING");
} else {
display.print("CLOSING");
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 500) {
previousMillis = currentMillis;
dotCount = (dotCount + 1) % 4;
}
for (int i = 0; i < dotCount; i++) {
display.print(".");
}
} else if (currentState == DOOR_OPEN) {
display.print("DOOR: OPEN, Rem:(");
unsigned long elapsed = millis() - stateTimer;
int remaining = (DOOR_OPEN_HOLD_TIME_MS - elapsed) / 1000;
if (remaining < 0) remaining = 0;
display.print(remaining);
display.print("s)");
} else {
display.print("CALLS:");
displayCallQueue();
}
if (currentState == MOVING_UP || currentState == MOVING_DOWN) {
if (millis() - lastBlink > 250) {
blinkState = !blinkState;
lastBlink = millis();
}
if (blinkState) {
if (currentState == MOVING_UP) {
drawArrowUp(100, 20, 20);
} else {
drawArrowDown(100, 20, 20);
}
}
}
display.display();
}
void displayCallQueue() {
int y = 57, x = 47, colW = 25, rowH = 10;
int callCount = 0;
bool anyCall = false;
for (int i = 0; i < totalFloors; i++) {
if (callUp[i] || callDown[i]) {
display.setCursor(x + (callCount % 3) * colW, y + (callCount / 3) * rowH);
display.print("F");
display.print(i + 1);
callCount++;
anyCall = true;
}
}
if (!anyCall) {
display.setCursor(x, y);
display.print("NONE");
}
}
void processDoorAndLightButtons() {
unsigned long now = millis();
bool openButton = (bitRead(inputData1, DOOR_OPEN_BUTTON_BIT) == 0);
bool closeButton = (bitRead(inputData1, DOOR_CLOSE_BUTTON_BIT) == 0);
if (openButton && (currentState == DOOR_CLOSING || currentState == IDLE || currentState == DOOR_OPEN)) {
currentState = DOOR_OPENING;
stateTimer = now;
return;
}
if (closeButton && currentState == DOOR_OPEN) {
currentState = DOOR_CLOSING;
stateTimer = now;
return;
}
bool currentCabinLightButtonState = (bitRead(inputData1, CABIN_LIGHT_BUTTON_BIT) == 0);
if (lastCabinLightButtonState == HIGH && currentCabinLightButtonState == LOW && (now - lastCabinLightTime) > DEBOUNCE_DELAY_MS) {
cabinLightOn = !cabinLightOn;
lastCabinLightTime = now;
Serial.print("Lampu Kabin: ");
Serial.println(cabinLightOn ? "ON" : "OFF");
}
lastCabinLightButtonState = currentCabinLightButtonState;
}
void processCallButtons() {
unsigned long now = millis();
for (int i = 0; i < 3; i++) {
bool reading = (bitRead(inputData1, i) == 0);
if (reading && (now - lastButtonPressTime[i]) > DEBOUNCE_DELAY_MS) {
if (i + 1 == currentFloor) {
if (currentState == IDLE || currentState == DOOR_OPEN) {
Serial.println("Tombol lantai saat ini ditekan. Memicu pembukaan pintu.");
currentState = DOOR_OPENING;
stateTimer = now;
}
} else {
if (i + 1 > currentFloor) {
callUp[i] = true;
copCallActive[i] = true;
Serial.print("Panggilan COP ke Lantai ");
Serial.print(i + 1);
Serial.println(" ditambahkan ke antrean naik.");
} else if (i + 1 < currentFloor) {
callDown[i] = true;
copCallActive[i] = true;
Serial.print("Panggilan COP ke Lantai ");
Serial.print(i + 1);
Serial.println(" ditambahkan ke antrean turun.");
}
}
lastButtonPressTime[i] = now;
}
}
bool callUpButtonState = (bitRead(inputData1, LOP_CALL_UP_BIT) == 0);
bool callDownButtonState = (bitRead(inputData1, LOP_CALL_DOWN_BIT) == 0);
if (callUpButtonState && (now - lastCallUpTime) > DEBOUNCE_DELAY_MS) {
if (currentFloor < totalFloors) {
callUp[currentFloor] = true;
Serial.print("Panggilan LOP Naik dari Lantai ");
Serial.print(currentFloor + 1);
Serial.println(" ditambahkan ke antrean.");
}
lastCallUpTime = now;
}
if (callDownButtonState && (now - lastCallDownTime) > DEBOUNCE_DELAY_MS) {
if (currentFloor > 1) {
callDown[currentFloor - 2] = true;
Serial.print("Panggilan LOP Turun dari Lantai ");
Serial.print(currentFloor + 1);
Serial.println(" ditambahkan ke antrean.");
}
lastCallDownTime = now;
}
}
int findNextFloor() {
if (lastDirection == MOVING_UP) {
for (int i = currentFloor - 1; i < totalFloors; i++) {
if (callUp[i] || callDown[i]) {
return i + 1;
}
}
for (int i = totalFloors - 1; i >= 0; i--) {
if (callUp[i] || callDown[i]) {
lastDirection = MOVING_DOWN;
return i + 1;
}
}
} else {
for (int i = currentFloor - 1; i >= 0; i--) {
if (callDown[i] || callUp[i]) {
return i + 1;
}
}
for (int i = 0; i < totalFloors; i++) {
if (callDown[i] || callUp[i]) {
lastDirection = MOVING_UP;
return i + 1;
}
}
}
return -1;
}
void handleLimitSwitches() {
unsigned long now = millis();
for (int i = 0; i < totalFloors; i++) {
bool currentLimitStatus = (bitRead(inputData2, i) == 0);
if (currentLimitStatus != lastLimitStatus[i] && (now - lastLimitDebounceTime[i]) > LIMIT_DEBOUNCE_DELAY_MS) {
if (currentLimitStatus) {
currentFloor = i + 1;
copCallActive[i] = false;
Serial.print("Limit switch terpicu di lantai ");
Serial.println(currentFloor);
Serial1.print(currentFloor);
Serial1.print("\n");
Serial.print("Mengirim notifikasi ke dfmniplayer: lantai ");
Serial.println(currentFloor);
int nextFloor = findNextFloor();
if (nextFloor == currentFloor) {
stopMotor();
callUp[i] = false;
callDown[i] = false;
currentState = DOOR_OPENING;
stateTimer = now;
Serial.println("Mencapai lantai tujuan. Pintu akan terbuka.");
}
}
lastLimitStatus[i] = currentLimitStatus;
lastLimitDebounceTime[i] = now;
}
}
}
void handleMotorTimeout() {
if ((currentState == MOVING_UP || currentState == MOVING_DOWN) && (millis() - motorStartTime > MOTOR_TIMEOUT_MS)) {
Serial.println("Motor timeout! Lift tidak mencapai lantai dalam waktu yang ditentukan.");
isEmergency = true;
currentState = EMERGENCY_STOP;
stopMotor();
}
}
void handleEmergency() {
bool emergencyPinState = digitalRead(EMERGENCY_PIN) == LOW;
if (emergencyPinState && !isEmergency) {
isEmergency = true;
currentState = EMERGENCY_STOP;
stopMotor();
Serial.println("Tombol darurat ditekan!");
} else if (!emergencyPinState && isEmergency) {
isEmergency = false;
currentState = EMERGENCY_RESETTING;
Serial.println("Reset darurat.");
}
}
void drawArrowUp(int x, int y, int size) {
display.fillTriangle(x, y + size, x + size, y + size, x + size / 2, y, WHITE);
}
void drawArrowDown(int x, int y, int size) {
display.fillTriangle(x, y, x + size, y, x + size / 2, y + size, WHITE);
}
// Orch-StartupDisplay
void showStartup() {
display.clearDisplay();
display.setTextSize(3);
display.setCursor(120, 0);
display.setTextColor(SSD1306_WHITE);
display.print("Orch.Gen");
display.display();
for (int x = 120; x > 33; x -= 2) {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(x, 0);
display.print("Orch_");
display.println("");
display.print(" ELEVATOR");
display.display();
delay(10);
}
display.setTextSize(1);
display.setCursor(31, 50);
display.print("By Elzabid");
display.display();
delay(150);
}
void readAllSRInputs() {
digitalWrite(SR_IN_LATCH, LOW);
delayMicroseconds(5);
digitalWrite(SR_IN_LATCH, HIGH);
inputData2 = shiftIn(SR_IN_DATA, SR_IN_CLOCK, MSBFIRST);
inputData1 = shiftIn(SR_IN_DATA, SR_IN_CLOCK, MSBFIRST);
}
void writeAllSROutputs() {
digitalWrite(SR_OUT_LATCH, LOW);
shiftOut(SR_OUT_DATA, SR_OUT_CLOCK, MSBFIRST, outputData2);
shiftOut(SR_OUT_DATA, SR_OUT_CLOCK, MSBFIRST, outputData1);
digitalWrite(SR_OUT_LATCH, HIGH);
}
void updateFloorLeds() {
outputData1 = 0;
outputData2 = 0;
for (int i = 0; i < totalFloors; i++) {
if (copCallActive[i]) {
bitSet(outputData1, i);
}
}
bool anyCallUp = false;
for (int i = 0; i < totalFloors; i++) {
if (callUp[i]) {
anyCallUp = true;
break;
}
}
if (anyCallUp) {
bitSet(outputData1, LOP_CALL_UP_LED_BIT);
} else {
bitClear(outputData1, LOP_CALL_UP_LED_BIT);
}
bool anyCallDown = false;
for (int i = 0; i < totalFloors; i++) {
if (callDown[i]) {
anyCallDown = true;
break;
}
}
if (anyCallDown) {
bitSet(outputData1, LOP_CALL_DOWN_LED_BIT);
} else {
bitClear(outputData1, LOP_CALL_DOWN_LED_BIT);
}
if (bitRead(inputData1, DOOR_OPEN_BUTTON_BIT) == 0) {
bitSet(outputData1, DOOR_OPEN_INDICATOR_BIT);
} else {
bitClear(outputData1, DOOR_OPEN_INDICATOR_BIT);
}
if (bitRead(inputData1, DOOR_CLOSE_BUTTON_BIT) == 0) {
bitSet(outputData1, DOOR_CLOSE_INDICATOR_BIT);
} else {
bitClear(outputData1, DOOR_CLOSE_INDICATOR_BIT);
}
if (cabinLightOn) {
bitSet(outputData1, CABIN_LIGHT_BIT);
} else {
bitClear(outputData1, CABIN_LIGHT_BIT);
}
if (isEmergency || currentState == CALIBRATION_FAILED) {
bitSet(outputData2, EMERGENCY_LED_BIT);
bitSet(outputData2, BUZZER_BIT);
} else {
bitClear(outputData2, EMERGENCY_LED_BIT);
bitClear(outputData2, BUZZER_BIT);
}
writeAllSROutputs();
}