Clap Counter

Difficulty: ● ● ○ ○ ○

  • Did you know ?

    Ancient cultures used rhythmic clapping and drumming as one of the earliest forms of long-distance communication—some signals could travel miles before modern technology existed.

No products found in parts_required metafield

How It Works ?

Clap detection: MEMS mic on GPIO7 → analog signal crosses threshold
Clap counting: Debounce 200ms → count claps within 1 second window
Display count: 5×5 LED matrix on GPIO16 → shows number or ‘X’ if >9
Vibration feedback: Vibro motor on GPIO17 → pulses equal to clap count
Idle state: Shows ‘0’ at startup → holds last count until new claps detected

GENESIS

Connection Guide

Snap your modules as shown above, copy the code into the Arduino IDE and Click upload

example.ino
#include <Adafruit_NeoPixel.h>

#define MIC_PIN     7
#define MATRIX_PIN  16
#define VIBRO_PIN   17
#define NUM_LEDS    25

const int threshold = 3000;
const int listenDelay = 1000;
const int debounceTime = 200;

int clapCount = 0;
unsigned long lastClapTime = 0;
bool clapsEverMade = false;

Adafruit_NeoPixel matrix(NUM_LEDS, MATRIX_PIN, NEO_GRB + NEO_KHZ800);

// 5×5 digits (0–9, X), index 0–10
const byte digitBitmaps[11][25] = {
  // 0
  {
    0,1,1,1,0,
    1,0,0,0,1,
    1,0,0,0,1,
    1,0,0,0,1,
    0,1,1,1,0
  },
  // 1
  {
    0,0,1,0,0,
    0,1,1,0,0,
    0,0,1,0,0,
    0,0,1,0,0,
    0,1,1,1,0
  },
  // 2
  {
    0,1,1,1,0,
    1,0,0,0,1,
    0,0,0,1,0,
    0,0,1,0,0,
    1,1,1,1,1
  },
  // 3
  {
    1,1,1,1,0,
    0,0,0,0,1,
    0,0,1,1,0,
    0,0,0,0,1,
    1,1,1,1,0
  },
  // 4
  {
    0,0,0,1,0,
    0,0,1,1,0,
    0,1,0,1,0,
    1,1,1,1,1,
    0,0,0,1,0
  },
  // 5
  {
    1,1,1,1,1,
    1,0,0,0,0,
    1,1,1,1,0,
    0,0,0,0,1,
    1,1,1,1,0
  },
  // 6
  {
    0,1,1,1,0,
    1,0,0,0,0,
    1,1,1,1,0,
    1,0,0,0,1,
    0,1,1,1,0
  },
  // 7
  {
    1,1,1,1,1,
    0,0,0,1,0,
    0,0,1,0,0,
    0,1,0,0,0,
    1,0,0,0,0
  },
  // 8
  {
    0,1,1,1,0,
    1,0,0,0,1,
    0,1,1,1,0,
    1,0,0,0,1,
    0,1,1,1,0
  },
  // 9
  {
    0,1,1,1,0,
    1,0,0,0,1,
    0,1,1,1,1,
    0,0,0,0,1,
    0,1,1,1,0
  },
  // X
  {
    1,0,0,0,1,
    0,1,0,1,0,
    0,0,1,0,0,
    0,1,0,1,0,
    1,0,0,0,1
  }
};

void setup() {
  pinMode(VIBRO_PIN, OUTPUT);
  analogReadResolution(12);
  Serial.begin(115200);

  matrix.begin();
  matrix.setBrightness(40);
  matrix.show();
  showDigit(0); // show 0 at startup only
}

void loop() {
  int micValue = analogRead(MIC_PIN);
  unsigned long now = millis();

  if (micValue > threshold) {
    if (now - lastClapTime > debounceTime) {
      clapCount++;
      lastClapTime = now;
      Serial.println("Clap!");
    }
  }

  if ((now - lastClapTime > listenDelay) && clapCount > 0) {
    Serial.print("Detected claps: ");
    Serial.println(clapCount);

    clapsEverMade = true;

    if (clapCount > 9) {
      blinkDigit(10, 1, true); // Show X
      showDigit(10);
    } else {
      blinkDigit(clapCount, clapCount, false);
      showDigit(clapCount);
    }

    clapCount = 0;
  }
}

// ---------------- Matrix and Vibro ----------------

void showDigit(byte index) {
  matrix.clear();
  for (int y = 0; y < 5; y++) {
    for (int x = 0; x < 5; x++) {
      // Rotate 90° CCW = pixel at (x, y) → (4 - y, x)
      int srcIndex = y * 5 + x;
      if (digitBitmaps[index][srcIndex]) {
        int rotatedX = 4 - y;
        int rotatedY = x;
        int pixel = rotatedY * 5 + rotatedX;
        matrix.setPixelColor(pixel, matrix.Color(0, 150, 0));
      }
    }
  }
  matrix.show();
}

void blinkDigit(byte index, int times, bool longBuzz) {
  for (int i = 0; i < times; i++) {
    showDigit(index);
    digitalWrite(VIBRO_PIN, HIGH);
    delay(100);
    clearMatrix();
    digitalWrite(VIBRO_PIN, LOW);
    delay(100);
  }

  if (longBuzz) {
    delay(200);
    longVibro();
  }
}

void clearMatrix() {
  matrix.clear();
  matrix.show();
}

void longVibro() {
  digitalWrite(VIBRO_PIN, HIGH);
  delay(400);
  digitalWrite(VIBRO_PIN, LOW);
}