const int micPin = A0; // A0 микрофона → A0 Arduino
const int buzzerPin = 8; // S пищалки
const int buttonPin = 3; // S кнопки
const int threshold = 60; // Порог хлопка (тишина ~20, хлопок ~100)
bool alarmActive = false;
bool lastButtonState = HIGH;
bool clapLock = false; // Чтобы не сработало несколько раз подряд
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
// 1. Читаем микрофон
int micValue = analogRead(micPin);
// Хлопок: значение выше порога
if (micValue > threshold && !alarmActive && !clapLock) {
alarmActive = true;
clapLock = true;
Serial.print("CLAP! Value: ");
Serial.println(micValue);
}
// Снимаем блокировку, когда звук стих
if (micValue < threshold - 10) {
clapLock = false;
}
// 2. Кнопка — стоп
bool btnState = digitalRead(buttonPin);
if (btnState == HIGH && lastButtonState == LOW) {
delay(50);
if (digitalRead(buttonPin) == HIGH) {
alarmActive = false;
noTone(buzzerPin);
Serial.println("STOP");
}
}
lastButtonState = btnState;
// 3. Сирена
if (alarmActive) {
tone(buzzerPin, 1000);
delay(150);
tone(buzzerPin, 2000);
delay(150);
} else {
noTone(buzzerPin);
}
}