Hi all!
I am making my first project with Arduino, everything is going pretty well but I am stuck in the last part of code which I haven't managed to write yet as I don't understand how to do.
So my project is a Rocking Crib (as seen in the picture).
COMPONENTS:
arduino YUN
sound detector: Sound Detector Hookup Guide - SparkFun Learn
medium servo (60 degrees)
MORE IN DEPTH:
I will attach the microphone and when the baby cries, the Arduino will tell the servo to move and lull the baby (and crib).
I already managed to have the servo and microphone working.
The only problem is this:
the servo starts working only when the microphone senses loud noise and just for that amount of time. So let's say there's loud noise for 3 milliseconds, it will sweep only for these 3milliseconds. Here is the code I have:
#include <Servo.h>
#define PIN_ANALOG_IN A0 //microphone reading
#define THRESHOLD 400 //point when crib starts moving
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int sensorValue;
int pos; // variable to store the servo position
void setup()
{
pos = 0;
Serial.begin(9600);
myservo.attach(5); // attaches the servo on pin 5 to the servo object
}
void loop()
{
sensorValue = analogRead(PIN_ANALOG_IN);
if(sensorValue > THRESHOLD ) //microphone value
{
for(pos = 0; pos <= 60; pos += 1) // goes from 0 degrees to 180 degrees
{
Serial.println(pos);
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}
for(pos = 60; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}
}
} [/b][/b]
What I would like is the Arduino to make an average for let's say 10 seconds. If in those 10 seconds the noise is above let's say 400, then it will start sweep for at least 1 minute or so. It won't stop in the moment the noise stops.
Then it will check again, make average and from the average it will sweep or stop again.
Is there any good soul that would give me a little hand with it? I am really new on the Arduino world and I am so stuck with this last little piece. argh! Any little help is very much appreciated ![]()
Thank you so much in advance,
Marcella
