// added position switch serial prints for reference/debugging.
int rearWindowState = 0;
int frontWindowState = 0;
int rearBlindState = 0;
int frontBlindState = 0;
// Pin definitions for the rear window and blind
const int buttonARear = 13; // Open rear window button
const int buttonBRear = 12; // Close rear window button
const int buttonCRear = 11; // Close rear blind button
const int buttonDRear = 10; // Open rear blind button
const int buttonAFront = 9; // Open front window button
const int buttonBFront = 8; // Close front window button
const int buttonCFront = 7; // Close front blind button
const int buttonDFront = 6; // Open front blind button
const int windowOpenRelayRear = 37; // Rear window open relay
const int windowCloseRelayRear = 35; // Rear window close relay
const int blindOpenRelayRear = 33; // Rear blind open relay
const int blindCloseRelayRear = 31; // Rear blind close relay
const int windowOpenRelayFront = 29; // Front window open relay
const int windowCloseRelayFront = 27; // Front window close relay
const int blindOpenRelayFront = 25; // Front blind open relay
const int blindCloseRelayFront = 23; // Front blind close relay
const int windowOpenSwitchRear = 5; // Rear window position switch
const int blindCloseSwitchRear = 4; // Rear blind position switch
const int windowOpenSwitchFront = 3; // Front window position switch
const int blindCloseSwitchFront = 2; // Front blind position switch
const int rainSensor = 50; // Rain sensor (shared)
// Timing durations (in milliseconds)
const unsigned long windowOpenDuration = 10000;
const unsigned long windowCloseDuration = 12000;
const unsigned long blindCloseDuration = 15000;
const unsigned long blindOpenDuration = 17000;
// State variables for rear controls
bool windowMovingRear = false;
bool blindMovingRear = false;
bool isWindowOpeningRear = false;
bool isBlindClosingRear = false;
bool windowMovingFront = false;
bool blindMovingFront = false;
bool isWindowOpeningFront = false;
bool isBlindClosingFront = false;
unsigned long windowStartTimeRear = 0;
unsigned long blindStartTimeRear = 0;
unsigned long windowStartTimeFront = 0;
unsigned long blindStartTimeFront = 0;
// Button states for rear controls
bool buttonAStateRear = false, buttonAPrevStateRear = false;
bool buttonBStateRear = false, buttonBPrevStateRear = false;
bool buttonCStateRear = false, buttonCPrevStateRear = false;
bool buttonDStateRear = false, buttonDPrevStateRear = false;
bool buttonAStateFront = false, buttonAPrevStateFront = false;
bool buttonBStateFront = false, buttonBPrevStateFront = false;
bool buttonCStateFront = false, buttonCPrevStateFront = false;
bool buttonDStateFront = false, buttonDPrevStateFront = false;
void setup() {
Serial.begin (115200);
// Configure pins as input or output
pinMode(buttonARear, INPUT);
pinMode(buttonBRear, INPUT);
pinMode(buttonCRear, INPUT);
pinMode(buttonDRear, INPUT);
pinMode(buttonAFront, INPUT);
pinMode(buttonBFront, INPUT);
pinMode(buttonCFront, INPUT);
pinMode(buttonDFront, INPUT);
pinMode(windowOpenRelayRear, OUTPUT);
pinMode(windowCloseRelayRear, OUTPUT);
pinMode(blindOpenRelayRear, OUTPUT);
pinMode(blindCloseRelayRear, OUTPUT);
pinMode(windowOpenRelayFront, OUTPUT);
pinMode(windowCloseRelayFront, OUTPUT);
pinMode(blindOpenRelayFront, OUTPUT);
pinMode(blindCloseRelayFront, OUTPUT);
pinMode(windowOpenSwitchRear, INPUT);
pinMode(blindCloseSwitchRear, INPUT);
pinMode(windowOpenSwitchFront, INPUT);
pinMode(blindCloseSwitchFront, INPUT);
pinMode(rainSensor, INPUT);
// Deactivate all relays initially
stopWindowsMotorsRear();
stopBlindsMotorsRear();
stopWindowsMotorsFront();
stopBlindsMotorsFront();
}
void loop() {
//position switches readings
delay(200);
Serial.println();
// read the state of the pushbutton value:
rearWindowState = digitalRead(windowOpenSwitchRear);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (rearWindowState == 1) {
// turn LED on:
Serial.println("Rear Window is Open");
} else {
// turn LED off:
Serial.println("Rear Window is Closed");
}
// read the state of the pushbutton value:
frontWindowState = digitalRead(windowOpenSwitchFront);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (frontWindowState == 1) {
// turn LED on:
Serial.println("Front Window is Open");
} else {
// turn LED off:
Serial.println("Front Window is Closed");
}
// read the state of the pushbutton value:
rearBlindState = digitalRead(blindCloseSwitchRear);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (rearBlindState == 1) {
// turn LED on:
Serial.println("Rear Blind is Closed");
} else {
// turn LED off:
Serial.println("Rear Blind is Open");
}
// read the state of the pushbutton value:
frontBlindState = digitalRead(blindCloseSwitchFront);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (frontBlindState == 1) {
// turn LED on:
Serial.println("Front Blind is Closed");
} else {
// turn LED off:
Serial.println("Front Blind is Open");
}
//if (!digitalRead(blindCloseSwitchRear) == 1){
// Serial.println ("Blind Closed");
// digitalWrite(windowOpenRelayRear, HIGH); // Deactivate relay
//}
handleWindowAndBlindRear();
// Copy the function and adapt it for rear controls if needed
}
void handleWindowAndBlindRear() {
// Read button states
buttonAStateRear = !digitalRead(buttonARear);
delay (20);
buttonBStateRear = !digitalRead(buttonBRear);
delay (20);
buttonCStateRear = !digitalRead(buttonCRear);
delay (20);
buttonDStateRear = !digitalRead(buttonDRear);
delay (20);
buttonAStateFront = !digitalRead(buttonAFront);
delay (20);
buttonBStateFront = !digitalRead(buttonBFront);
delay (20);
buttonCStateFront = !digitalRead(buttonCFront);
delay (20);
buttonDStateFront = !digitalRead(buttonDFront);
delay (20);
// Rain sensor handling: close the window if rain is detected
if (digitalRead(rainSensor) == LOW && (digitalRead(windowOpenSwitchRear) || digitalRead(windowOpenSwitchFront))) {
closeWindowRear();
closeWindowFront();}
if (digitalRead(rainSensor) == LOW){
Serial.println ("Rain Detected");
}
// Rear window control
if (buttonAStateRear && !buttonAPrevStateRear && !digitalRead(blindCloseSwitchRear)) {
if (windowMovingRear && isWindowOpeningRear) {
stopWindowsMotorsRear();
windowMovingRear = false;
} else {
openWindowRear();
}
//if (!digitalRead(blindCloseSwitchRear) == 0){
//digitalWrite(windowOpenRelayRear, HIGH); // Deactivate relay
//}
}
if (buttonAStateFront && !buttonAPrevStateFront && !digitalRead(blindCloseSwitchFront)) {
if (windowMovingFront && isWindowOpeningFront) {
stopWindowsMotorsFront();
windowMovingFront = false;
} else {
openWindowFront();
}
}
if (buttonBStateRear && !buttonBPrevStateRear) {
if (windowMovingRear && !isWindowOpeningRear) {
stopWindowsMotorsRear();
windowMovingRear = false;
} else {
closeWindowRear();
}
}
if (buttonBStateFront && !buttonBPrevStateFront) {
if (windowMovingFront && !isWindowOpeningFront) {
stopWindowsMotorsFront();
windowMovingFront = false;
} else {
closeWindowFront();
}
}
// Rear blind control
if (buttonCStateRear && !buttonCPrevStateRear && !digitalRead(windowOpenSwitchRear)) {
if (blindMovingRear && isBlindClosingRear) {
stopBlindsMotorsRear();
blindMovingRear = false;
} else {
closeBlindRear();
}
}
if (buttonCStateFront && !buttonCPrevStateFront && !digitalRead(windowOpenSwitchFront)) {
if (blindMovingFront && isBlindClosingFront) {
stopBlindsMotorsFront();
blindMovingFront = false;
} else {
closeBlindFront();
}
}
if (buttonDStateRear && !buttonDPrevStateRear) {
if (blindMovingRear && !isBlindClosingRear) {
stopBlindsMotorsRear();
blindMovingRear = false;
} else {
openBlindRear();
}
}
if (buttonDStateFront && !buttonDPrevStateFront) {
if (blindMovingFront && !isBlindClosingFront) {
stopBlindsMotorsFront();
blindMovingFront = false;
} else {
openBlindFront();
}
}
// Timing for rear window
if (windowMovingRear) {
if (!digitalRead(blindCloseSwitchRear) == 0){
digitalWrite(windowOpenRelayRear, HIGH); // Deactivate relay
}
unsigned long currentTime = millis();
if (isWindowOpeningRear && (currentTime - windowStartTimeRear >= windowOpenDuration)) {
stopWindowsMotorsRear();
windowMovingRear = false;
}
if (!isWindowOpeningRear && (currentTime - windowStartTimeRear >= windowCloseDuration)) {
stopWindowsMotorsRear();
windowMovingRear = false;
}
}
// Timing for front window
if (windowMovingFront) {
if (!digitalRead(blindCloseSwitchFront) == 0){
digitalWrite(windowOpenRelayFront, HIGH); // Deactivate relay
} unsigned long currentTime = millis();
if (isWindowOpeningFront && (currentTime - windowStartTimeFront >= windowOpenDuration)) {
stopWindowsMotorsFront();
windowMovingFront = false;
}
if (!isWindowOpeningFront && (currentTime - windowStartTimeFront >= windowCloseDuration)) {
stopWindowsMotorsFront();
windowMovingFront = false;
}
}
// Timing for rear blind
if (blindMovingRear) {
if (!digitalRead(windowOpenSwitchRear) == 0){
digitalWrite(blindCloseRelayRear, HIGH); // Deactivate relay
}
unsigned long currentTime = millis();
if (isBlindClosingRear && (currentTime - blindStartTimeRear >= blindCloseDuration)) {
stopBlindsMotorsRear();
blindMovingRear = false;
}
if (!isBlindClosingRear && (currentTime - blindStartTimeRear >= blindOpenDuration)) {
stopBlindsMotorsRear();
blindMovingRear = false;
}
}
// Timing for front blind
if (blindMovingFront) {
if (!digitalRead(windowOpenSwitchFront) == 0){
digitalWrite(blindCloseRelayFront, HIGH); // Deactivate relay
}
unsigned long currentTime = millis();
if (isBlindClosingFront && (currentTime - blindStartTimeFront >= blindCloseDuration)) {
stopBlindsMotorsFront();
blindMovingFront = false;
}
if (!isBlindClosingFront && (currentTime - blindStartTimeFront >= blindOpenDuration)) {
stopBlindsMotorsFront();
blindMovingFront = false;
}
}
// Save previous button states
buttonAPrevStateRear = buttonAStateRear;
buttonBPrevStateRear = buttonBStateRear;
buttonCPrevStateRear = buttonCStateRear;
buttonDPrevStateRear = buttonDStateRear;
// Save previous button states
buttonAPrevStateFront = buttonAStateFront;
buttonBPrevStateFront = buttonBStateFront;
buttonCPrevStateFront = buttonCStateFront;
buttonDPrevStateFront = buttonDStateFront;
}
void openWindowRear() {
stopWindowsMotorsRear();
if (!digitalRead(blindCloseSwitchRear) == 1){
digitalWrite(windowOpenRelayRear, LOW); // Activate relay
windowStartTimeRear = millis();
windowMovingRear = true;
isWindowOpeningRear = true;
Serial.println ("Rear Window Opening");
}
//if (!digitalRead(blindCloseSwitchRear) == 0){
//digitalWrite(windowOpenRelayRear, HIGH); // Deactivate relay
//}
}
void closeWindowRear() {
stopWindowsMotorsRear();
digitalWrite(windowCloseRelayRear, LOW); // Activate relay
windowStartTimeRear = millis();
windowMovingRear = true;
isWindowOpeningRear = false;
Serial.println ("Rear Window Closing");
}
void openBlindRear() {
stopBlindsMotorsRear();
digitalWrite(blindOpenRelayRear, LOW); // Activate relay
blindStartTimeRear = millis();
blindMovingRear = true;
isBlindClosingRear = false;
Serial.println ("Rear Blind Opening");
}
void closeBlindRear() {
stopBlindsMotorsRear();
if (!digitalRead(windowOpenSwitchRear) == 1){
digitalWrite(blindCloseRelayRear, LOW); // Activate relay
blindStartTimeRear = millis();
blindMovingRear = true;
isBlindClosingRear = true;
Serial.println ("Rear Blind Closing");
} else {stopBlindsMotorsRear();
}
}
void stopWindowsMotorsRear() {
digitalWrite(windowOpenRelayRear, HIGH); // Deactivate relay
digitalWrite(windowCloseRelayRear, HIGH); // Deactivate relay
}
void stopBlindsMotorsRear() {
digitalWrite(blindOpenRelayRear, HIGH); // Deactivate relay
digitalWrite(blindCloseRelayRear, HIGH); // Deactivate relay
}
void openWindowFront() {
stopWindowsMotorsFront();
//if (!digitalRead(blindCloseSwitchFront) == 1){
digitalWrite(windowOpenRelayFront, LOW); // Activate relay
windowStartTimeFront = millis();
windowMovingFront = true;
isWindowOpeningFront = true;
Serial.println ("Front Window Opening");
//} else {stopWindowsMotorsFront();
//}
}
void closeWindowFront() {
stopWindowsMotorsFront();
digitalWrite(windowCloseRelayFront, LOW); // Activate relay
windowStartTimeFront = millis();
windowMovingFront = true;
isWindowOpeningFront = false;
Serial.println ("Front Window Closing");
}
void openBlindFront() {
stopBlindsMotorsFront();
digitalWrite(blindOpenRelayFront, LOW); // Activate relay
blindStartTimeFront = millis();
blindMovingFront = true;
isBlindClosingFront = false;
Serial.println ("Front Blind Opening");
}
void closeBlindFront() {
stopBlindsMotorsFront();
if (!digitalRead(windowOpenSwitchFront) == 1){
digitalWrite(blindCloseRelayFront, LOW); // Activate relay
blindStartTimeFront = millis();
blindMovingFront = true;
isBlindClosingFront = true;
Serial.println ("Front Blind Closing");
} else {stopBlindsMotorsFront();}
}
void stopWindowsMotorsFront() {
digitalWrite(windowOpenRelayFront, HIGH); // Deactivate relay
digitalWrite(windowCloseRelayFront, HIGH); // Deactivate relay
}
void stopBlindsMotorsFront() {
digitalWrite(blindOpenRelayFront, HIGH); // Deactivate relay
digitalWrite(blindCloseRelayFront, HIGH); // Deactivate relay
}