This circuit is designed to automatically control a motorized roof based on rain detection. It utilizes two Arduino Mega 2560 microcontrollers to manage inputs and outputs, a rain sensor to detect rain, an L298N DC motor driver to control two DC motors, a 12v battery as the power source, and a solar panel with a solar charge controller to recharge the battery.
// Define pins
const int rainSensorPin = 2; // Digital pin connected to rain sensor
const int motorPin1 = 9; // Motor driver input pin 1
const int motorPin2 = 10; // Motor driver input pin 2
// Define states
bool isRoofClosed = false;
void setup() {
// Initialize pins
pinMode(rainSensorPin, INPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
// Start with the roof open
openRoof();
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
int rainDetected = digitalRead(rainSensorPin);
if (rainDetected == LOW && !isRoofClosed) {
// Rain detected, close the roof
closeRoof();
} else if (rainDetected == HIGH && isRoofClosed) {
// No rain, open the roof
openRoof();
}
delay(1000); // Check every second
}
void closeRoof() {
Serial.println("Closing Roof...");
// Rotate motor to close the roof
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
// Assuming it takes 5 seconds to close the roof
delay(5000);
// Stop the motor
stopMotor();
isRoofClosed = true;
}
void openRoof() {
Serial.println("Opening Roof...");
// Rotate motor to open the roof
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
// Assuming it takes 5 seconds to open the roof
delay(5000);
// Stop the motor
stopMotor();
isRoofClosed = false;
}
void stopMotor() {
// Stop the motor by setting both inputs to LOW
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
}
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
This secondary controller currently has no specific functionality defined in the provided code. It may be used for future expansion or additional features.