What's new
additonal rain sensor to this code
Sure! Here's an example code that uses both a sound sensor and a rain sensor to control a servo motor:

Code:
#include <Servo.h>

const int soundPin = A0; // sound sensor pin
const int rainPin = A1; // rain sensor pin
const int servoPin = 9; // servo motor pin

Servo myServo;

void setup() {
  Serial.begin(9600);
  myServo.attach(servoPin);
}

void loop() {
  int soundValue = analogRead(soundPin); // read sound sensor value
  int rainValue = analogRead(rainPin); // read rain sensor value
  Serial.print("Sound: ");
  Serial.print(soundValue);
  Serial.print(" | Rain: ");
  Serial.println(rainValue);

  // if sound is detected and it's not raining, move the servo
  if (soundValue > 500 && rainValue > 500) {
    myServo.write(90); // move servo to 90 degrees
    delay(1000); // wait for 1 second
  }
  // if no sound is detected or it's raining, stop the servo
  else {
    myServo.write(0); // move servo to 0 degree
    delay(100); // wait for 0.1 second
  }
}

In this code, the soundPin variable is used to set the analog pin that the sound sensor is connected to. The rainPin variable is used to set the analog pin that the rain sensor is connected to. The servoPin variable is used to set the digital pin that the servo motor is connected to.

In the setup() function, the serial monitor is started and the servo motor is attached to the servoPin.

In the loop() function, both the sound sensor value and rain sensor value are read using the analogRead() function and stored in the soundValue and rainValue variables. If the soundValue is greater than 500 and the rainValue is greater than 500, which means sound is detected and it's not raining, the servo motor is moved to 90 degrees using the write() function. If no sound is detected or it's raining, the servo motor is stopped by moving it to 0 degree. The delay() function is used to wait for a short time before checking the sensor values again.
 

Similar threads

Back
Top