// ---------- PIN DEFINITIONS ----------
const int pulseOutPin = 2; // Pin to send pulse
const int pulseInPin = 3; // Pin to receive pulse
const int buzzerPin = 4; // Buzzer pin
// ---------- TIMING ----------
const unsigned long sendInterval = 5000; // 5 seconds between pulses
const unsigned long receiveTimeout = 1000; // 1 second to wait for response
const int pulseDuration = 50; // 50 ms pulse
const int buzzerToneFreq = 1000; // Max tone 1000 Hz
const int buzzerDuration = 500; // 0.5 second buzzer duration
unsigned long lastSendTime = 0;
void setup() {
pinMode(pulseOutPin, OUTPUT);
pinMode(pulseInPin, INPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(pulseOutPin, LOW);
Serial.begin(9600);
}
void loop() {
unsigned long currentTime = millis();
// ---------- SEND PULSE EVERY 5 SECONDS ----------
if (currentTime - lastSendTime >= sendInterval) {
sendPulse();
lastSendTime = currentTime;
// ---------- WAIT FOR RESPONSE ----------
bool pulseReceived = waitForPulse(receiveTimeout);
if (!pulseReceived) {
// ---------- BUZZER IF NO PULSE RECEIVED ----------
tone(buzzerPin, buzzerToneFreq, buzzerDuration); // sound 1000 Hz
Serial.println("No pulse received! Buzzer ON.");
} else {
Serial.println("Pulse received. All good.");
}
}
}
// ---------- FUNCTION TO SEND PULSE ----------
void sendPulse() {
digitalWrite(pulseOutPin, HIGH);
delay(pulseDuration); // brief electrical pulse
digitalWrite(pulseOutPin, LOW);
Serial.println("Pulse sent.");
}
// ---------- FUNCTION TO WAIT FOR PULSE ----------
bool waitForPulse(unsigned long timeout) {
unsigned long startTime = millis();
while (millis() - startTime < timeout) {
if (digitalRead(pulseInPin) == HIGH) {
// pulse detected
return true;
}
}
return false; // no pulse received in timeframe
}