Pausing between analog read

So I have an issue.

I need help figuring out how to pause between analog reads (if you couldn’t guess that). I won’t get into the detail on what the code is used for (not enough time), but my problem is that as you blow into the microphone, thresholdPass goes up and up and up. I only need it to increase one, pause, and increase one on the next blow, pause, then increase again on the next. There is a digital output, as well as analog output, on the microphone, but it’s too sensitive to read between I/O (unless someone knows better).

I’m in a bit of rush, so if haven’t shared enough details, my apologies! Ask any questions you need.

int threshold = 700;
int thresholdPass = 0;

void setup() {
// put your setup code here, to run once:

pinMode(22, OUTPUT);
pinMode(23, OUTPUT);
pinMode(24, OUTPUT);

Serial.begin(9600);

}

void loop() {
// put your main code here, to run repeatedly:

Serial.print(analogRead(A6));
Serial.print(" “);
Serial.print(thresholdPass);
Serial.print(” “);
Serial.print(digitalRead(22));
Serial.print(” “);
Serial.print(digitalRead(23));
Serial.print(” “);
Serial.print(digitalRead(24));
Serial.println(” ");

if(analogRead(A6) > threshold) { //reads input from a microphone to see when air is blown into it
thresholdPass += 1;
}

if(thresholdPass == 3) { //condition if air is blown 3 times
digitalWrite(22, HIGH);
digitalWrite(23, HIGH);
digitalWrite(24, HIGH);
}
}

Have a look at the State Change example. It works on a digitalRead of a pin, like a button or something. But the concept it uses to handle counting each event only once will extend to what you are doing. You just need to track what the value was last time and only react if it is above the threshold now but wasn't last time.

Don't pause.

Think about it as a "state machine." What are the states that the system must go through to reach the finish? I'm reading between the lines but I think you want something like...

Person blows on the microphone (threshold exceeded for at least 0.5 seconds) Person stops blowing (under threshold for at least 0.5 seconds but not more than 2 seconds) Person blows on the microphone (threshold exceeded for at least 0.5 seconds) Person stops blowing (under threshold for at least 0.5 seconds but not more than 2 seconds) Person blows on the microphone (threshold exceeded for at least 0.5 seconds) Device does its thing. After 2 seconds the device stops doing its thing and goes back to the top to wait for another blow.

There's a lot more steps there than you've put into your program. In fact, there's going to be even more steps inside each of those.

The characteristics of the analog input need to be considered. A loud noise at the microphone will make an audio waveform which goes up and down rapidly. So you will get some readings "above threshold" and some "below" even though the noise sounds continuous to your ear. So you need to ignore small periods of time (milliseconds) when it's below threshold. That's why I put in specific times in both the "above" and "below" cases.

I really appreciate the responses!

I'll have to go check out the state change and refresh my brain.

In regards to MorganS,

That is more or less the idea. In brief, we're using a bellows to blow in a microphone and turn on an electric fire place as if you were really giving oxygen to the embers. We want it to seem more realistic than not, so we've decided that you'll need to use the bellows 3 times in succession for it to finally activate.

I do have a couple of other things that I need to add to my code, but this is the first hurdle that I've ran into that's a little too tough for me to jump on my own.

I wasn't married to the pausing idea...I more so meant that when the threshold number is passed (the max is 744 (for some reason, instead of 1023)), I need an int to increase by one number and one number only. Not one number every few milliseconds while above the threshold.

I hope that makes sense.

P.S. In typing out this response, I think I figured out a way to achieve this same effect by changing

if(thresholdPass == 3)

to

if(thresholdPass >= 100)

It will allow the numbers to climb naturally, this still providing the same effect of needing to use the bellows more than one.

With that being said, I'm still curious to know if there is a way to achieve what I initially wanted to achieve.

Think about the loop running thousands of times per second.

Record the time that the threshold was first passed.

The next time around the loop, look at that time and the current time to decide if this is still part of the same ‘blow’ event or if the blowing has stopped for the required amount of time.

This is similar to a “debounce”. Look up that concept.

I won't get into the detail on what the code is used for (not enough time)

I'm in a bit of rush, so if haven't shared enough details, my apologies! Ask any questions you need.

You don't have enough time to do it right the first time, but you have time to do it over? I don't get it.

So debounce and state change. Got it.

I really appreciate the help!

PaulS,

I was in a rush because I was leaving a job and only had so much time to type before I needed to get in my car and leave.