int pirSensorPin = 3;
int alarmBuzzerPin = 4;
int alarmLedPin = 5;
void setup() {
pinMode(pirSensorPin, INPUT);
pinMode(alarmBuzzerPin, OUTPUT);
pinMode(alarmLedPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int motionDetected = digitalRead(pirSensorPin); // 讀取 PIR 感測器狀態
if (motionDetected == HIGH) {
for (int i = 0; i < 5; i++) { // 讓 LED 閃爍 10 次
tone(alarmBuzzerPin, 1000); // 產生1kHz的音調
digitalWrite(alarmLedPin, HIGH);
delay(500);
noTone(alarmBuzzerPin); // 停止蜂鳴器
digitalWrite(alarmLedPin, LOW);
delay(500);
}
} else {
noTone(alarmBuzzerPin); // 確保蜂鳴器關閉
}
Serial.print("Motion Detected: ");
Serial.println(motionDetected == HIGH ? "YES" : "NO");
}