Status
Not open for further replies.

Kulantoy

Forum Veteran
wiring diagram of automatic baby cry cradle using:

Arduino
servo motor
rain sensor
sound detector
buzzer
speaker
 
swing servo motor back and forth when sound is detected by sound sensor
Here is an example code for Arduino that swings a servo motor back and forth when sound is detected by a sound sensor:

Code:
C++
#include <Servo.h>

Servo myservo;
int soundSensor = A0;
int soundThreshold = 500;
int servoPos = 90; // start at 90 degrees
int servoStep = 1;

void setup() {
  myservo.attach(9);
  pinMode(soundSensor, INPUT);
}

void loop() {
  int soundValue = analogRead(soundSensor);
  if (soundValue > soundThreshold) {
    servoStep = -servoStep; // change direction
  }
  servoPos += servoStep;
  servoPos = constrain(servoPos, 0, 180); // limit servo position
  myservo.write(servoPos);
  delay(10);
}

This code starts by setting up a Servo object and initializing the sound sensor pin and threshold. In the loop() function, it reads the sound sensor value and checks if it is above the threshold. If it is, it changes the direction of the servo motor. The servoPos variable is then updated by adding the servoStep variable, which determines the swing range of the servo motor. The constrain() function is used to limit the value of servoPos between 0 and 180, which is the range of motion of a standard servo motor. Finally, the servoPos value is written to the servo motor using the myservo.write() function and a delay is added to slow down the movement.

Note: This code assumes that the sound sensor outputs an analog value between 0 and 1023. You may need to adjust the soundThreshold value to suit your particular sound sensor. Additionally, you may need to adjust the servoStep value to change the range of motion of the servo motor.
 
Here is an example code for Arduino that swings a servo motor back and forth when sound is detected by a sound sensor:

Code:
C++
#include <Servo.h>

Servo myservo;
int soundSensor = A0;
int soundThreshold = 500;
int servoPos = 90; // start at 90 degrees
int servoStep = 1;

void setup() {
  myservo.attach(9);
  pinMode(soundSensor, INPUT);
}

void loop() {
  int soundValue = analogRead(soundSensor);
  if (soundValue > soundThreshold) {
    servoStep = -servoStep; // change direction
  }
  servoPos += servoStep;
  servoPos = constrain(servoPos, 0, 180); // limit servo position
  myservo.write(servoPos);
  delay(10);
}

This code starts by setting up a Servo object and initializing the sound sensor pin and threshold. In the loop() function, it reads the sound sensor value and checks if it is above the threshold. If it is, it changes the direction of the servo motor. The servoPos variable is then updated by adding the servoStep variable, which determines the swing range of the servo motor. The constrain() function is used to limit the value of servoPos between 0 and 180, which is the range of motion of a standard servo motor. Finally, the servoPos value is written to the servo motor using the myservo.write() function and a delay is added to slow down the movement.

Note: This code assumes that the sound sensor outputs an analog value between 0 and 1023. You may need to adjust the soundThreshold value to suit your particular sound sensor. Additionally, you may need to adjust the servoStep value to change the range of motion of the servo motor.
swing a servo motor back and forth
 
swing a servo motor back and forth
Here is an example code for Arduino that swings a servo motor back and forth:

Code:
C++
#include <Servo.h>

Servo myservo;
int servoPos = 90; // start at 90 degrees
int servoStep = 1;

void setup() {
  myservo.attach(9);
}

void loop() {
  if (servoPos == 0) {
    servoStep = 1; // change direction
  } else if (servoPos == 180) {
    servoStep = -1; // change direction
  }
  servoPos += servoStep;
  myservo.write(servoPos);
  delay(10);
}

This code sets up a Servo object and attaches it to pin 9. In the loop() function, it checks if the servoPos variable has reached the limits of the servo's range of motion. If it has, it changes the direction of the servo motor by updating the servoStep variable. The servoPos variable is then updated by adding the servoStep variable, which determines the swing range of the servo motor. Finally, the servoPos value is written to the servo motor using the myservo.write() function and a delay is added to slow down the movement.

Note: This code assumes that the servo motor has a range of motion of 0 to 180 degrees. You may need to adjust the servoPos variable and the servoStep variable to suit your particular servo motor.
 
Status
Not open for further replies.

About this Thread

  • 22
    Replies
  • 1K
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
971
Guests online
1,247
Total visitors
2,218

Forum statistics

Threads
2,273,520
Posts
28,950,121
Members
1,235,795
Latest member
cardodalisay0702
Back
Top