I recently got into this hobby. I just want to share with you guys my little project that counts the number of clapping made in a specified time.
I'm using an Arduino nano and just a sound sensor module for this project.
The circuit diagram might look like this (Let's just pretend that ir sensor is a sound sensor)
The code I'm using to turn on or off the led on pin 13 is the following
Where the CLAP_TIME is the time limit for the claps following the first clap.
#define SAMPLE_TIME 10
#define CLAP_TIME 2000
#define AUDIO 2
#define DELAY_TIME 100
unsigned long lastMillisec = 0;
unsigned long current_time = 0;
unsigned long elapsed_time = 0;
int sample = 0;
void setup()
{
pinMode(2, INPUT);
pinMode(13, OUTPUT);
}
void turnLed(boolean on)
{
if (on)
{
digitalWrite(13, HIGH);
}
else
digitalWrite(13, LOW);
}
int clap()
{
int clapCnt;
current_time = millis();
elapsed_time = current_time - lastMillisec;
if (digitalRead(AUDIO) == LOW)
sample++;
if (elapsed_time > SAMPLE_TIME)
{
if (sample >= 100)
{
delay(100);
clapCnt = 1;
sample = 0;
unsigned long timeStamp = millis();
lastMillisec = timeStamp;
while (millis() - timeStamp < CLAP_TIME)
{
current_time = millis();
elapsed_time = current_time - lastMillisec;
if (digitalRead(AUDIO) == LOW)
sample++;
if (elapsed_time > SAMPLE_TIME)
if (sample >= 100)
{
clapCnt++;
sample = 0;
lastMillisec = current_time;
delay(200);
}
}
return clapCnt;
}
sample = 0;
lastMillisec = current_time;
}
return 0;
}
void loop()
{
int i = clap();
if (i == 2)
{
turnLed(true);
}
else if (i == 3)
{
turnLed(false);
}
}
It works fine and I'm happy with it. Hope to hear everyone's comments about this small project.
Welcome to the forum
Good for you for getting something working which is always satisfying
A few comments
lastMillisec = current_time;
delay(200);
Mixing delay() and millis() timing in the same sketch does not sit well with me
To make it worse you have wrapped millis() timing in a while loop thus effectively stopping the execution of the loop() function. That may not matter in your current project but you should be aware of its effects
It is good that you have used a function to control the LED but it could be shorter because all you need to do is to write the value of the boolean passed to it to the LED pin using digitalWrite(). You do not need to test the value
You need to restructure your program so that loop() can run freely. One way to do this would be to use what is known as a state machine. It sounds scary but is really just a way of making sure that only the code for the current state of the program is run plus any other general purpose code that runs in loop()
It looks like your program has 3 states, so let's give them meaningful names
WAITING
COUNTING
RESULTS
My favourite way of implementing a state machine is to use switch/case. Something like this
switch (currentState)
{
case WAITING:
//code here to wait for first clap
//then change to COUNTING STATE
break;
case COUNTING:
//code here to count claps for a period
//when the period ends change to state RESULTS
break;
case RESULTS:
//code here to report and act on number of claps
//reset variables etc and change to state WAITING
break;
};
This code goes in loop() or a function called from it and it can call other functions to do what you need. It must not hold up the execution of loop() by using delay() or while()
Hey bob, I've done the state machine code and everything else for another project and made a board for It. It has some problems that I want to ask about, should I ask about it here or make another post?
I don’t see anything that says it is specific to clapping. I guess any loud noise will trigger it. I expected to see a FFT analysis of the frequency, a bit misleading tbh. Also this is not a question why is this marked solved? If you are asking for feedback, then this is not the right forum.