Start over void loop with a mic sensor

Hello all,
I am working on a project, where I would be able to control the Arduino's loop with a mix sensor. I am not sure which code to use and searching the internet did not help me. I want to record noise levels with the mix and when the Noise passes a certain threshold, it will tell the device to start the loop from the beginning. If the Noise does not exceed the threshold, the loop should continue and start over when it is finished.

Here's the Code: I have two pumps attached to pins 12 and 13, Pins 8 and 9 are running through an amp into two speakers. The system should run as such and from time to time reset itself by detecting the Noise from the speakers. Is it possible to attach a mic to pin A1 for example and manually ajust the Noise level?

void setup() {
 Serial.begin(9600);
 randomSeed(analogRead(0));                
 pinMode(13, OUTPUT);                       
 pinMode(12, OUTPUT);                       
}

void loop() {
 digitalWrite(13, HIGH);
 delay(random(100,5000));
 digitalWrite(13, LOW);
 delay(random(10,10000));
 digitalWrite(12, HIGH);
 delay(random(100,5000));
 digitalWrite(12, LOW);
 delay(random(10,10000));             
 noTone(8);
 delay(1);
 tone(9, random(10,100),random(100,2000));
 delay(random(10,10000));
 noTone(9);
 delay(1);
 tone(8, random(10,100),random(100,2000));
 delay(random(10,10000));      
}

chilljazz:
Hello all,
I am working on a project, where I would be able to control the Arduino's loop with a mix sensor. I am not sure which code to use and searching the internet did not help me. I want to record noise levels with the mix and when the Noise passes a certain threshold, it will tell the device to start the loop from the beginning. If the Noise does not exceed the threshold, the loop should continue and start over when it is finished.

Here's the Code: I have two pumps attached to pins 12 and 13, Pins 8 and 9 are running through an amp into two speakers. The system should run as such and from time to time reset itself by detecting the Noise from the speakers. Is it possible to attach a mic to pin A1 for example and manually ajust the Noise level?

void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
}

void loop() {
digitalWrite(13, HIGH);
delay(random(100,5000));
digitalWrite(13, LOW);
delay(random(10,10000));
digitalWrite(12, HIGH);
delay(random(100,5000));
digitalWrite(12, LOW);
delay(random(10,10000));
noTone(8);
delay(1);
tone(9, random(10,100),random(100,2000));
delay(random(10,10000));
noTone(9);
delay(1);
tone(8, random(10,100),random(100,2000));
delay(random(10,10000));
}

Welcome to the Arduino forum. No one knows what you are describing because you are using words to name things that only you know what they mean.
You write of a "mix sensor" without giving examples or data sheets or anything so we know what it really is.
You want to record noise levels, but never define what is "noise" and what is not "noise". Where do you want to record this? We are working with voltage measurements here, NOT sound levels. Sound levels are converted to voltage levels by your Arduino program.
You write of "pumps", but obviously are not pumping any type of fluid. What are you really describing?
Good luck,
Paul

Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

loop() runs all the time, that's its job, to drive the whole system.

You need your actions in a separate function, and to call that function from loop only when its time
to do so.

You'd probably be best to use another function to do the noise sensing, which would also be called from
loop like this:

void loop()
{
  if (noise_threshold())
    do_actions () ;
}

Good code is lots of small functions each doing one thing well, and named carefully.

Have you looked at the thresholding microphone sensors that have a trimmer
adjustment on them? They might be the simplest sensor for your noise thresholding...

First of all, thank you for your responces. Paul, I'm sorry if I did not communicate my problem well enough. I'm fairly new to arduino and a beginner in coding.
I have made a small Soundbox which has mics in it (these have nothing to do with arduino, they record the noises and send them to my speakers). Arduino controls the soundmakers. These are two 5V Pumps which are run through a MOSFET module which is being turned on/off by pins 12 and 13. The other Soundmaker are two 8Ohm speakers, which are run through an amp module. These are connected via Pin 8 and 9. The operating length and delay between these soundmakers is determined by a random value which is being taken from the noise of the empty A0 pin. So what I want to do is to connect a mic module, this one to be exact which reads noise level throughout the whole process and shows these values in the serial plotter. I tried to find some infos about the meaning behind the numbers on the y-axis, but I did not find anything. The mic transfers the data to arduino, lowest value is 0 and highest is 200 for example, is there any way to tell the unit to restart the loop cycle when the mic reading pass the value of 100? If the value stays below that, the cycle will not be interrupted and will continue as always. At the end I want to have a responsive system where you can reset the process by knocking on the box or make it restart by itself when it makes too much noise.
-Thanks

You program whatever functionality you want - but first you have to describe that functionality as
an explicit set of actions to perform.

If you want to be able to interrupt the series of actions you have, they need to be event-driven as
a state-machine, not a single monolithic function full of delay() calls. delay() is the enemy of
responsiveness.

I see, thanks for clarification Mark. Unfortinately, I never worked with an event driven code (as you can probably see). Where do I find guides how to change my code into so I can use the function you showed me?

chilljazz:
I see, thanks for clarification Mark. Unfortinately, I never worked with an event driven code (as you can probably see). Where do I find guides how to change my code into so I can use the function you showed me?

You will find the following tutorials helpful:

BlinkWithoutDelay

Arduino Multiple Things

Several Things at a Time

Thank you all for the help. Nice evening to you all!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.