#include <Bounce2.h> // Include the Bounce2 library
const int upButtonPin = 2; // Up button connected to pin 2
const int downButtonPin = 3; // Down button connected to pin 3
const int windowPinUp = 9; // Window motor connected to pin 9
const int windowPinDown = 10; // Window motor connected to pin 10
const int overLoadPin = 4; // Overload sensor connected to pin 4
int overLoadState = 0; // Variable to store the overload sensor state
Bounce debouncerUp = Bounce(); // Create a Bounce object for the up button
Bounce debouncerDown = Bounce(); // Create a Bounce object for the down button
void setup() {
pinMode(windowPinUp, OUTPUT);
pinMode(windowPinDown, OUTPUT);
pinMode(overLoadPin, INPUT);
debouncerUp.attach(upButtonPin); // Attach the debouncer to the up button pin
debouncerUp.interval(50); // Set debounce interval to 50 milliseconds
debouncerDown.attach(downButtonPin); // Attach the debouncer to the down button pin
debouncerDown.interval(50); // Set debounce interval to 50 milliseconds
}
void loop() {
debouncerUp.update(); // Update the up button debounce state
debouncerDown.update(); // Update the down button debounce state
overLoadState = digitalRead(overLoadPin);
if (debouncerUp.fell() && overLoadState == LOW) { // Up button pressed and no overload detected
openWindow(); // Open the window
}
else if (debouncerDown.fell() && overLoadState == LOW) { // Down button pressed and no overload detected
closeWindow(); // Close the window
}
else {
stopWindow(); // Stop the window motor
}
}
void openWindow() {
digitalWrite(windowPinUp, HIGH); // Activate the window motor to open the window
delay(1000); // Wait for 1 second
digitalWrite(windowPinUp, LOW); // Stop the window motor
}
void closeWindow() {
digitalWrite(windowPinDown, HIGH); // Activate the window motor to close the window
delay(1000); // Wait for 1 second
digitalWrite(windowPinDown, LOW); // Stop the window motor
}
void stopWindow() {
digitalWrite(windowPinUp, LOW); // Stop the window motor (up direction)
digitalWrite(windowPinDown, LOW); // Stop the window motor (down direction)
}