#define SENSOR_PIN A0 // Sensor terhubung ke pin A0
#define LED_PIN A1 // LED merah terhubung ke pin A1
int clapCount = 0; // Jumlah tepukan
unsigned long detectionStartTime = 0;
unsigned long detectionTime = 0;
bool ledStatus = false;
void setup() {
Serial.begin(9600); // Mulai komunikasi serial pada 9600 baud
pinMode(SENSOR_PIN, INPUT); // Set pin sensor sebagai input
pinMode(LED_PIN, OUTPUT); // Set pin LED sebagai output
}
void loop() {
int sensorValue = analogRead(SENSOR_PIN); // Membaca nilai analog dari sensor
Serial.print("Tepukan: ");
Serial.println(clapCount);
// Threshold untuk mendeteksi tepukan (ubah nilai sesuai kebutuhan)
const int THRESHOLD = 512;
if (sensorValue > THRESHOLD) {
if (clapCount == 0) {
detectionStartTime = detectionTime = millis();
clapCount++;
} else if (millis() - detectionTime >= 50) {
detectionTime = millis();
clapCount++;
}
}
// Reset jika sudah melewati waktu deteksi
if (millis() - detectionStartTime >= 400) {
if (clapCount == 1) { // Hanya aktifkan LED jika ada satu tepukan
ledStatus = !ledStatus;
digitalWrite(LED_PIN, ledStatus ? HIGH : LOW);
}
clapCount = 0; // Reset jumlah tepukan
}
}