I'm looking for help to make my prop work. I have a PIR motion sensor to trigger two of my props. It uploads fine but the motion does not trigger a reaction.
// libraries
#include <SPI.h>
#include <SdFat.h>
#include <vs1053_SdFat.h>
#include <Bounce2.h>
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); // initialize PIR1 as an input
pinMode (CASKET_JUMP, OUTPUT);
pinMode (BOX, OUTPUT);
pinMode (BOTH, OUTPUT);
Serial.begin(9600); // initialize serial
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(F("Trigger Casket"));
MP3player.stopTrack(); //
MP3player.playTrack(003); //play scream sound
digitalWrite(Plug1, HIGH); //turns on plug
Serial.print("10 second delay\n");
delay(10000);
digitalWrite(Plug1, LOW); //turns on plug
//delay(4000); //delay to prevent triggering back to back ++++++++++++ BLOCK THIS LINE TO TEST
Serial.print("I'm ready to scare!\n");
}
//box Casket code
void BOX(){
Serial.print("Box Casket Banger\n");
MP3player.stopTrack();
MP3player.playTrack(005); //play screeming yells let me out
digitalWrite(Plug2, LOW);
digitalWrite(Plug2, HIGH); //activate LED's to backlight owl in hole
delay(100);
digitalWrite(Plug2, LOW);
delay(600);
digitalWrite(Plug2, HIGH);
delay(100);
digitalWrite(Plug2, LOW);
delay (100);
digitalWrite(Plug2, HIGH);
delay (2000);
digitalWrite(Plug2, LOW);
delay(300);
digitalWrite(Plug2, HIGH);
delay(1000);
digitalWrite(Plug2, LOW);
delay(200);
digitalWrite(Plug2, HIGH);
delay(1000);
digitalWrite(Plug2, LOW);
delay (300);
digitalWrite(Plug2, HIGH);
delay (2000);
digitalWrite(Plug2, LOW);
delay (4000);
Serial.print("Box Casket Banger End\n");
}
//Code for Both
void BOTH(){
digitalWrite(Plug2, HIGH);
delay(500);
digitalWrite(Plug2, LOW);
delay (88);
digitalWrite(Plug2, HIGH);
delay (500);
digitalWrite(Plug2, LOW);
delay(33);
digitalWrite(Plug2, HIGH);
delay(500);
digitalWrite(Plug2, LOW);
delay(18);
digitalWrite(Plug2, HIGH);
delay(500);
digitalWrite(Plug2, LOW);
delay (18);
digitalWrite(Plug2, HIGH);
delay (500);
digitalWrite(Plug2, LOW);
delay (38);
MP3player.stopTrack();
MP3player.playTrack(006); //play CRAZY LAUGH
digitalWrite(Plug1, HIGH); //PLUG ONE ON
delay (500);
digitalWrite(Plug1, LOW); //PLUG ONE OFF
delay (93);
digitalWrite(Plug1, HIGH); //PLUG ONE ON
delay (500);
digitalWrite(Plug1, LOW); //PLUG ONE OFF
delay (1000);
}
void loop(){
val = digitalRead(PIR1); // read PIR1 value
if (val == HIGH) { // check if the PIR1 is HIGH
//CASKET JUMP CODE TAKEN OUT HERE AND RENAMED
switch (var){
case 1:
CASKET_JUMP ();
break;
case 2:
BOX();
break;
case 3:
BOTH();
break;
default:
return;
}
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
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();
delay(1);
// MP3player.stopTrack(); //
MP3player.playTrack(001); //play background track 4
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}
??? What is this supposed to do? The value of A1 is 15 if this is an UNO. It will never equal any of the cases in your switch statement which means it will default and return from the loop() function.
This makes no sense at all. CASKET_JUMP(), BOX() and BOTH() are functions, not pins.
I suggest you delete this code and start over. Start by taking some Arduino tutorials to help you learn coding.
Coding is not like chanting magic spells in Hogwarts. It's a language. You can't just copy & paste and hope for the best. That would be like shouting random French words to a French person. You have to understand every part of every line of code you write, or it will just be nonsense.
Sorry if this sounds harsh, I give you this advice with the best intentions.
OK, the logic is kinda sorta there amongst all the mistakes as have been pointed out.
I side-stepped the things I don't have or was too lazy to add to the sketch by commenting them out.
I commented out plain mistaken code.
I added a random number for the switch/case.
I used a trick to avoid waiting for delay() to finish and maybe, just maybe, this will work out somehow.
Imma guess this is for Hallowe'en, so I won't bother telling you to re-write the whole thing so it functions better.
It's mostly the original code, but I did move a few things so the reporting is more logicial. I did this on the wokwi, where the sun is always shining, wire errors can be fixed with a click, and so forth. Play with it here:
Okay, I have tried to rework this some and believe I'm getting it.
// libraries
#include <SPI.h>
#include <SdFat.h>
#include <vs1053_SdFat.h>
#include <Bounce2.h>
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
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); // initialize PIR1 as an input
Serial.begin(9600); // initialize serial
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(F("Trigger Casket"));
MP3player.stopTrack(); //
MP3player.playTrack(003); //play scream sound
digitalWrite(Plug1, HIGH); //turns on plug
Serial.println("10 second delay\n");
delay(10000);
digitalWrite(Plug1, LOW); //turns on plug
Serial.println("Ten Second Delay\n");
delay(10000); //delay to prevent triggering back to back ++++++++++++ BLOCK THIS LINE TO TEST
Serial.println("I'm ready to scare!\n");
}
//box Casket code
void BOX(){
Serial.println("Box Casket Banger\n");
MP3player.stopTrack();
MP3player.playTrack(005); //play screeming yells let me out
digitalWrite(Plug2, HIGH); //activate LED's to backlight owl in hole
delay(100);
digitalWrite(Plug2, LOW);
delay(600);
digitalWrite(Plug2, HIGH);
delay(100);
digitalWrite(Plug2, LOW);
delay (100);
digitalWrite(Plug2, HIGH);
delay (500);
digitalWrite(Plug2, LOW);
delay(300);
digitalWrite(Plug2, HIGH);
delay(100);
digitalWrite(Plug2, LOW);
delay(200);
digitalWrite(Plug2, HIGH);
delay(100);
digitalWrite(Plug2, LOW);
delay (300);
digitalWrite(Plug2, HIGH);
delay (700);
digitalWrite(Plug2, LOW);
Serial.println("Ten Second Delay\n");
delay (10000);
Serial.println("Box Casket Banger End\n");
}
//Code for Both
void BOTH(){
Serial.println("Both going, cute, huh?\n");
digitalWrite(Plug2, HIGH);
delay(50);
digitalWrite(Plug2, LOW);
delay (88);
digitalWrite(Plug2, HIGH);
delay (50);
digitalWrite(Plug2, LOW);
delay(33);
digitalWrite(Plug2, HIGH);
delay(50);
digitalWrite(Plug2, LOW);
delay(18);
digitalWrite(Plug2, HIGH);
delay(50);
digitalWrite(Plug2, LOW);
delay (18);
digitalWrite(Plug2, HIGH);
delay (50);
digitalWrite(Plug2, LOW);
delay (38);
MP3player.stopTrack();
MP3player.playTrack(006); //play CRAZY LAUGH
digitalWrite(Plug1, HIGH); //PLUG ONE ON
delay (50);
digitalWrite(Plug1, LOW); //PLUG ONE OFF
delay (93);
digitalWrite(Plug1, HIGH); //PLUG ONE ON
delay (50);
digitalWrite(Plug1, LOW); //PLUG ONE OFF
delay (100);
Serial.println("End\n");
Serial.println("Ten Second Delay\n");
delay (10000);
}
void loop(){
if (digitalRead(PIR1) == HIGH) { // check if the PIR1 is HIGH
switch (random(0,4)){
case 1:
CASKET_JUMP ();
break;
case 2:
BOX();
break;
case 3:
BOTH();
break;
default:
return;
}
if (state == LOW) {
Serial.println("Ready to Scare!");
state = HIGH; // update variable state to HIGH
}
}
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();
delay(1);
MP3player.stopTrack(); //
MP3player.playTrack(001); //play background track 1
if (state == HIGH){
Serial.println("lets go!");
state = LOW; // update variable state to LOW
}
}
}