Hello, this is my first time using arduino for my final year project. My group has decided to make an automatic sliding window, that open when day and sunny, and close when night or rain. i think that i almost get the coding mosty complete, but when i run the arduino, after the first action of servo to close the window, it continue the action of closing a window. I use LDR to detect day and night, Raindrop sensor to detect rain or not, and ultrasonic sensor to detect the window is close or open. This is my code:
#include<Servo.h>
const int ldr = A4;
const int triggerPin = 6;
const int echoPin = 7;
const int raindrop = A0;
const int led = 12;
int set = 10;
int i;
Servo motor;
float brightness;
float rain;
float duration_us;
float distance_cm;
void setup()
{
pinMode(ldr, INPUT);
pinMode(raindrop, INPUT);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
motor.attach(8);
motor.write(90);
digitalWrite(12,LOW);
}
void loop()
{
rain = analogRead(raindrop);
int sensorvalue = map(rain, 0, 1023, 225, 0);
brightness = analogRead(ldr);
int distance_threshold = 20;
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
duration_us = pulseIn(echoPin, HIGH);
distance_cm = 0.017*duration_us;
//tutup
if(brightness >= 100 || sensorvalue >= set && distance_cm >= 20)
{
motor.write(45);
delay(4000);
motor.write(90);
}
//buka
else if(brightness <= 100 && sensorvalue <= set && distance_cm <= 20)
{
motor.write(135);
delay(4000);
motor.write(90);
}
else
{
motor.write(90);
}
}