Sie sind auf Seite 1von 2

// Define pin numbers for components

const int openButtonPin = 2; // Pin for the "open" button


const int closeButtonPin = 3; // Pin for the "close" button
const int mEnablePin = 4; // Motor enable pin
const int mDirPin = 5; // Motor direction pin
const int windPin = 6; // Wind sensor input pin
const int solarPin = A0; // Light sensor input pin (analog)

// Constants
const int windSpeedThreshold = 10; // Adjust this value based on the system
const int lightThreshold = 1023 * 0.25; // 25% of full scale for the light sensor
const int motorRunTime = 10000; // 10 seconds for motor operation

// Variables
bool userOverride = true; // Initial state allowing user control

void setup() {
// Setup pins
pinMode(openButtonPin, INPUT_PULLUP);
pinMode(closeButtonPin, INPUT_PULLUP);
pinMode(mEnablePin, OUTPUT);
pinMode(mDirPin, OUTPUT);

// Initialize motor and sensor states


digitalWrite(mEnablePin, LOW); // Motor off initially
digitalWrite(mDirPin, LOW); // Motor closed initially
}

void loop() {
// Check user buttons
if (userOverride) {
if (digitalRead(openButtonPin) == LOW) {
operateMotor(HIGH); // Open awning
} else if (digitalRead(closeButtonPin) == LOW) {
operateMotor(LOW); // Close awning
}
}

// Check wind speed requirement


if (checkWindSpeed()) {
operateMotor(LOW); // Close awning due to high wind speed
userOverride = false; // Disable user control
}

// Check light level requirement


if (checkLightLevel()) {
operateMotor(LOW); // Close awning due to low light level
userOverride = false; // Disable user control
}
}

void operateMotor(int direction) {


digitalWrite(mEnablePin, HIGH); // Enable motor
digitalWrite(mDirPin, direction); // Set motor direction

delay(motorRunTime); // Motor operates for a specified time

digitalWrite(mEnablePin, LOW); // Disable motor


}

bool checkWindSpeed() {
int windFrequency = pulseIn(windPin, HIGH); // Measure pulse frequency

// Calculate wind speed based on the proportion provided (adjust as needed)


float windSpeed = windFrequency / 10.0;

return windSpeed > windSpeedThreshold;


}

bool checkLightLevel() {
int lightLevel = analogRead(solarPin);

return lightLevel < lightThreshold;


}

Das könnte Ihnen auch gefallen