const int potPin = A0;
const int motorDir1Pin = 8; // Пин управления направлением 1
const int motorDir2Pin = 9; // Пин управления направлением 2
const int motorDir1SimulatePin = 5; // Пин управления направлением 1
const int motorDir2SimulatePin = 4; // Пин управления направлением 2
const int motorEnablePin = 10; // Пин управления включением двигателя
const int buttonPin = 2;
const int buttonUpPin = 11;
const int buttonDownPin = 12;
const int ledPin = 3;
const int tempSensorPin = A1;
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
int potValue;
bool isOn = false;
bool isOpen = false;
bool isManualClosed = false;
bool prevButtonState = HIGH;
bool prevCloseButtonState = HIGH;
void setup() {
Serial.begin(115200);
pinMode(motorDir1SimulatePin, OUTPUT);
pinMode(motorDir2SimulatePin, OUTPUT);
pinMode(motorEnablePin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonUpPin, INPUT_PULLUP);
pinMode(buttonDownPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
potValue = analogRead(potPin);
if (potValue > 10) {
// Close panel
digitalWrite(motorDir1SimulatePin, LOW);
digitalWrite(motorDir2SimulatePin, HIGH);
digitalWrite(motorEnablePin, HIGH); // Включаем двигатель
isOpen = true;
Serial.print("closing panel 1 \n");
} else {
// is closed
digitalWrite(motorEnablePin, LOW); // Выключаем двигатель
isOpen = false;
Serial.print("is closed 1 \n");
}
digitalWrite(ledPin, HIGH);
}
void stopDisplay() {
if (digitalRead(motorEnablePin) != LOW) {
digitalWrite(motorEnablePin, LOW); // Выключаем двигатель
digitalWrite(motorDir1SimulatePin, LOW); //
digitalWrite(motorDir2SimulatePin, LOW); //
}
}
void openDisplay(int potValue) {
if (!isManualClosed) {
if (potValue < 1000) {
// Open panel
digitalWrite(motorDir1SimulatePin, HIGH);
digitalWrite(motorDir2SimulatePin, LOW);
digitalWrite(motorEnablePin, HIGH); // Включаем двигатель
Serial.print("opening panel \n");
} else {
stopDisplay();
Serial.print("is opened \n");
}
}
}
void closeDisplay(int potValue) {
if (potValue > 10) {
// Закрываем дисплей
digitalWrite(motorDir1SimulatePin, LOW);
digitalWrite(motorDir2SimulatePin, HIGH);
digitalWrite(motorEnablePin, HIGH); // Включаем двигатель
Serial.print("closing panel 2 \n");
} else {
stopDisplay();
Serial.print("is closed 2 \n");
}
}
void loop() {
potValue = analogRead(potPin);
bool upButtonState = digitalRead(buttonUpPin);
bool downButtonState = digitalRead(buttonDownPin);
bool buttonState = digitalRead(buttonPin);
if (isOn) {
openDisplay(potValue);
if (upButtonState == LOW) {
Serial.print("manual open\n");
isManualClosed = false;
}
if (downButtonState == LOW && prevCloseButtonState == HIGH) {
Serial.print("manual close\n");
isManualClosed = !isManualClosed;
// closeDisplay(potValue);
}
} else {
closeDisplay(potValue);
}
if (buttonState == LOW && prevButtonState == HIGH) {
isOn = !isOn;
}
if (isManualClosed) {
closeDisplay(potValue);
}
prevButtonState = buttonState;
prevCloseButtonState = downButtonState;
Serial.print("==================\n");
digitalWrite(ledPin, !digitalRead(ledPin));
delay(500);
}