Servos twitches when one sensor meets its condition but the other sensor does not

Hey, so I basically just started doing this project (Also clarify that I'm not putting together this yet, I'ts being done in Tinkercad right now).

I decided to do an automatic windows with a few functions (In a smaller scale), the functions are that when someone from outside the house is too close, the windows close automatically, this function works with an ultrasound sensor, and the other one is that when it's raining the windows also closes automatically, this one is done with the "soil moisture sensor".

This is supposed to be done by the servos rotating 90 degrees, but for some reason when one or both of the conditions are true the servos just start twitching, I have been trying to figure out why this happens, but no idea, I don't know if this is a problem with the simulation since I'm a beginner so if anyone could help me it would be great. (I'm attaching the code and an image of the simulation)

Here is the code:

#include <Servo.h>

const int sensorH = A0;
const int trigPin = 7;
const int echoPin = 6;
int WindowState = 0;
int servoR = 10;
int servoL = 11;
int humidity = 0;
int distanceA = 0;
int humidityA = 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 && WindowState == 0) {
    distanceA = 1;
  } 
  else if(distance > 50 || WindowState == 1) {
    distanceA = 0;
  }

  if (humidity > 500 && WindowState == 0) {
    humidityA = 1;
  }
  else if (humidity < 500 || WindowState == 1 ){
    humidityA = 0;
  }
  
  if (distanceA == 1 || humidityA == 1){
    WindowState = 1;
  }
  else if (distanceA == 0 && humidityA == 0){
    WindowState = 0;
  }
  
  if (WindowState == 1){
    right.write(90);
    left.write(90);
  }
  else if (WindowState == 0){
    right.write(0);
    left.write(0);
  }
  delay(100);
}

If you are supplying power to both servos from Arduino, you are hurting Arduino and not giving the servos enough power. Supply the servos with power externally.

Arduino Simulator: WindowsControl.ino

  1. Add two more variables to understand the window state clearly

    #define CLOSE LOW
    #define OPEN  HIGH
    
  2. Not an issue, but better to use #define instead of declaring a variable for pins

    #define sensorH A0
    #define trigPin 7
    #define echoPin 6
    #define servoR  10
    #define servoL  11
    
    int windowState = 0;
    int humidity = 0;
    
  3. Remove the unnecessary variables, or you can keep maintaining them,
    int distanceA = 0;
    int humidityA = 0;

  4. Modify your code on the condition of distance and humidity.

    Revised void loop() code.

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

    μB.

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