Smart Home Rain Protection Model– Assembly Video & Arduino Code

Code

// C++ Arduino Code (Copy Below)

#include <Servo.h> 

// --- Pins ---
const int PIN_SENSOR = A0;  // analog from soil moisture sensor
const int PIN_SERVO  = 9;   // servo signal

 

// --- Servo angles (adjust for your mechanism) ---
const int OPEN_ANGLE  = 0;    // cloths uncovered (dry)
const int CLOSE_ANGLE = 90;   // shield position (wet)

 

// --- Calibration ---
// Measure your sensor values first (see notes below).
// Typical modules (varies!): dry≈800–900, wet≈350–500

int DRY_VALUE = 850;   // reading in air / very dry
int WET_VALUE = 450;   // reading when water is on the sensor

// Hysteresis to avoid chatter: close when >= 60%, open when <= 45%
const int THRESH_WET   = 60;
const int THRESH_DRY   = 45;

// Smoothing and timing
const int SAMPLES = 10;                 // average N samples
const unsigned long COOLDOWN_MS = 800;  // ignore re-triggers for this time

Servo cover;
bool isClosed = false;
unsigned long lastMoveMs = 0;

int readMoisturePercent() {
  long sum = 0;

  for (int i = 0; i < SAMPLES; i++) {
    sum += analogRead(PIN_SENSOR);
    delay(5);
  }

  int raw = sum / SAMPLES;

  // Map raw to 0..100% (0 = dry, 100 = wet)
  int pct = map(raw, DRY_VALUE, WET_VALUE, 0, 100);
  if (pct < 0) pct = 0;
  if (pct > 100) pct = 100;

  return pct;
}

void goToAngle(int target) {

  // snap fast; change to a loop for smooth motion if you like
  cover.write(target);
}

void setup() {

  cover.attach(PIN_SERVO);
  pinMode(PIN_SENSOR, INPUT);
  goToAngle(OPEN_ANGLE);  // start open
  isClosed = false;

  Serial.begin(9600);
}

 

void loop() {
  int moisture = readMoisturePercent();
  unsigned long now = millis();

  // Close when wet enough; open again when dry enough (hysteresis)
  if (!isClosed && moisture >= THRESH_WET && (now - lastMoveMs) > COOLDOWN_MS) {

    goToAngle(CLOSE_ANGLE);  // move to 90°
    isClosed = true;
    lastMoveMs = now;

  } else if (isClosed && moisture <= THRESH_DRY && (now - lastMoveMs) > COOLDOWN_MS) {
    goToAngle(OPEN_ANGLE);   // back to 0°
    isClosed = false;
    lastMoveMs = now;
  }

  // Debug (optional)
  Serial.print("Moisture: ");
  Serial.print(moisture);
  Serial.println("%");

  delay(50);
}

What You Need for This Project

To build this Automatic Goalkeeper, we used the following parts from the Plzpapa Basic Robotic Kit:

  • Arduino Uno Board
  • Servo Motor (SG90)
  • Soil Moisture Sensor
  • Wooden Goalkeeper Structure
  • Jumper Wires
  • (No soldering required!)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top