/*
Sketch: AuCP_WindowControl.ino
Created: 02-Mar-2023
Author: MicroBeaut
GitHub: https://github.com/MicroBeaut/
Youtube: https://www.youtube.com/channel/UCAztmS6ogRMH5spdH0bSmEg
*/
/*
Solution For:
Topics: Servos twitches when one sensor meets its condition but the other sensor does not
Category: Programming Questions
Link: https://forum.arduino.cc/t/servos-twitches-when-one-sensor-meets-its-condition-but-the-other-sensor-does-not/1096705
*/
#include <Servo.h>
#define CLOSE LOW
#define OPEN HIGH
#define sensorH A0
#define trigPin 7
#define echoPin 6
#define servoR 10
#define servoL 11
int WindowState = 0;
int humidity = 0;
Servo right;
Servo left;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
right.attach(servoR);
left.attach(servoL);
}
void loop() {
humidity = analogRead(sensorH);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = duration * 0.034 / 2;
if (distance <= 50 || humidity >= 500) {
WindowState = CLOSE;
} else {
WindowState = OPEN;
}
if (WindowState == CLOSE) {
right.write(90);
left.write(90);
} else {
right.write(0);
left.write(0);
}
delay(100);
}