制造商零件编号 PTS-00196-201
NANO FLIP ATMEGA328P EVAL BRD
PTSolns
License: Attribution Microcontrollers Sound Arduino
You may be old enough to remember the commercials of "The Clapper". Clap ON, Clap OFF. I sure do! I even had one back in the day. To reproduce the clapper nowadays is very easy. All the hard work is done by the microcontroller development board.
In this project, we will toggle the onboard LED ON and OFF, and vice versa, with two consecutive claps. The code handles the timing of the claps as can be seen below. We've made a short video to show the working principle of this project.
This is an entry-level project, but a good one for new users to get used to various parts. Once a user has better skills relating to microcontroller development boards, there are many possibilities. These little boards have become quite powerful and easy to use. This project also serves as a starting point from which more complicated projects can be made. I like to start with small building blocks and slowly add to them, making them more complex, until I've made the thing I wanted to make. It is often better to start small and add to something that works, as opposed to making a complicated project right from the start. Small simple steps.
There are only three parts to this project:
- The Nano Flip development board for processing the logic and executing commands
- The digital sound sensor to produce a digital signal of either 1 (HIGH) or 0 (LOW) signal and sends it to the Nano Flip
- The NTEA-LG expansion board that physically connects the Nano Flip with the sound sensor.
The sound sensor we are using is 5V tolerant, so go ahead and connect the GND to GND and VCC to 5V. For the digital pin, you can use any you like. We chose the D2 pin as this can also be used as an interrupt. E.g. if a sound is detected, an interrupt is triggered and something special can happen. But in this tutorial, we don't implement an interrupt and keep it as simple as possible.
The code is as follows.
// Tutorial: Making "The Clapper" with the digital sound sensor
// Last Update: Nov 5, 2024
//
// DESCRIPTION
// This quick tutorial uses the Nano Flip and the NTEA-LG to reproduce the popular "The Clapper".
//
// Pin definitions
const int soundSensorPin = 2; // Digital sound sensor input pin
const int ledPin = 13; // Built-in LED pin
// Timing variables
unsigned long firstClapTime = 0;
unsigned long secondClapTime = 0;
unsigned long lastClapTime = 0;
bool clapDetected = false;
bool ledState = false;
// Time limits in milliseconds
const unsigned long minClapInterval = 100; // Minimum time between claps
const unsigned long maxClapInterval = 2000; // Maximum time between claps
const unsigned long debounceTime = 300; // Time to ignore subsequent signals after a clap
void setup() {
pinMode(soundSensorPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
// Read the sensor and check for clap
if (digitalRead(soundSensorPin) == HIGH) {
unsigned long currentTime = millis();
// Check if enough time has passed since the last clap to consider it a new clap
if (currentTime - lastClapTime >= debounceTime) {
lastClapTime = currentTime; // Update the last clap time for debounce
if (!clapDetected) {
// First clap detected
firstClapTime = currentTime;
clapDetected = true;
} else {
// Second clap detected
secondClapTime = currentTime;
// Check if the second clap is within the valid time range
if ((secondClapTime - firstClapTime) >= minClapInterval &&
(secondClapTime - firstClapTime) <= maxClapInterval) {
// Toggle LED state
ledState = !ledState;
digitalWrite(ledPin, ledState);
clapDetected = false; // Reset clap detection
} else if ((secondClapTime - firstClapTime) > maxClapInterval) {
// Reset if time between claps exceeds the max interval
clapDetected = false;
}
}
}
}
}
Datasheets & supporting material on PTSolns Documentation Repository: https://docs.ptsolns.com