Switch case help to trigger props

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

int val = A0;  

and this

val = digitalRead(PIR1); 

explain, please.

this

int var = A1;                 // value of random?

and this

switch (var)

does not make much sense. Could you explain what you are trying to do here?

And if you program is NOT responsive it's because of the many many delays(xxxxx).

How is var incremented?

What is SUPPOSED to happen is when motion is detected a random case 1,2,or 3 is selected and triggered.

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.

There is no code to generate a random number, as a note.

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:

motion detect prop dispatcher

and once you get the logic working, start hanging the fruit back onto the branches in your real life project... step by step testing as you go.

Because I have no life was curious about what was hiding in there.

a7

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
    }
  }
}

Yes on the right track.

Your call to random() will return 0, 1, 2 and 3. But you prolly wouldn't notice the 0s, as it would be on to the next thing fairly soon.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.