Rain Sensor Arduino Uno
-------------|-----------
VCC --|--> 5V
GND --|--> GND
DO --|--> Digital Pin 7
AO --|--> Analog Pin A0
#include <Servo.h>
Servo myservo;
int pos = 0;
int rainSensor = A0;
int soundSensor = A1;
int soundThreshold = 600;
void setup() {
myservo.attach(9);
pinMode(rainSensor, INPUT);
pinMode(soundSensor, INPUT);
}
void loop() {
int rainValue = analogRead(rainSensor);
int soundValue = analogRead(soundSensor);
if (rainValue == HIGH) {
myservo.write(90);
} else {
if (soundValue > soundThreshold) {
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
} else {
myservo.write(90);
}
}
}
automatic cradle using arduino servo motor and digital sound detector sensor it start to cradle when the baby starts crying code for thisTo create an automatic cradle using Arduino, servo motor, rain sensor, and digital sound detector sensor that starts to cradle when the baby starts crying, you can follow the below steps:
1. Connect the servo motor to the Arduino board and program it to move back and forth to create the cradling motion.
2. Connect the rain sensor to the Arduino board and program it to detect rain and stop the cradling motion in case it rains.
3. Connect the digital sound detector sensor to the Arduino board and program it to detect the baby's cry.
4. Write code that integrates the servo motor, rain sensor, and digital sound detector sensor to start the cradling motion when the baby cries and stop it when it rains.
Below is a sample code that you can use as a base for your project:
Code:#include <Servo.h> Servo myservo; int pos = 0; int rainSensor = A0; int soundSensor = A1; int soundThreshold = 600; void setup() { myservo.attach(9); pinMode(rainSensor, INPUT); pinMode(soundSensor, INPUT); } void loop() { int rainValue = analogRead(rainSensor); int soundValue = analogRead(soundSensor); if (rainValue == HIGH) { myservo.write(90); } else { if (soundValue > soundThreshold) { for (pos = 0; pos <= 180; pos += 1) { myservo.write(pos); delay(15); } for (pos = 180; pos >= 0; pos -= 1) { myservo.write(pos); delay(15); } } else { myservo.write(90); } } }
The code above reads the values from the rain sensor and sound detector sensor and checks if it's raining or if the baby is crying. If it's raining, it stops the cradling motion, but if the baby is crying, it starts the servo motor to create the cradling motion.
Note that you may need to adjust the sound threshold value depending on your specific sound detector sensor and the environment where you're using the cradle.
To create an automatic cradle using Arduino, servo motor, and digital sound detector sensor that starts to cradle when the baby starts crying, follow these steps:automatic cradle using arduino servo motor and digital sound detector sensor it start to cradle when the baby starts crying code for this
#include <Servo.h>
Servo myservo;
int pos = 0;
int soundSensor = A1;
int soundThreshold = 600;
void setup() {
myservo.attach(9);
pinMode(soundSensor, INPUT);
}
void loop() {
int soundValue = analogRead(soundSensor);
if (soundValue > soundThreshold) {
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
} else {
myservo.write(90);
}
}
the servo dosnt stop when the baby is stop cryingTo create an automatic cradle using Arduino, servo motor, and digital sound detector sensor that starts to cradle when the baby starts crying, follow these steps:
1. Connect the servo motor to the Arduino board and program it to move back and forth to create the cradling motion.
2. Connect the digital sound detector sensor to the Arduino board and program it to detect the baby's cry.
3. Write code that integrates the servo motor and digital sound detector sensor to start the cradling motion when the baby cries.
Below is a sample code that you can use as a base for your project:
Code:#include <Servo.h> Servo myservo; int pos = 0; int soundSensor = A1; int soundThreshold = 600; void setup() { myservo.attach(9); pinMode(soundSensor, INPUT); } void loop() { int soundValue = analogRead(soundSensor); if (soundValue > soundThreshold) { for (pos = 0; pos <= 180; pos += 1) { myservo.write(pos); delay(15); } for (pos = 180; pos >= 0; pos -= 1) { myservo.write(pos); delay(15); } } else { myservo.write(90); } }
The code above reads the value from the sound detector sensor and checks if the baby is crying. If the baby is crying, it starts the servo motor to create the cradling motion. If the baby is not crying, it stops the cradling motion.
Note that you may need to adjust the sound threshold value depending on your specific sound detector sensor and the environment where you're using the cradle.
To make the servo stop when the baby stops crying, you need to modify the code to add a delay before stopping the cradling motion. This delay will give the baby enough time to settle down before the servo stops moving.the servo dosnt stop when the baby is stop crying
#include <Servo.h>
Servo myservo;
int pos = 0;
int soundSensor = A1;
int soundThreshold = 600;
int quietTime = 5000; // quiet time duration in milliseconds
unsigned long lastCryTime = 0;
void setup() {
myservo.attach(9);
pinMode(soundSensor, INPUT);
}
void loop() {
int soundValue = analogRead(soundSensor);
if (soundValue > soundThreshold) {
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
lastCryTime = millis();
} else {
if (millis() - lastCryTime >= quietTime) {
myservo.write(90);
}
}
}
quietTime
, which represents the duration of quiet time needed for the cradling motion to stop. The lastCryTime
variable is used to keep track of when the baby last cried.lastCryTime
is updated to the current time. If the baby stops crying, the code checks if the quiet time duration has passed since the last cry. If it has, the servo stops moving.quietTime
to suit your needs. Also, make sure that your sound detector sensor is calibrated to detect the baby's cry accurately.servo will stop when the baby stop cryingTo make the servo stop when the baby stops crying, you need to modify the code to add a delay before stopping the cradling motion. This delay will give the baby enough time to settle down before the servo stops moving.
Here's an updated code that includes a delay and stops the cradling motion if the baby is not crying for a specified duration:
Code:#include <Servo.h> Servo myservo; int pos = 0; int soundSensor = A1; int soundThreshold = 600; int quietTime = 5000; // quiet time duration in milliseconds unsigned long lastCryTime = 0; void setup() { myservo.attach(9); pinMode(soundSensor, INPUT); } void loop() { int soundValue = analogRead(soundSensor); if (soundValue > soundThreshold) { for (pos = 0; pos <= 180; pos += 1) { myservo.write(pos); delay(15); } for (pos = 180; pos >= 0; pos -= 1) { myservo.write(pos); delay(15); } lastCryTime = millis(); } else { if (millis() - lastCryTime >= quietTime) { myservo.write(90); } } }
The updated code introduces a new variable calledquietTime
, which represents the duration of quiet time needed for the cradling motion to stop. ThelastCryTime
variable is used to keep track of when the baby last cried.
If the baby is crying, the servo starts moving andlastCryTime
is updated to the current time. If the baby stops crying, the code checks if the quiet time duration has passed since the last cry. If it has, the servo stops moving.
You can adjust the value ofquietTime
to suit your needs. Also, make sure that your sound detector sensor is calibrated to detect the baby's cry accurately.
To make the servo stop when the baby stops crying, you need to modify the code to add a delay before stopping the cradling motion. This delay will give the baby enough time to settle down before the servo stops moving.servo will stop when the baby stop crying
#include <Servo.h>
Servo myservo;
int pos = 0;
int soundSensor = A1;
int soundThreshold = 600;
int quietTime = 5000; // quiet time duration in milliseconds
unsigned long lastCryTime = 0;
void setup() {
myservo.attach(9);
pinMode(soundSensor, INPUT);
}
void loop() {
int soundValue = analogRead(soundSensor);
if (soundValue > soundThreshold) {
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
lastCryTime = millis();
} else {
if (millis() - lastCryTime >= quietTime) {
myservo.write(90);
}
}
}
quietTime
, which represents the duration of quiet time needed for the cradling motion to stop. The lastCryTime
variable is used to keep track of when the baby last cried.lastCryTime
is updated to the current time. If the baby stops crying, the code checks if the quiet time duration has passed since the last cry. If it has, the servo stops moving.quietTime
to suit your needs. Also, make sure that your sound detector sensor is calibrated to detect the baby's cry accurately.the servo cradle when the baby is crying and stop servo permanent if the baby stop cryingTo make the servo stop when the baby stops crying, you need to modify the code to add a delay before stopping the cradling motion. This delay will give the baby enough time to settle down before the servo stops moving.
Here's an updated code that includes a delay and stops the cradling motion if the baby is not crying for a specified duration:
Code:#include <Servo.h> Servo myservo; int pos = 0; int soundSensor = A1; int soundThreshold = 600; int quietTime = 5000; // quiet time duration in milliseconds unsigned long lastCryTime = 0; void setup() { myservo.attach(9); pinMode(soundSensor, INPUT); } void loop() { int soundValue = analogRead(soundSensor); if (soundValue > soundThreshold) { for (pos = 0; pos <= 180; pos += 1) { myservo.write(pos); delay(15); } for (pos = 180; pos >= 0; pos -= 1) { myservo.write(pos); delay(15); } lastCryTime = millis(); } else { if (millis() - lastCryTime >= quietTime) { myservo.write(90); } } }
The updated code introduces a new variable calledquietTime
, which represents the duration of quiet time needed for the cradling motion to stop. ThelastCryTime
variable is used to keep track of when the baby last cried.
If the baby is crying, the servo starts moving andlastCryTime
is updated to the current time. If the baby stops crying, the code checks if the quiet time duration has passed since the last cry. If it has, the servo stops moving.
You can adjust the value ofquietTime
to suit your needs. Also, make sure that your sound detector sensor is calibrated to detect the baby's cry accurately.
To make the servo stop permanently when the baby stops crying, you can modify the code to add a flag that indicates whether the baby is crying or not. When the baby starts crying, the flag isthe servo cradle when the baby is crying and stop servo permanent if the baby stop crying
incomplete informationTo make the servo stop permanently when the baby stops crying, you can modify the code to add a flag that indicates whether the baby is crying or not. When the baby starts crying, the flag is
Apologies for the incomplete response earlier. Here's an updated code that should work as per your requirements:incomplete information
#include <Servo.h>
Servo myservo;
int pos = 0;
int soundSensor = A1;
int soundThreshold = 600;
int quietTime = 5000; // quiet time duration in milliseconds
unsigned long lastCryTime = 0;
bool isCrying = false;
void setup() {
myservo.attach(9);
pinMode(soundSensor, INPUT);
}
void loop() {
int soundValue = analogRead(soundSensor);
if (soundValue > soundThreshold) {
if (!isCrying) {
isCrying = true;
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
lastCryTime = millis();
}
} else {
if (isCrying) {
if (millis() - lastCryTime >= quietTime) {
myservo.write(90);
isCrying = false;
}
}
}
}
isCrying
which keeps track of whether the baby is crying or not. When the baby starts crying, it sets the flag to true and starts the cradling motion. If the baby stops crying, the code checks if the quiet time duration has passed since the last cry. If it has, the servo stops moving and sets the flag back to false.code for cradle servo when sound sensor has detected amd stop the servo from cradle of there is no sound detectedApologies for the incomplete response earlier. Here's an updated code that should work as per your requirements:
Code:#include <Servo.h> Servo myservo; int pos = 0; int soundSensor = A1; int soundThreshold = 600; int quietTime = 5000; // quiet time duration in milliseconds unsigned long lastCryTime = 0; bool isCrying = false; void setup() { myservo.attach(9); pinMode(soundSensor, INPUT); } void loop() { int soundValue = analogRead(soundSensor); if (soundValue > soundThreshold) { if (!isCrying) { isCrying = true; for (pos = 0; pos <= 180; pos += 1) { myservo.write(pos); delay(15); } for (pos = 180; pos >= 0; pos -= 1) { myservo.write(pos); delay(15); } lastCryTime = millis(); } } else { if (isCrying) { if (millis() - lastCryTime >= quietTime) { myservo.write(90); isCrying = false; } } } }
The updated code adds a new boolean variable calledisCrying
which keeps track of whether the baby is crying or not. When the baby starts crying, it sets the flag to true and starts the cradling motion. If the baby stops crying, the code checks if the quiet time duration has passed since the last cry. If it has, the servo stops moving and sets the flag back to false.
You
#include <Servo.h>
const int soundPin = A0; // sound 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
Serial.println(soundValue); // print the value on serial monitor
// if sound is detected, move the servo
if (soundValue > 500) {
myServo.write(90); // move servo to 90 degrees
delay(1000); // wait for 1 second
}
// if no sound is detected, stop the servo
else {
myServo.write(0); // move servo to 0 degree
delay(100); // wait for 0.1 second
}
}
soundPin
variable is used to set the analog pin that the sound sensor is connected to. The servoPin
variable is used to set the digital pin that the servo motor is connected to.setup()
function, the serial monitor is started and the servo motor is attached to the servoPin
.loop()
function, the sound sensor value is read using the analogRead()
function and stored in the soundValue
variable. If the soundValue
is greater than 500, which means sound is detected, the servo motor is moved to 90 degrees using the write()
function. If no sound is detected, 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 sound sensor value again.additional rain sensor with same principle to the cradle servoHere's an example code using a sound sensor and a servo motor to move a cradle:
Code:#include <Servo.h> const int soundPin = A0; // sound 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 Serial.println(soundValue); // print the value on serial monitor // if sound is detected, move the servo if (soundValue > 500) { myServo.write(90); // move servo to 90 degrees delay(1000); // wait for 1 second } // if no sound is detected, stop the servo else { myServo.write(0); // move servo to 0 degree delay(100); // wait for 0.1 second } }
In this code, thesoundPin
variable is used to set the analog pin that the sound sensor is connected to. TheservoPin
variable is used to set the digital pin that the servo motor is connected to.
In thesetup()
function, the serial monitor is started and the servo motor is attached to theservoPin
.
In theloop()
function, the sound sensor value is read using theanalogRead()
function and stored in thesoundValue
variable. If thesoundValue
is greater than 500, which means sound is detected, the servo motor is moved to 90 degrees using thewrite()
function. If no sound is detected, the servo motor is stopped by moving it to 0 degree. Thedelay()
function is used to wait for a short time before checking the sound sensor value again.
Sure! Here's an example code that uses a rain sensor to control a servo motor:additional rain sensor with same principle to the cradle servo
#include <Servo.h>
const int rainPin = A0; // rain sensor pin
const int servoPin = 9; // servo motor pin
Servo myServo;
void setup() {
Serial.begin(9600);
myServo.attach(servoPin);
}
void loop() {
int rainValue = analogRead(rainPin); // read rain sensor value
Serial.println(rainValue); // print the value on serial monitor
// if it's raining, move the servo
if (rainValue < 500) {
myServo.write(90); // move servo to 90 degrees
delay(1000); // wait for 1 second
}
// if it's not raining, stop the servo
else {
myServo.write(0); // move servo to 0 degree
delay(100); // wait for 0.1 second
}
}
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.setup()
function, the serial monitor is started and the servo motor is attached to the servoPin
.loop()
function, the rain sensor value is read using the analogRead()
function and stored in the rainValue
variable. If the rainValue
is less than 500, which means it's raining, the servo motor is moved to 90 degrees using the write()
function. If it's not 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 rain sensor value again.additonal rain sensor to this codeHere's an example code using a sound sensor and a servo motor to move a cradle:
Code:#include <Servo.h> const int soundPin = A0; // sound 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 Serial.println(soundValue); // print the value on serial monitor // if sound is detected, move the servo if (soundValue > 500) { myServo.write(90); // move servo to 90 degrees delay(1000); // wait for 1 second } // if no sound is detected, stop the servo else { myServo.write(0); // move servo to 0 degree delay(100); // wait for 0.1 second } }
In this code, thesoundPin
variable is used to set the analog pin that the sound sensor is connected to. TheservoPin
variable is used to set the digital pin that the servo motor is connected to.
In thesetup()
function, the serial monitor is started and the servo motor is attached to theservoPin
.
In theloop()
function, the sound sensor value is read using theanalogRead()
function and stored in thesoundValue
variable. If thesoundValue
is greater than 500, which means sound is detected, the servo motor is moved to 90 degrees using thewrite()
function. If no sound is detected, the servo motor is stopped by moving it to 0 degree. Thedelay()
function is used to wait for a short time before checking the sound sensor value again.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?