// libraries
//#include <SPI.h>
//#include <SdFat.h>
//#include <vs1053_SdFat.h>
//#include <Bounce2.h>
// route all delay() calls theough myDelay so we can not die of old age testing
void myDleay(unsigned long ms)
{
// for now let's step this along 1000 times faster
// delayMicroseconds(ms);
// ok, now just 4 times faster - life very short!
// delay(ms / 4);
// and real time
delay(ms);
}
int Plug1 = 5; // the pin that the Plug1 is atteched to
int Plug2 = 6; // the pin that the Plug2 is atteched to
int PIR1 = 3; // the pin that the PIR1 is atteched to
int state = LOW; // by default, no motion detected
int val = A0; // variable to store the PIR1 status (value)
int var = A1; // value of random?
//SdFat sd;
// vs1053 MP3player;
void setup() {
pinMode(Plug1, OUTPUT); // initalize Plug1 as an output
pinMode(Plug2, OUTPUT); // initalize Plug2 as an output
pinMode(PIR1, INPUT_PULLUP); // initialize PIR1 as an input - used here inverted to original
/* the symobls are function names. they do not have/need pinModes
pinMode (CASKET_JUMP, OUTPUT);
pinMode (BOX, OUTPUT);
pinMode (BOTH, OUTPUT);
*/
Serial.begin(9600); // initialize serial
Serial.println("hello prop world!\n");
/* stuff we can lave off unitl the logic functions
if(!sd.begin(9, SPI_HALF_SPEED)) sd.initErrorHalt();
if (!sd.chdir("/")) sd.errorHalt("sd.chdir");
MP3player.begin();
MP3player.setVolume(10,10);
*/
}
//casket jump code
void CASKET_JUMP(){
Serial.println("Trigger Casket");
// MP3player.stopTrack(); //
// MP3player.playTrack(003); //play scream sound
digitalWrite(Plug1, HIGH); //turns on plug
Serial.print("scream and 10 second delay\n");
myDleay(10000);
digitalWrite(Plug1, LOW); //turns on plug - on or off? one HIGH or LOW mustbe on, the other off
myDleay(4000); //delay to prevent triggering back to back ++++++++++++ BLOCK THIS LINE TO TEST
Serial.print("now I'm ready to scare!\n");
}
//box Casket code
void BOX(){
Serial.print("Box Casket Banger activate LEDs to backlight owl in hole\n");
// MP3player.stopTrack();
// MP3player.playTrack(005); //play screeming yells let me out
digitalWrite(Plug2, LOW);
digitalWrite(Plug2, HIGH); //activate LEDs to backlight owl in hole
myDleay(100);
digitalWrite(Plug2, LOW);
myDleay(600);
digitalWrite(Plug2, HIGH);
myDleay(100);
digitalWrite(Plug2, LOW);
myDleay (100);
digitalWrite(Plug2, HIGH);
myDleay (2000);
digitalWrite(Plug2, LOW);
myDleay(300);
digitalWrite(Plug2, HIGH);
myDleay(1000);
digitalWrite(Plug2, LOW);
myDleay(200);
digitalWrite(Plug2, HIGH);
myDleay(1000);
digitalWrite(Plug2, LOW);
myDleay (300);
digitalWrite(Plug2, HIGH);
myDleay (2000);
digitalWrite(Plug2, LOW);
myDleay (4000);
Serial.print("Box Casket Banger End\n");
}
//Code for Both
void BOTH(){
digitalWrite(Plug2, HIGH);
myDleay(500);
digitalWrite(Plug2, LOW);
myDleay (88);
digitalWrite(Plug2, HIGH);
myDleay (500);
digitalWrite(Plug2, LOW);
myDleay(33);
digitalWrite(Plug2, HIGH);
myDleay(500);
digitalWrite(Plug2, LOW);
myDleay(18);
digitalWrite(Plug2, HIGH);
myDleay(500);
digitalWrite(Plug2, LOW);
myDleay (18);
digitalWrite(Plug2, HIGH);
myDleay (500);
digitalWrite(Plug2, LOW);
myDleay (38);
// MP3player.stopTrack();
// MP3player.playTrack(006); //play CRAZY LAUGH
Serial.print("play CRAZY LAUGH\n");
digitalWrite(Plug1, HIGH); //PLUG ONE ON
myDleay (500);
digitalWrite(Plug1, LOW); //PLUG ONE OFF
myDleay (93);
digitalWrite(Plug1, HIGH); //PLUG ONE ON
myDleay (500);
digitalWrite(Plug1, LOW); //PLUG ONE OFF
myDleay (1000);
}
void loop() {
val = !digitalRead(PIR1); // read PIR1 value INVERTED so pushing (LOW) is motion
var = random(1, 4); // random number 1, 2 or 3
if (val == HIGH) { // check if the PIR1 is HIGH
// moved this so it reports at a logical point in the sequence
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
Serial.print(" motion sensor HIGH randomly selected ");
Serial.println(var);
//CASKET JUMP CODE TAKEN OUT HERE AND RENAMED
switch (var){
case 1:
CASKET_JUMP ();
break;
case 2:
BOX();
break;
case 3:
BOTH();
break;
default:
Serial.println("cannot/should not happen");
return; // ??
}
}
else {
digitalWrite(Plug1, LOW); // turn Plug1 OFF
if (!(millis()%1000)) // prints a dot per second
Serial.print(".");
if (!(millis()%60000)) // prints a new line once per minute
Serial.println();
myDleay(1);
// this looks wrong - constantly stop and then play track 1?
// MP3player.stopTrack(); //
// MP3player.playTrack(001); //play background track 4
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}