About Servo and LDR,Raindrop sensor, and Ultrasonic sensor

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);
				}
				
}

Thank you for helping me :smile:

motor.attach(8);
motor.write(90);

Before attach the servo write the position otherwise the servo will move to default position first.

motor.write(90);
motor.attach(8);

Use a boolean variable to set the window status.

Something like this:

// Global variable
bool windowClosed = false;

if(windowClosed == false)
{
 // Close the window
windowClosed = true;
}
else
{
// Do something else
}

Thank you for your reply, i will do more research on bool and try to use it on my coding. Have a nice day FernandoGarcia.

Thank you very much FernandoGarcia

#include<Servo.h>

/*bool windowClosed = false;
 if(windowClosed == false) 
{ // Close the window window
Closed = true;
 }
 else
 { // Do something else 
}*/

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;
int backward = 180;
int foward = 0;
int stop = 90;
Servo motor;

float brightness;
float rain;
float duration_us;
float distance_cm;

bool windowClosed = false;
bool windowOpen = false;

void setup()
{
				pinMode(ldr, INPUT);
				pinMode(raindrop, INPUT);
				pinMode(triggerPin, OUTPUT);
				pinMode(echoPin, INPUT);
				motor.write(90);
				motor.attach(8);
				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;
				
				if(brightness <= 100 && sensorvalue <= set /*&& distance_cm <= 20*/)
				{
				 if(windowOpen == false)
				 {
					motor.write(backward);
					delay(5000);
					motor.write(stop);
					windowOpen = true;
					windowClosed = false;		
				 }
				 else
				 {
					motor.write(stop);			
				 }
				}
				
				else if(brightness >= 100 || sensorvalue >= set /*&& distance_cm >= 20*/)
				{						
				 if(windowClosed == false)
				 {
				 motor.write(foward);
					delay(5000);
					motor.write(stop);
					windowClosed = true;
					windowOpen = false;
				 }
				 else
				 {
				 motor.write(stop);
				 }
				}
				
				delay(10);
}

this is my code when i use the bool variable. It worked. may god bless your day.

Gĺad to hear!

You don't needs of two variables because a can mean both things.

windowClosed = false; // the window is OPENED.

windowClosed = true; // the window is CLOSED.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.