int chirpPin = 8; //the pin on the microcontroller that will send the tone signal to the pizoelectric buzzer. THIS MUST BE A PWM PIN int inputPin = 4; //the pin on the microcontroller that will recieve a high signal when the PIR sensor detects motion. int chirpTime = 50; //length of chirp in ms int chirpTone = 3951; //freq of chirp tone unsigned long motionDelay = 120; //delay between motion and chirp in seconds unsigned long chirpInterval = 90; //delay between chirps in seconds unsigned long nextChirp = 0; //the time for the next chirp void setup() { //setup the pins for input and output pinMode(inputPin, INPUT); pinMode(chirpPin, OUTPUT); //start the serial communications Serial.begin(9600); //setup the next chirp time nextChirp = millis() + (chirpInterval * 1000); } void chirp() { //reset the chirp timer and sound a chirp nextChirp = millis() + (chirpInterval * 1000); Serial.println("CHIRP SENT"); tone(chirpPin, chirpTone, chirpTime); } void movementDetected() { //reset the timer and add a bit of an extra delay nextChirp = millis() + (motionDelay * 1000); Serial.println("Movement Detected. Resetting chirp"); } void loop() { //if movement is detected, the input pin will become HIGH and call the movementDetected() function if (digitalRead(inputPin) == HIGH) { movementDetected(); //a movement was detected so do this } Serial.print("Time To Chirp: "); Serial.print((nextChirp / 1000) - (millis() / 1000)); Serial.println(" Seconds"); if (millis() > nextChirp) { //its time to chirp! chirp();; } }