Nothing Moves

Hello! I’m currently working on a project and wrote a program for a small automated house model.

There is a small model window (attached to a servo motor) that moves from a fixed position of 90 degrees to a fixed position of 0 deg when water is detected on a Grove water sensor. If there is no water detected, it goes back to 90 degrees.

I’m not sure if I wrote it out correctly. I tried testing it out on an Arduino uno and nothing moved at all. Here’s my code:

#include <Servo.h>
#define rainSensor 13
#define EQ ==

Servo myservo;

void setup() {

 pinMode(rainSensor, INPUT_PULLUP);
  myservo.attach(12);
}

void loop() {
  
  digitalRead(rainSensor);
   
    if (rainSensor EQ LOW){             //rain sensor triggered
                              
           myservo.write(0);        //window closes
            delay(5000);
           }                               
   myservo.write(90);        //window is open
}

OK, very cute. You used a #define to make it look like your favourite language. Don't do that. It's fine to know more than one language and prefer to use just one of them. But when you're forced to use C++ you should use it like it's C++, not like your favourite. You will hit brick walls that can't be solved with a #define and you will miss out on the power of C++ that your other language doesn't do because you're simply not thinking in C++. You get the least of both.

You read the rain sensor pin, then ignored that value. Then you try comparing the pin number to LOW. 13 will never be equal to LOW.

Hi,
Try this.

#include <Servo.h>
#define rainSensorPin 13
bool rainSensorVal;


Servo myservo;

void setup() {

  pinMode(rainSensorPin, INPUT_PULLUP);
  myservo.attach(12);
}

void loop() {

  rainSensorVal = digitalRead(rainSensorPin);

  if (rainSensorVal == false) //Is rain sensor triggered?
  {
    myservo.write(0);        //window closes
  }
  else
  {
    myservo.write(90);        //window is open
  }
}

It compiles, but not tried.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
How are you powering the servo?
Tom… :slight_smile: