#include <Servo.h>
#include <Keypad.h>
#include <SevSeg.h>
Servo doorServo;
const int doorPin = 12;
const int doorOpenAngle = 90;
const int doorClosedAngle = 0;
const int stepPin = 3;
const int dirPin = 4;
SevSeg sevseg;
const int numDigits = 4;
int currentFloor = 1;
const byte ROW_NUM = 4;
const byte COLUMN_NUM = 4;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte pin_rows[ROW_NUM] = {11, 10, 9, 8};
byte pin_column[COLUMN_NUM] = {7, 6, 5, 4};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
const int callButton1 = 21;
const int callButton2 = 20;
const int callButton3 = 19;
void setup() {
// Инициализация сервопривода
doorServo.attach(doorPin);
doorServo.write(doorClosedAngle);
// Инициализация шагового двигателя
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// Инициализация семисегментного индикатора
byte numDigits = 1;
byte segmentPins[] = {25, 23, 53, 51, 49, 27, 29};
sevseg.begin(COMMON_ANODE, numDigits, segmentPins);
sevseg.setBrightness(90);
// Инициализация кнопок вызова
pinMode(callButton1, INPUT_PULLUP);
pinMode(callButton2, INPUT_PULLUP);
pinMode(callButton3, INPUT_PULLUP);
}
void loop() {
// Обработка нажатий кнопок вызова
if (digitalRead(callButton1) == LOW) {
currentFloor = 1;
moveElevator();
} else if (digitalRead(callButton2) == LOW) {
currentFloor = 2;
moveElevator();
} else if (digitalRead(callButton3) == LOW) {
currentFloor = 3;
moveElevator();
}
// Обработка ввода с клавиатуры
char key = keypad.getKey();
if (key) {
if (key == '1') {
currentFloor = 1;
moveElevator();
} else if (key == '2') {
currentFloor = 2;
moveElevator();
} else if (key == '3') {
currentFloor = 3;
moveElevator();
}
}
// Обновление отображения на семисегментном индикаторе
sevseg.setNumber(currentFloor);
sevseg.refreshDisplay();
}
void moveElevator() {
// Открытие дверей
doorServo.write(doorOpenAngle);
delay(5000); // Ожидание 5 секунд
// Закрытие дверей
doorServo.write(doorClosedAngle);
// Перемещение лифта
if (currentFloor == 1) {
digitalWrite(dirPin, LOW);
for (int i = 0; i < 2048; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
} else if (currentFloor == 2) {
digitalWrite(dirPin, HIGH);
for (int i = 0; i < 2048; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
} else if (currentFloor == 3) {
digitalWrite(dirPin, HIGH);
for (int i = 0; i < 2048; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
}
}