sound triggered baby crib rocker

hi,

I’m a happy grandfather and here is my project: (sorry for my poor english, I’m french)

I want to connect a servomotor (activated by a microphone) to my grandson bed in order to rock him during 5 minutes when he is crying.

I’ve tried this scketch (see below) It works very well, but I don’t know how to include the microphone command and the 5 mn time during which the servo is activated until the next crying.

PS: the micro
https://fr.aliexpress.com/item/3pin-Voix-Capteur-De-D-tection-Sonore-Module-Intelligent-Smart-V-hicule-Robot-H-licopt-re/32677423078.html?spm=a2g0s.9042311.0.0.szl9X3

the] servo:

thanks for your help

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position
char command;
void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}


void loop()
{
  for(pos = 0; pos < 90; pos += 1)  // entre < et ; mettre le debattement
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5);                       // le chiffre fait varier la vitesse d'aller (a regler eventuellement avec un potentiometre)
  }
  for(pos = 90; pos>=1; pos-=1)     // entre < et ; mettre le debattement
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5
    );                       // le chiffre fait varier la vitesse de retour
  }
}

Hi,

eleph:
I don’t know how to include the microphone command and the 5 mn time during which the servo is activated until the next crying.

Have a look at Measure Sound Levels with the Adafruit Microphone Amplifier
The connections to the Arduino will be the same for your microphone board.

Go on to the “Measuring Sound Levels” page and run that “Example Sound Level Sketch”

That shows you how to get a ‘volts’ level that you can test to see if he is crying.

If you take the code you have for the servo and make it a separate function, like this:

void rockTheCradle()
{
  for(pos = 0; pos < 90; pos += 1)  // entre < et ; mettre le debattement
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5);                       // le chiffre fait varier la vitesse d'aller (a regler eventuellement avec un potentiometre)
  }
  for(pos = 90; pos>=1; pos-=1)     // entre < et ; mettre le debattement
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5
    );                       // le chiffre fait varier la vitesse de retour
  }
}

Then in your new loop() where you test the sound level, you can very simply add:

if( TEST_IF_SOUND_LEVEL_MEANS_HE_IS_CRYING )
{
    // rock the cradle 100 times
    for( int count=0; count<100; count++ ){
        rockTheCradle();
    }

}

Yours,
TonyWilk

thanks for your reply tonywilk,

I’m a beginner with arduino and I’ve tried to follow your instructions,
is it all right?

any suggestions ?
(the microphone board have a potentiometer to adjust sensibility.) that is why I only use digitalRead(micro)==HIGH

is it ok?

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position
char command;
const int micro =3;

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode (micro,INPUT)
}

void rock the craddle ()
{
  for(pos = 0; pos < 90; pos += 1)  // entre < et ; mettre le debattement
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5);                       // le chiffre fait varier la vitesse d'aller (a regler eventuellement avec un potentiometre)
  }
  for(pos = 90; pos>=1; pos-=1)     // entre < et ; mettre le debattement
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5
    );                       // le chiffre fait varier la vitesse de retour
  }
}  

void loop()

if(digitalRead(micro)==HIGH)
{
    // rock the cradle 100 times
    for( int count=0; count<100; count++ ){
        rockTheCradle();
    }

}

after some corrections … (with auto formatter )

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position
char command;
const int micro = 3;

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode (micro, INPUT);
}

void rockTheCradle ()
{
  for (pos = 0; pos < 90; pos += 1) // entre < et ; mettre le debattement
  { // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5);                       // le chiffre fait varier la vitesse d'aller (a regler eventuellement avec un potentiometre)
  }
  for (pos = 90; pos >= 1; pos -= 1) // entre < et ; mettre le debattement
  {
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5
         );                       // le chiffre fait varier la vitesse de retour
  }
}

void loop()
{
  if (digitalRead(micro) == HIGH)
  {
    // rock the cradle 100 times
    for ( int count = 0; count < 100; count++ ) {
      rockTheCradle();
    }

  }  }

eleph:
(the microphone board have a potentiometer to adjust sensibility.) that is why I only use digitalRead(micro)==HIGH

is it ok?

Does it work ?

Just using "digitalRead(micro)==HIGH" should be OK if the microphone board will give a large enough signal.

If it does not work, you would have to use an analog input and "analogRead(micro) > A_NUMBER"

Yours,
TonyWilk

I've tried without the microphone which is not yet delivered from china.

I've connected a jumper on 10 (microphone pin) and not connected the other end, and ....
big surprise, it works !!!
when I disconnect and reconnect, It works again ...

there something magic in arduino :slight_smile:

but I don't understand, any explanation ?
I would have understand if I have connected the 10 pin to the 5V pin but ????

eleph:
but I don't understand, any explanation ?
I would have understand if I have connected the 10 pin to the 5V pin but ????

Because the microphone pin is not connected, it is 'floating'. It can be HIGH or LOW.

if you set the pin to be:

  pinMode (micro, INPUT_PULLUP);

Then it will always be pulled HIGH.
Connect the pin to GND and it will go LOW.
Disconnect and it will go HIGH again.

Yours,
TonyWilk

I've tried what you suggest,

connected to GND it does works, it's ok,

but disconnected, it run without end,
it doesn't respect the number of count.

????

thanks for your help, you seems to be arduino "nobel prize" comparing my arduino "low level"
Since how many years did you work on arduino ?

connected to GND it does NOT works, it’s ok, (I’ve forgotten one word)

I think you’re referring to this part of your code:

void loop()
{
  if (digitalRead(micro) == HIGH)
  {
    // rock the cradle 100 times
    for ( int count = 0; count < 100; count++ ) {
      rockTheCradle();
    }
  }
}

This will stop when count reaches 100, and the micro is LOW again. If micro is still high, it will start another run of 100 rocks.