/* Controller for Christine prop PIR sensor will fire relays for lights and mp3 audio of the engine revving In this program I control the four headlights in two groups (as if low beams and brights) The PIR calibration and detection loop in this code is based heavily on: CalHaunts PIR, Relays, and Fog Machine controller, for Make & Take workshop, July 2013 which in turn was based on AdaFruit example code The circuit: * pin 26 attached to PIR sensor's output * pin 41 feeds an LED just to visualize the PIR sensor state * pin 45 attached to relay IN1 * pin 47 attached to relay IN2 * pin 49 attached to relay IN3 */ // Note: requires additional libraries for the music shield from Seeed Studio #include #include #include #include #include "pins_config.h" #include "vs10xx.h" #include "newSDLib.h" #include "MusicPlayer.h" MusicPlayer myplayer; // the time we give the sensor to calibrate (10-60 seconds typically) int calibrationTime = 30; // the time when the sensor outputs a low impulse long unsigned int lowIn; // the amount of milliseconds the sensor has to be low // before we assume all motion has stopped long unsigned int pause = 2000; boolean lockLow = true; boolean takeLowTime; int pirPin = 41; int ledPin = 26; int relayPin1 = 45; int relayPin2 = 47; int relayPin3 = 49; void setup(){ Serial.begin(9600); // initialize music sheild player myplayer.keyDisable(); myplayer.begin(); // will initialize the hardware and set default mode to be normal. pinMode(pirPin, INPUT); pinMode(ledPin, OUTPUT); // initialize the RELAY pins as an output: pinMode(relayPin1, OUTPUT); pinMode(relayPin2, OUTPUT); pinMode(relayPin3, OUTPUT); digitalWrite(pirPin, LOW); // fog off digitalWrite(relayPin3, LOW); // briefly test all lights digitalWrite(relayPin1, HIGH); digitalWrite(relayPin2, HIGH); delay(1500); digitalWrite(relayPin1, LOW); digitalWrite(relayPin2, LOW); //give the sensor some time to calibrate Serial.print("calibrating sensor "); for(int i = 0; i < calibrationTime; i++){ Serial.print("."); delay(1000); } Serial.println(" done"); Serial.println("SENSOR ACTIVE"); delay(50); // we keep the low-beams on all the time digitalWrite(relayPin1, HIGH); } void loop(){ if(digitalRead(pirPin) == HIGH){ digitalWrite(ledPin, HIGH); // LED shows what the PIR is doing // turn on all lights digitalWrite(relayPin1, HIGH); digitalWrite(relayPin2, HIGH); if(lockLow){ // makes sure we wait for a transition to LOW before any further output is made: lockLow = false; Serial.println("---"); Serial.print("motion detected at "); Serial.print(millis()/1000); Serial.println(" sec"); // turn on fog and sound digitalWrite(relayPin3, HIGH); myplayer.playSong("rev.mp3"); } takeLowTime = true; } if(digitalRead(pirPin) == LOW){ digitalWrite(ledPin, LOW); // turn fog off digitalWrite(relayPin3, LOW); if(takeLowTime){ lowIn = millis(); //save the time of the transition from high to LOW takeLowTime = false; //make sure this is only done at the start of a LOW phase } // if the sensor is low for more than the given pause, // we assume that no more motion is going to happen if(!lockLow && millis() - lowIn > pause){ // makes sure this block of code is only executed again after // a new motion sequence has been detected lockLow = true; Serial.print("motion ended at "); Serial.print((millis() - pause)/1000); Serial.println(" sec"); // turn off lights2 (brights) digitalWrite(relayPin2, LOW); delay(20000); } } }