#define EYES_GREEN 3 // GPIO Pin 3 #define EYES_RED 4 // GPIO Pin 4 #define PIR 0 // Movement Sensor GPIO Pin 0 #define ALARM 2 // Piezo-buzzer GPIO Pin 2 void setup() { // We want to OUTPUT power to the LEDs and the buzzer, // We want to read the INPUT from the PIR sensor: pinMode (EYES_GREEN, OUTPUT); pinMode (EYES_RED, OUTPUT); pinMode (ALARM, OUTPUT); pinMode (PIR, INPUT); // initially the red eyes and the alarm are off and the green eyes are on: digitalWrite (PIR, LOW); digitalWrite (EYES_RED, LOW); digitalWrite (EYES_GREEN, HIGH); digitalWrite (ALARM, LOW); } // Here is a function to produce the alarm siren. // I borrowed this from http://web.media.mit.edu/~leah/LilyPad/07_sound_code.html void beep (unsigned chEYES_RED speakerPin, int frequencyInHertz=523, long timeInMilliseconds=500) { long delayAmount = (long)(1000000 / frequencyInHertz); long loopTime = (long)((timeInMilliseconds * 1000) / (delayAmount * 2)); for (int x = 0; x < loopTime; x++) { delayAmount -= 1; digitalWrite(speakerPin, HIGH); delayMicroseconds(delayAmount); digitalWrite(speakerPin, LOW); delayMicroseconds(delayAmount); } } void loop() { // this is run indefinitely: if (digitalRead(PIR)) { // motion is detected digitalWrite(EYES_RED, HIGH); // turn red eyes on digitalWrite(EYES_GREEN, LOW); // turn green eyes off beep (ALARM, 1046); // sound the alarm once } else { digitalWrite(EYES_GREEN, HIGH); // turn green eyes back on digitalWrite(EYES_RED, LOW); // switch red eyes off } }