How to make this program like a clap/snap fingers switch, and filter out all excess noise?

to preface, "Fan" here is a pin connected to the emitter of a PNP transistor, with ground connected to base, so that when it's high, current can flow from collector to emitter, activating a fan powered externally by a 12V battery. The LED's, GreenLED and RedLED are both just normal LED's, designated to turn on and off in respect to the state of the fan. the sound sensor is so that i can clap or snap my fingers, and it will turn on for a set delay. however, issues i take with this circuit/program are as follows:

  1. outside noise causes it to go high at random moments
  2. i don't want a set delay, i want it to go indefinitely in a state until another clap/snap is heard
  3. I don't know how to make it so that it goes indefinitely in a state until another clap/snap is heard.

so finally, how to make it so that when it's off, a clap/snap will turn it on indefinitely, and when it's on, a clap/snap will turn it off indefinitely, as well as filtering out outside noises?

int Fan=27;
int micsensorpin=3;
int micState;
int RedLED=29;
int GreenLED=31;
void setup() {
// put your setup code here, to run once:
pinMode (Fan,OUTPUT);
pinMode(micsensorpin,INPUT);
pinMode (GreenLED,OUTPUT);
pinMode (RedLED,OUTPUT);
digitalWrite(micsensorpin,LOW);
digitalWrite(Fan,LOW);

}

void loop() {
// put your main code here, to run repeatedly
micState=digitalRead(micsensorpin);
if(micState==HIGH)
{
digitalWrite(Fan,HIGH);
digitalWrite(GreenLED,HIGH);
digitalWrite(RedLED,LOW);
delay (5000);
}
else
{
digitalWrite(Fan,LOW);
digitalWrite(GreenLED,LOW);
digitalWrite(RedLED,HIGH);
}

}

Please provide links to your clap sensor, and re-read your post… it doesn’t sound right
Clap on - Clap on.

Can you sketch out your circuit including power paths.

Thanks

Determine what a 'clap' looks like to a microcontroller. Then only trigger on that particular pattern.
Currently you're just triggering on any signal on pin 3 that happens to be detected as HIGH by the microcontroller. Any loud sound will therefore 'work' - which you don't want.

Start obviously with what @lastchancename said and tell more about the actual detection circuit. That will determine the (im)possibilities of sound detection.

Then do as I suggested and write a sketch that basically 'listens' to the sensor and outputs data at a rapid speed in a format you can evaluate later on (e.g. through Serial.print's). Use that 'research' sketch to analyze different sounds - a clap or a snap, but also outside noises. See if you can make a profile of the kind of sound you want the microcontroller to recognize as a snap/clap, and then write your actual sketch in such a way that it looks for that particular profile.
Hint: this will likely involve many more measurements and a sort of algorithm to analyze them than the single measurement you're doing now.

A 'finite state machine' is often used for this. It can be implemented fairly simply using a switch-case statement and a variable in which you store the present system state. Plenty of examples available also on this forum if you search a bit. In fact, just a few minutes ago one was posted in another thread: Milli() instead of delay due to incorrect execution of delays - #3 by de_botaniker1 It could easily be adjusted for your purpose.

The reliable detection of a clap/snap will be the hardest part, I imagine.

the model of the sound sensor

schematic:

That's not what your schematic shows. I'd look up some schematics of high-side PNP switches if I were you. And after that I'd forget what you saw and just use a low-side N-MOSFET switch (or NPN if you want to go the 1970s way with this.) Don't forget the freewheel diode!!
In any case, don't try to run a fan the way you've connected it now. There'll be trouble.

As to your microphone sensor: that looks like a pretty standard run-off-the-mill comparator circuit using a microphone as its input. Probably one half of the opamp amplifies the signal and the other half acts as a comparator, with the pot setting the bias for the comparator. That would be my guess, but I'm too lazy to chart it out based on the product photos.

Anyway, my earlier advice still stands as to creating the right sound profile and selectively triggering on it.

I assume "The Clapper" uses an analog high-pass filter plus some more filtering (analog or digital) to ignore continuous high-frequency sounds. The 2nd part is pretty-easy in software.

There's no way to absolutely-prevent false triggers, or misses if the clap is masked (drowned-out) by loud sounds.

Obviously, a clap has very-short duration so you can't have any delays while "listening" for the clap. Even without delays you can miss the peak of the clap because it only reads for an instant every time through the loop and the Arduino is rather slow. (I did some experiments once and it didn't miss the clap completely.) But I wasn't trying to do anything like what you're doing... I was just testing to see if a clap was too-short to "catch".

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