Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Clap-activated LED array
- // License: MIT
- //
- // Used for finding the biggest value int can be
- #include <limits.h>
- // Time delay (in milliseconds) between clap detection to avoid 1 clap registering as 2 claps. Adjust if needed.
- #define TIME_DELAY 100
- // Timeout duration (in milliseconds).
- #define CLAP_DETECT_TIME 500
- // Defines the clap sensitivity (i.e. how loud a sound has to be for it to be considered a clap).
- // This value can be adjusted to suit your hardware as different microphones have different sensitivities.
- // Too low and Arduino may detect background noise as a clap - too high and Arduino may not detect some claps.
- // In my tests, 3 seems ideal.
- #define CLAP_STRICTNESS 3
- // Constants that define the LED pins Arduino will output to. Change these according to your setup.
- const int led1Pin = 2;
- const int led2Pin = 3;
- const int led3Pin = 4;
- // Constant that defines the microphone input pin.
- const int micPin = A0;
- // VARIABLES
- // Global variables that store the state of the LEDs. Useful for toggling them on/off.
- bool led1PinState = false;
- bool led2PinState = false;
- bool led3PinState = false;
- // Dictates whether Arduino is currently detecting successive claps.
- bool detecting = false;
- // The number of claps detected in quick succession so far
- int clapCount = 0;
- // Last analog reading - used to determine difference in air pressure and hence whether there is a loud noise
- int lastAReading = INT_MAX;
- // The time (in milliseconds after Arduino reset) that the last clap was detected.
- unsigned long lastActivateMillis = 0;
- // Updates state of all LEDs
- void updateAllLEDs() {
- if (led1PinState) {
- digitalWrite(led1Pin, HIGH);
- } else {
- digitalWrite(led1Pin, LOW);
- }
- if (led2PinState) {
- digitalWrite(led2Pin, HIGH);
- } else {
- digitalWrite(led2Pin, LOW);
- }
- if (led3PinState) {
- digitalWrite(led3Pin, HIGH);
- } else {
- digitalWrite(led3Pin, LOW);
- }
- }
- void setup() {
- // Initialise input and output pins
- pinMode(led1Pin, OUTPUT);
- pinMode(led2Pin, OUTPUT);
- pinMode(led3Pin, OUTPUT);
- pinMode(micPin, INPUT);
- // Start 57600 baud connection via serial port (if present)
- Serial.begin(57600);
- }
- void loop() {
- // If currently detecting successive claps and another clap has not been detected for CLAP_DETECT_TIME milliseconds, process the command
- if (detecting && lastActivateMillis + CLAP_DETECT_TIME < millis()) {
- Serial.println("Done");
- // Print how many successive claps have been detected in total
- Serial.print("Claps: ");
- Serial.println(clapCount);
- switch (clapCount) {
- // If 2 claps, toggle LED 1
- case 2:
- led1PinState = !led1PinState;
- updateAllLEDs();
- break;
- // If 3 claps, toggle LED 2
- case 3:
- led2PinState = !led2PinState;
- updateAllLEDs();
- break;
- // If 4 claps, toggle LED 3
- case 4:
- led3PinState = !led3PinState;
- updateAllLEDs();
- break;
- // If 5 claps, turn all LEDs off
- case 5:
- led1PinState = false;
- led2PinState = false;
- led3PinState = false;
- updateAllLEDs();
- break;
- // If 6 claps, turn all LEDs on
- case 6:
- led1PinState = true;
- led2PinState = true;
- led3PinState = true;
- updateAllLEDs();
- break;
- // If 16 claps, do fancy stuff with LEDs
- case 16:
- // Flash all LEDs 16 times
- for (int i = 0; i < 16; i++) {
- digitalWrite(led1Pin, HIGH);
- digitalWrite(led2Pin, HIGH);
- digitalWrite(led3Pin, HIGH);
- delay(100);
- digitalWrite(led1Pin, LOW);
- digitalWrite(led2Pin, LOW);
- digitalWrite(led3Pin, LOW);
- delay(100);
- }
- // Display all numbers from 0 to 7 in binary 8 times
- for (int x = 0; x < 8; x++) {
- for (int i = 0; i < 8; i++) {
- led1PinState = (i >> 0) & 1;
- led2PinState = (i >> 1) & 1;
- led3PinState = (i >> 2) & 1;
- updateAllLEDs();
- delay(50);
- }
- }
- // Reset all LEDs
- led1PinState = false;
- led2PinState = false;
- led3PinState = false;
- updateAllLEDs();
- break;
- }
- // Reset clap count and stop detecting successive claps
- clapCount = 0;
- detecting = false;
- }
- // Read air pressure from microphone
- int aReading = analogRead(micPin);
- // If last clap was detected longer than 100 milliseconds ago, try detect another clap
- if (lastActivateMillis + TIME_DELAY < millis()) {
- // If sound was loud enough to be a clap
- if (aReading > lastAReading + CLAP_STRICTNESS) {
- // Make sure you are detecting successive claps
- detecting = true;
- // Reset the time that the last clap was detected
- lastActivateMillis = millis();
- // Add 1 to clap count
- clapCount++;
- // Print that Arduino is detecting successive claps
- if (clapCount == 1) {
- Serial.print("Detecting... ");
- }
- }
- }
- // Set lastAReading
- lastAReading = aReading;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement