Detect multiple claps with clapping sensor

For school I am making a clapping sensor with my arduino nano. I have found some code to detect if there are 2 claps (link). But now I want to modify the code so it can distinguish if I clapped 1,2 or 3 times. I have now changed the source to detect 1 or 2 claps. But now if I clap twice, always a one clap is detected before it sees the 2 claps. And i have totally no idea how to detect 3 claps. Can someone please help me with this problem?

Code:

#define signalToRelayPin              12
#define sensorPin                      7

int lastSoundValue;
int soundValue;
long lastNoiseTime = 0;
long currentNoiseTime = 0;
long lastLightChange = 0;
int relayStatus = HIGH;

void setup() {
  pinMode(sensorPin, INPUT);
  pinMode(signalToRelayPin, OUTPUT);
  // put your setup code here, to run once:
  Serial.begin(115200);
}

struct DataBlockStruct  meting1,meting2;

void loop() {

  soundValue = digitalRead(sensorPin);
  currentNoiseTime = millis();

  if (soundValue == 1) { // if there is currently a noise
	if (
	  (currentNoiseTime > lastNoiseTime + 200) && // to debounce a sound occurring in more than a loop cycle as a single noise
	  (lastSoundValue == 0) &&  // if it was silent before
	  (currentNoiseTime < lastNoiseTime + 800) && // if current clap is less than 0.8 seconds after the first clap
	  (currentNoiseTime > lastLightChange + 1000) // to avoid taking a third clap as part of a pattern
	) {

	  relayStatus = !relayStatus;
	  Serial.println("2 X CLAP");

	 } else {    
	  Serial.println("1 X CLAP");
	  }

	 lastNoiseTime = currentNoiseTime;
  }

  lastSoundValue = soundValue;


}

ArvidBerends:
For school I am making a clapping sensor with my arduino nano. I have found some code to detect if there are 2 claps (link). But now I want to modify the code so it can distinguish if I clapped 1,2 or 3 times. I have now changed the source to detect 1 or 2 claps. But now if I clap twice, always a one clap is detected before it sees the 2 claps. And i have totally no idea how to detect 3 claps. Can someone please help me with this problem?

Code:

#define signalToRelayPin              12

#define sensorPin                      7

int lastSoundValue;
int soundValue;
long lastNoiseTime = 0;
long currentNoiseTime = 0;
long lastLightChange = 0;
int relayStatus = HIGH;

void setup() {
  pinMode(sensorPin, INPUT);
  pinMode(signalToRelayPin, OUTPUT);
  // put your setup code here, to run once:
  Serial.begin(115200);
}

struct DataBlockStruct  meting1,meting2;

void loop() {

soundValue = digitalRead(sensorPin);
  currentNoiseTime = millis();

if (soundValue == 1) { // if there is currently a noise
if (
  (currentNoiseTime > lastNoiseTime + 200) && // to debounce a sound occurring in more than a loop cycle as a single noise
  (lastSoundValue == 0) &&  // if it was silent before
  (currentNoiseTime < lastNoiseTime + 800) && // if current clap is less than 0.8 seconds after the first clap
  (currentNoiseTime > lastLightChange + 1000) // to avoid taking a third clap as part of a pattern
) {

  relayStatus = !relayStatus;
  Serial.println("2 X CLAP");

 } else {    
  Serial.println("1 X CLAP");
  }

 lastNoiseTime = currentNoiseTime;

}

lastSoundValue = soundValue;

}

I am not going to look at your code, but ask: How do you, as a person, determine if there is one clap, two claps, or three claps?

When you can reasonably answer that, then I may be able to give some direction.

Paul

You probably need to add "within a certain amount of time".