Millis() for a sound sensor????? HELP!!!

Hello! Basically what I would like to is by using a sound sensor, send a signal to the arduino. However, I don't want the arduino to receive the signal for 3 seconds. For example, I want the sound sensor to detect sound for 3 seconds before it sends signal to arduino. Any guidence will be greatly appreciated!!! Thank you!

Study the Blink Without Delay code example for how to do two or more things at the same time -- in your case watch an input as well as the time. A more extensive set of examples is at the top of this forum listing (how to do several things at the same time...)

Link to several things at a time

When the sound is first detected you want to record millis() and then keep checking the difference between the recorded value and the current value of millis() until the difference is 3000 msecs. HOWEVER, if the sound stops during that period, you need to update the recorded value - effectively restarting the timer.

...R

Robin2:
Link to several things at a time

When the sound is first detected you want to record millis() and then keep checking the difference between the recorded value and the current value of millis() until the difference is 3000 msecs. HOWEVER, if the sound stops during that period, you need to update the recorded value - effectively restarting the timer.

...R

Thank you robin2 but I am new to this language. Is there any way you could provide me a basic example to work off of? Something maybe close to what I am trying to do? I am trying to learn this by looking at examples and studying to see what the code is doing. Then i try to change it to fit my application. Thank you!

Something a bit like this should work (not complete, not tested)

soundVal = digitalRead(soundPin); // or whatever is needed to get a value
if (soundVal == HIGH && soundON == false) {
    soundON = true;
    startMillis = millis();
}
if (soundVal == LOW) {
    soundON = false;
}
if (millis() - startMillis >= intervalMillis) {
   // time has expired - do stuff
}

...R

Robin2:
Something a bit like this should work (not complete, not tested)

soundVal = digitalRead(soundPin); // or whatever is needed to get a value

if (soundVal == HIGH && soundON == false) {
    soundON = true;
    startMillis = millis();
}
if (soundVal == LOW) {
    soundON = false;
}
if (millis() - startMillis >= intervalMillis) {
  // time has expired - do stuff
}




...R

Thank you! However, I am having a really hard time understanding what's going on here. I apologize for being so dumb with this

TheROO:
Thank you! However, I am having a really hard time understanding what's going on here. I apologize for being so dumb with this

If you can explain what you don't understand I will try to help.

...R

Robin2:
If you can explain what you don’t understand I will try to help.

…R

I don’t understand what the code is saying. For example what are the lines in the code saying? Please look at the comments I made in your sketch

Robin2:

soundVal = digitalRead(soundPin); // or whatever is needed to get a value

if (soundVal == HIGH && soundON == false) // what does this do??
{
    soundON = true;
    startMillis = millis(); // what does this do?
}
if (soundVal == LOW) { //what does this do??
    soundON = false;
}
if (millis() - startMillis >= intervalMillis) { // what does this do??
  // time has expired - do stuff
}




...R

I appreciate your help a lot. And also you said that this isn’t complete. What else would I have to do to make it complete? How would I put the “if sound is detected for 3 seconds, do this” part? I’m confused on the 3 sec part.

Thanks!

soundVal = digitalRead(soundPin); // or whatever is needed to get a value
I don’t know how you plan to determine the sound - so this is just an illustration

if (soundVal == HIGH && soundON == false) // what does this do??
if the sound is now HIGH and a variable called soundON has the value false
{
soundON = true;
startMillis = millis(); // what does this do?
save the value of millis() - millis() gives you the number of milliseconds since the Arduino was started
}
if (soundVal == LOW) { //what does this do??
check to see if the sound has stopped before the interval has expired
soundON = false;
}
if (millis() - startMillis >= intervalMillis) { // what does this do??
checks to see has the interval expired - how many msecs since millis() was previously saved
// time has expired - do stuff
}

Your questions suggest that you are very unfamiliar with programming. If so it would be useful to study several of the example programs that come with the Arduino IDE and also to study things like millis() in the reference section of this website.

…R

Robin2:
Your questions suggest that you are very unfamiliar with programming. If so it would be useful to study several of the example programs that come with the Arduino IDE and also to study things like millis() in the reference section of this website.

…R

Thanks robin2. does the attached code look like it will work? Do you see any problems? Thanks!

sound_lights_led.ino (740 Bytes)

The Arduino is a 'try it and see' environment.
Does it work for you?
If not, tell us what it's (not) doing that's wrong.

TheROO:
Thanks robin2. does the attached code look like it will work? Do you see any problems? Thanks!

I don't think that will work. For one thing you are mixing up the concepts of soundVal and soundON from my example. Also you have not defined your variable soundStart at the top of your program and I think you don't have all your {} in pairs.

Why not stick as closely as possible with the code I suggested.

As @Henry_Best, has said, learn-by-doing.

...R

Your conditionals are really hard to follow.

"if (soundStart == 0)" doesn't really have a place in a program trying to do what you want...

Try sitting down with a pen and paper - set up columns with all the variables involved along the top, now be the code yourself and run down through it, at each point you need to change a variable do it, but you don't then carry 'down' the variable so you keep track of what state each one is at. Run though some loops with nothing happening - i.e no input - then try flipping the input to high, see what happens ...

Sometimes it's the best thing to do.

That way you can figure out what is going wrong, an actual unit won't tell you this stuff! (well, with an LCD or serial connection you can sometimes get it to...)

You might want to try Robins code next - I'm guessing you could learn something from it :wink:

Robin2:
I don't think that will work. For one thing you are mixing up the concepts of soundVal and soundON from my example. Also you have not defined your variable soundStart at the top of your program and I think you don't have all your {} in pairs.

Why not stick as closely as possible with the code I suggested.

As @Henry_Best, has said, learn-by-doing.

...R

What does soundON mean in your code?

TheROO:
What does soundON mean in your code?

It is just a variable to record the state of things - a way for the code to remember what was the value the last time we checked. In this case to record that the sound is ON (true) or NOT ON (false).

...R

M

Robin2:
It is just a variable to record the state of things - a way for the code to remember what was the value the last time we checked. In this case to record that the sound is ON (true) or NOT ON (false).

…R

So should I have to declare that at the top of my code? If so, example??

TheROO:
So should I have to declare that at the top of my code? If so, example??

Yes. C/C++ requires every variable to be declared before it can be used.

In this case it can be defined either as a boolean or as a byte

boolean soundON = false;
or
byte soundON = 0;

...R

Robin2:
Something a bit like this should work (not complete, not tested)

soundVal = digitalRead(soundPin); // or whatever is needed to get a value

if (soundVal == HIGH && soundON == false) {
    soundON = true;
    startMillis = millis();
}
if (soundVal == LOW) {
    soundON = false;
}
if (millis() - startMillis >= intervalMillis) {
  // time has expired - do stuff
}




...R

So I have taken the past few days to study this code and think about somethings. Quick question. Would the startMillis and intervalMillis need to be defined with the other intervals at the top? If so, how. Also I was looking to do when it's held down for 3 seconds do something, how would I incorperate the 3 seconds in the code? Is that the intervalMillis? Once I get this straighter out I think I'll get it. Thanks for everyone's help and suggestions.

TheROO:
Would the startMillis and intervalMillis need to be defined with the other intervals at the top?

Yes - that is why I said the code is not complete. And after my Reply #16 I thought you would have figured that out.

If so, how.

The "how" is the same as in the example several things at a time that I linked to in Reply #2

Also I was looking to do when it's held down for 3 seconds do something

Get it working first, then come back to this question

...R

Alrighty then :slightly_smiling_face: ;D thanks