Hi. I'm going to post unabriged code and wiring diagrams at the bottom of the post, I've had to take apart my UNO and start putting it back together one pin at a time to claw back functionality, so I might post a scaled back diagram and abriged code to explain where I think the problem is, but the full story will be at the bottom of this post.
I have a setup to control an escape room, there's probably a million better ways to do this, but I'm amateur so I'd appreciate the help. My setup is made of three parts;
- An arduino MEGA, controlling most aspects of the room. Lights, doors, relays etc.
- An UNO, specifically for scares, lights and horror sounds
- A couple of esps, controlling keypads, LCDs and an ultrasound distance sensor.
The uno and mega are connected to a "scare switch" about 10m down the corridor that basically blocks anything spooky in case my customers don't want to be scared. Currently both boards are reacting properly to this switch (for once, haha).
The esps are communicating properly with the mega when I send the following low 600ms 'pulse'
digitalWrite (successPin, LOW); // Tell mega to initiate firstCams
digitalWrite (buzzPin, HIGH);
digitalWrite (lockPin, LOW);
delay (600);
digitalWrite (successPin, HIGH);
digitalWrite (buzzPin, LOW);
digitalWrite (lockPin, HIGH);
But then I send a similar low signal from the mega to the uno;
if(circsqtriSwitch.fell())
{
Serial.println("1612 complete");
digitalWrite(circsqtriSig, LOW); // Tell the scare uno that 1612 has been input
firstcams();
}
This signal doesn't get picked up by the uno (here's the code that's looking out for it);
circsqtriSwitch.update();
if(circsqtriSwitch.fell())
{
Serial.println("1612 complete");
violentAttack();
}
Here's an abriged diagram of how it looks
I'm thinking it has something to do with ground reference, like, the mega pin (pin A15 is already at ground, therefore digitalWrite(LOW) doesn't cause the pin to "fall" and trigger the violentAttack()
As per the diagram I've connected the grounds together (they're actually connected via the scare switch ground pin but I guess it's all the same).
It works when I disconnect the cable from the UNO pin A0, then short the pin to ground. Doing this triggers violentAttack() (the same is true for pin A1 and A3) a la this diagram;

Interestingly, this doesn't work when the pin is connected to the mega (I.e. UNO A0 connected to MEGA A15) which is why I think the pin isn't "falling" when the puzzle is completed and the pulse is sent, it only works if the pin is disconnected.
I initially solved it by writing the 3 mega pins high in setup, then they worked. The problem is it caused some funny business with the UNO, in particular when I disconnected Vin and gnd from the UNO, it was still powered through these pins that were getting this high signal. I'm not an expert but I'd imagine powering the UNO through the logic pins was a bad idea, so I turned this part of the code off.
pinMode (circsqtriSig, OUTPUT);
// digitalWrite (circsqtriSig, HIGH);
pinMode (reedSig, OUTPUT);
// digitalWrite (reedSig, HIGH);
pinMode (doorSig, OUTPUT);
// digitalWrite (doorSig, HIGH);
So this is why I've come to the forum. I'd imagine there's a million ways to do this better, I'm currently looking into learning espNOW to controll all the microcontrollers wirelessly but for now I kinda need to get the experience reopened so if anyone had an easy fix for now, then suggested I look into a better implementation (i've heard of something called UART?) then I'm all ears.
Is there a way to write the three pins (A0, 1 and 3) as "floating", neither HIGH or LOW?
Should I use the non- analog pins? (actually, my spidey sense is telling me this might be the problem, I'll try changing the pins, and update today)
Thank you in advance!
UNO code
// Scare control UNO
#include <AltSoftSerial.h> // Alt serial library, see library folder in dropbox
#include <Wire.h>
#include <MD_YX5300.h> // See library folder in dropbox
#include <Bounce2.h> // For debouncing button input. See https://github.com/thomasfredericks/Bounce2
// Tunable variables - These are so you can sync up sounds and scares easily
int FHdim = 1500; // Time for the spotlight to dim after the MP3 has been triggered
int FHrelay = 1500; // Time for relay to hit
int BYdelay1 = 5000; // Time for first behind you after reed trigger
int BYdelay2 = 5000; // Time for second behind you after first behind you
int BYflicker1 = 5000; // Time for first flicker pulse
int BYflicker2 = 5000; // Time for second flicker pulse
int fadeAmount = 5; // how many points to fade the spotlight by
int fadeinTime = 50; // how quickly to fade the spotlight up (this is slower)
int fadeoutTime = 20; // how quickly to fade the spotlight up (this is quick)
// Input pins
int hiss = 12;
int hissSelect; // Random number to select which hiss mp3 to select
int flicker = 6;
int exhale = 10;
int baby = 11;
// Output pins
int megaFlicker = A5;
int megavolumeDown = 2;
// Input triggers
int circsqtriPin = A0; // Trigger for 1612 and starts violet attack countdown
int rfidPin = A1; // Trigger the facehugger sequence
int jump7Pin= A2; // Trigger the window opening sounds
int reedPin = A3; // Trigger any behind you sequence
int plugPin = A4; // Trigger the scare
int ultrasoundPin = 7;
// Pins for scare switch
int scareSwitch = 13; // This is pulled low when scares are being turned on.
int switchState; //
// Utility
int spotlight = 5; // PWM pin to control the brightness of the light (via MOSFET)
int brightness = 0; // how bright the spotlight is
int lightRelay = 3; // Relay attached to light NO is floor NC is hand
int facehuggerRelay = 4; // Relay to release the facehugger scare
bool plugState = false; // Keeps the "waiting for plug" loop going
// GLOBALS
// Create an array of Bounce objects for each input button
Bounce circsqtriSwitch = Bounce();
Bounce reedSwitch = Bounce();
Bounce rfidSwitch = Bounce();
Bounce jump7Switch = Bounce();
Bounce plugSwitch = Bounce();
Bounce ultrasoundSwitch = Bounce();
Bounce hissSwitch = Bounce();
Bounce exhaleSwitch = Bounce();
Bounce babySwitch = Bounce();
Bounce flickerSwitch = Bounce();
// Initialise a software serial interface on the approriate Rx/Tx pins (8/9)
AltSoftSerial altSerial;
// And create an MP3 object based on the serial connection
MD_YX5300 mp3(altSerial);
void setup()
{
delay (10000);
// Initialise a serial connection (used for debugging only)
Serial.begin(115200);
Serial.println(__FILE__ __DATE__);
Serial.println("Serial connection initialised");
Serial.println ("Setup 2 begun, buffer initiated");
delay (10000);
Serial.println ("Buffer 2 complete");
reedSwitch.attach(reedPin, INPUT_PULLUP);
rfidSwitch.attach(rfidPin, INPUT_PULLUP);
circsqtriSwitch.attach(circsqtriPin, INPUT_PULLUP);
jump7Switch.attach(jump7Pin, INPUT_PULLUP);
plugSwitch.attach(plugPin, INPUT_PULLUP);
ultrasoundSwitch.attach(ultrasoundPin, INPUT_PULLUP);
hissSwitch.attach(hiss, INPUT_PULLUP);
flickerSwitch.attach(flicker, INPUT_PULLUP);
exhaleSwitch.attach(exhale, INPUT_PULLUP);
babySwitch.attach(baby, INPUT_PULLUP);
Serial.println("Utility buttons initialised");
// Scare mode?
pinMode (scareSwitch, INPUT_PULLUP);
switchState = digitalRead(scareSwitch);
Serial.println(switchState);
// Initialise pins and test
pinMode(megaFlicker, OUTPUT);
pinMode(megavolumeDown, OUTPUT);
pinMode(spotlight, OUTPUT);
pinMode(facehuggerRelay, OUTPUT);
digitalWrite(facehuggerRelay, LOW);
pinMode(lightRelay, OUTPUT);
digitalWrite (lightRelay, LOW);
// Test
// Initialise the serial interface to the MP3 player
altSerial.begin(9600);
mp3.begin();
mp3.volume(30);
delay(1000);
if (switchState == 0){
Serial.print(switchState);
mp3.playTrack(1); // "Horror mode is turned on"
Serial.println ("Horror mode is turned ON");
}
else {
Serial.print(switchState);
mp3.playTrack(2); // "Horror mode is turned off"
Serial.println ("Horror mode is turned OFF");
}
delay(1000);
Serial.println("Setup complete");
}
void loop()
{
switchState = digitalRead(scareSwitch);
// Serial.print(switchState);
if (switchState == 0){
// Serial.println("SPOOKY");
}
else {
Serial.println("Safe");
}
reedSwitch.update();
rfidSwitch.update();
circsqtriSwitch.update();
jump7Switch.update();
hissSwitch.update();
flickerSwitch.update();
exhaleSwitch.update();
babySwitch.update();
if(circsqtriSwitch.fell())
{
Serial.println("1612 complete");
violentAttack();
}
if(rfidSwitch.fell())
{
Serial.println("rfid complete");
//Todo light relay change
if (switchState == 0)
{
Serial.println("SCARES ARE ON");
jumpScare();
}
else
{
Serial.println("SCARES ARE OFF");
noScare();
}
}
if(jump7Switch.fell())
{
Serial.println("jump7 complete");
cryBabies();
}
if(reedSwitch.fell())
{
Serial.println("reed complete");
behindYou();
}
if(flickerSwitch.fell())
{
Serial.println("flicker");
{
sendFlicker();
}
}
if(babySwitch.fell())
{
Serial.println("baby");
if (switchState == 0)
{
mp3.playTrack(10);
}
}
if(hissSwitch.fell())
{
Serial.println("hiss");
if (switchState == 1)
{
whataHisser();
}
}
if(exhaleSwitch.fell())
{
Serial.println("exhale");
if (switchState == 1)
{
mp3.playTrack(11);
}
}
}
void violentAttack()
{
Serial.println("in function violentAttack");
if (switchState == 0)
{
Serial.println("playing violent attack");
delay(45000);
mp3.playTrack(3);
}
return;
}
void jumpScare() // TO DO
{
Serial.println("in jumpScare");
digitalWrite(lightRelay, HIGH);
mp3.playTrack(7);
int ultrasoundPinging = true;
do
{
plugSwitch.update();
ultrasoundSwitch.update();
if(ultrasoundSwitch.fell())
{
mp3.playTrack(5); // Facehugger cry
ultrasoundPinging = false; // Cease this IF statement in future
spotlightOn(); // Turn on spotlight
digitalWrite(megavolumeDown, LOW); // Send volume down signal
delay(200);
digitalWrite(megavolumeDown, HIGH);
}
if(plugSwitch.fell())
{
Serial.println("plug connected");
mp3.playTrack(9);
delay(FHdim);
spotlightOff();
delay(FHrelay);
digitalWrite (facehuggerRelay, HIGH);
delay(200);
digitalWrite(facehuggerRelay, LOW);
plugState = true;
delay(2000);
digitalWrite(lightRelay, LOW);
digitalWrite(megavolumeDown, LOW); // Send volume up signal
delay(200);
digitalWrite(megavolumeDown, HIGH);
}
}
while (plugState == false);
}
void noScare()
{
Serial.println ("in noScare");
mp3.playTrack(8);
return;
}
void cryBabies ()
{
if (switchState == 0)
{
delay(10000);
mp3.playTrack(5);
delay(60000);
mp3.playTrack(4);
}
else
{
return;
}
}
void behindYou()
{
switchState = digitalRead(scareSwitch);
Serial.println(switchState);
if(switchState == 0)
{
delay (BYdelay1);
mp3.playTrack(12);
delay (BYdelay2);
sendFlicker();
delay (BYflicker1);
mp3.playTrack(13);
delay (BYflicker2);
sendFlicker();
}
else
{
return;
}
}
void spotlightOn()
{
brightness = 0;
do
{
analogWrite(spotlight, brightness); // Set current brightness
// Serial.println (brightness);
brightness = brightness + fadeAmount; // Increase brightness
delay(fadeinTime);
}
while (brightness <= 225); // Till brightness maxed
analogWrite(spotlight,225);
return;
}
void spotlightOff()
{
do
{
analogWrite(spotlight, brightness); // Set current brightness
// Serial.println (brightness);
brightness = brightness - fadeAmount; // Decrease brightness
delay(fadeoutTime);
}
while (brightness >= 0); // Till brightness at minimum
analogWrite(spotlight, 0);
return;
}
void whataHisser()
{
hissSelect = random(3);
Serial.println (hissSelect);
if (hissSelect == 0)
{
mp3.playTrack(14);
}
else if (hissSelect == 1)
{
mp3.playTrack(15);
}
else if (hissSelect == 2)
{
mp3.playTrack(16);
}
}
void sendFlicker()
{
if (switchState == 0)
{
digitalWrite(megaFlicker, LOW);
delay(200);
digitalWrite(megaFlicker, HIGH);
mp3.playTrack(18);
}
}
UNO diagram
MEGA code
// DSB mega brain
#include <AltSoftSerial.h> // Alt serial library, see library folder in dropbox
#include <Wire.h>
#include <MD_YX5300.h> // See library folder in dropbox
#include <Bounce2.h> // For debouncing button input. See https://github.com/thomasfredericks/Bounce2
// Cam relay pins
int cam[8] = {2,3,4,5,6,7,8,9};
// Tunable variables - These are so you can sync up sounds and scares easily
int NSmp3 = 1500; // Delay for door voice over for scare free
int JSmp3 = 1500; // Delay for door voice over for jump scare
int vol = 30; // Variable to control the volume of ambient music
int volInterval = 1; // Variable to control the speed of volUp and volDown
bool lightsOn; // Bool to keep track of the lights status
bool jumpScare; // Bool to keep us in the jumpScare do while loop
int FHstrike = 5000; // Time to wait to turn on lights after facehugger has struck
int intMax = 300; // Random max interval (Decrease if flicker is taking too long)
int intMin = 50; // Random min interval (Increase if flicker is too snappy)
int BYflicker2 = 5000; // Time for second flicker pulse
int fadeAmount = 5; // how many points to fade the spotlight by
int fadeinTime = 50; // how quickly to fade the spotlight up (this is slower)
int fadeoutTime = 20; // how quickly to fade the spotlight up (this is quick)
int distanceThreshold = 700; // how close player has to be to distance sensor
// Pins leds
int r1 = 30;
int g1 = 31;
int r2 = 32;
int g2 = 33;
int r3 = 34;
int g3 = 35;
int r4 = 36;
int g4 = 37;
int r5 = 38;
int g5 = 39;
int r6 = 40;
int g6 = 41;
int r7 = 42;
int g7 = 43;
int r8 = 44;
int g8 = 45;
// Reed pins
int wReed = A1;
int reedPin = A0;
int rReed = A3;
int gReed = A2;
// Scare stuff
int circsqtriSig = A15; // UNO pin A0
int doorSig = A14; // UNO pin A1
int reedSig = A13; // UNO pin A3
// Success triggers
int circsqtriPin = A8;
int rfidPin = A7;
int flickerPin= A12;
int volUpPin = A10; // Connected to UNO 0
int volDownPin = A11; // Connected to UNO 1
// Pins for scare switch
int scareSwitch = A9;
int switchState;
// Utility pins
int buzzPin = 53;
int relay1 = 21;
int relay2 = 20; // Door NO is bolt engaged
int relay3 = 19; // Rotato potato NC is lights, NO is maglock
int strip1 = 18;
int strip2 = 17;
int strip3 = 16;
int strip4 = 15;
int strip5 = 14;
// GLOBALS
// Create an array of Bounce objects for each input button
Bounce circsqtriSwitch = Bounce();
Bounce reedSwitch = Bounce();
Bounce rfidSwitch = Bounce();
Bounce flickerSwitch = Bounce();
Bounce volDownSwitch = Bounce();
Bounce volUpSwitch = Bounce();
// Initialise a software serial interface on the approriate Rx/Tx pins (8/9)
AltSoftSerial altSerial;
// And create an MP3 object based on the serial connection
MD_YX5300 mp3(altSerial);
void setup()
{
// Initialise a serial connection (used for debugging only)
Serial.begin(115200);
Serial.println(__FILE__ __DATE__);
Serial.println("Serial connection initialised");
reedSwitch.attach(reedPin, INPUT_PULLUP);
rfidSwitch.attach(rfidPin, INPUT_PULLUP);
circsqtriSwitch.attach(circsqtriPin, INPUT_PULLUP);
flickerSwitch.attach(flickerPin, INPUT_PULLUP);
volUpSwitch.attach(volUpPin, INPUT_PULLUP);
volDownSwitch.attach(volDownPin, INPUT_PULLUP);
Serial.println("Utility buttons initialised");
// Initialise led pins and test
pinMode (buzzPin, OUTPUT);
pinMode (scareSwitch, INPUT_PULLUP);
pinMode (r1, OUTPUT);
pinMode (r2, OUTPUT);
pinMode (r3, OUTPUT);
pinMode (r4, OUTPUT);
pinMode (r5, OUTPUT);
pinMode (r6, OUTPUT);
pinMode (r7, OUTPUT);
pinMode (r8, OUTPUT);
pinMode (g1, OUTPUT);
pinMode (g2, OUTPUT);
pinMode (g3, OUTPUT);
pinMode (g4, OUTPUT);
pinMode (g5, OUTPUT);
pinMode (g6, OUTPUT);
pinMode (g7, OUTPUT);
pinMode (g8, OUTPUT);
pinMode (rReed, OUTPUT);
pinMode (gReed, OUTPUT);
pinMode (wReed, OUTPUT);
pinMode (circsqtriSig, OUTPUT);
// digitalWrite (circsqtriSig, HIGH);
pinMode (reedSig, OUTPUT);
// digitalWrite (reedSig, HIGH);
pinMode (doorSig, OUTPUT);
// digitalWrite (doorSig, HIGH);
// Test
digitalWrite (buzzPin, HIGH);
delay(200);
digitalWrite (buzzPin, LOW);
digitalWrite (g1, HIGH);
delay(100);
digitalWrite (g2, HIGH);
delay(100);
digitalWrite (g3, HIGH);
delay(100);
digitalWrite (g4, HIGH);
delay(100);
digitalWrite (g5, HIGH);
delay(100);
digitalWrite (g6, HIGH);
delay(100);
digitalWrite (g7, HIGH);
delay(100);
digitalWrite (g8, HIGH);
delay(100);
digitalWrite (buzzPin, HIGH);
delay(200);
digitalWrite (buzzPin, LOW);
digitalWrite (g1, LOW);
delay(100);
digitalWrite (g2, LOW);
delay(100);
digitalWrite (g3, LOW);
delay(100);
digitalWrite (g4, LOW);
delay(100);
digitalWrite (g5, LOW);
delay(100);
digitalWrite (g6, LOW);
delay(100);
digitalWrite (g7, LOW);
delay(100);
digitalWrite (g8, LOW);
delay(100);
digitalWrite (buzzPin, HIGH);
delay(200);
digitalWrite (buzzPin, LOW);
digitalWrite (r1, HIGH);
delay(100);
digitalWrite (r2, HIGH);
delay(100);
digitalWrite (r3, HIGH);
delay(100);
digitalWrite (r4, HIGH);
delay(100);
digitalWrite (r5, HIGH);
delay(100);
digitalWrite (r6, HIGH);
delay(100);
digitalWrite (r7, HIGH);
delay(100);
digitalWrite (r8, HIGH);
delay(100);
digitalWrite (buzzPin, HIGH);
delay(200);
digitalWrite (buzzPin, LOW);
// Initialise cam pins and set them low
for(int i=0; i<8; i++)
{
pinMode (cam[i], OUTPUT);
digitalWrite (cam[i], LOW); // Set all pins low to turn the relays on (and turn the cameras off)
delay(100);
}
// Initiate secondary relays
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
digitalWrite(relay2, LOW); // Engage bolt
pinMode(relay3, OUTPUT);
digitalWrite(relay3, LOW); // Engage maglock
pinMode(strip1, OUTPUT);
pinMode(strip2, OUTPUT);
pinMode(strip3, OUTPUT);
pinMode(strip4, OUTPUT);
pinMode(strip5, OUTPUT);
// Test reed box
digitalWrite (rReed, HIGH);
delay(200);
digitalWrite (buzzPin, HIGH);
delay(200);
digitalWrite (buzzPin, LOW);
digitalWrite (rReed, LOW);
delay(200);
digitalWrite (gReed, HIGH);
delay(200);
digitalWrite (buzzPin, HIGH);
delay(200);
digitalWrite (buzzPin, LOW);
digitalWrite (gReed, LOW);
delay(200);
digitalWrite (wReed, HIGH);
delay(200);
digitalWrite (buzzPin, HIGH);
delay(200);
digitalWrite (buzzPin, LOW);
delay(200);
Serial.println("Cams initialised");
// Initialise the serial interface to the MP3 player
altSerial.begin(9600);
mp3.begin();
mp3.volume(vol);
switchState = digitalRead(scareSwitch);
if (switchState == 0) // Scares are on
{
mp3.playTrack(2);
digitalWrite(strip1,LOW);
digitalWrite(strip2,LOW);
digitalWrite(strip3,LOW);
digitalWrite(strip4,LOW);
digitalWrite(strip5,LOW);
lightsOn = false; // Update the bool to say lights are off
}
else
{
mp3.playTrack(1);
digitalWrite(strip1,HIGH);
digitalWrite(strip2,HIGH);
digitalWrite(strip3,HIGH);
digitalWrite(strip4,HIGH);
digitalWrite(strip5,HIGH);
lightsOn = true; // Update the bool to say lights are on
}
Serial.println("Setup complete");
}
void loop()
{
switchState = digitalRead(scareSwitch);
if (switchState == 0)
{
digitalWrite (relay1, LOW);
Serial.println ("SPOOKY");
}
else
{
digitalWrite (relay1, HIGH);
Serial.println ("SAFE");
}
reedSwitch.update();
rfidSwitch.update();
circsqtriSwitch.update();
flickerSwitch.update();
// volDownSwitch.update();
// volUpSwitch.update();
if(circsqtriSwitch.fell())
{
Serial.println("1612 complete");
digitalWrite(circsqtriSig, LOW); // Tell the scare uno that 1612 has been input
firstcams();
}
if(rfidSwitch.fell())
{
Serial.println("rfid complete");
digitalWrite (doorSig, LOW); // Tell the scare uno that the door has been opened
dooropen();
}
if(reedSwitch.fell())
{
Serial.println("reed complete");
digitalWrite(reedSig, LOW); // Tell scare uno that the magnet is in the recepticle
firstobjective();
}
if(flickerSwitch.fell())
{
Serial.println("flicker complete");
flicker();
}
}
void flicker()
{
Serial.println(lightsOn);
Serial.println("flickering");
if (lightsOn == true)
{
Serial.println("flickering off");
digitalWrite(strip1, HIGH);
digitalWrite(strip2, HIGH);
digitalWrite(strip3, HIGH);
digitalWrite(strip4, HIGH);
digitalWrite(strip5, HIGH);
delay(random(intMin,intMax)); //1
digitalWrite(strip2, LOW);
delay(random(intMin,intMax)); //2
digitalWrite(strip4, LOW);
delay(random(intMin,intMax)); //3
digitalWrite(strip1, LOW);
digitalWrite(strip2, HIGH);
delay(random(intMin,intMax)); //4
digitalWrite(strip1, HIGH);
digitalWrite(strip3, LOW);
digitalWrite(strip4, HIGH);
delay(random(intMin,intMax)); //5
digitalWrite(strip1, LOW);
digitalWrite(strip2, LOW);
delay(random(intMin,intMax)); //6
digitalWrite(strip2, HIGH);
digitalWrite(strip4, LOW);
delay(random(intMin,intMax)); //7
digitalWrite(strip1, HIGH);
digitalWrite(strip3, HIGH);
digitalWrite(strip5, LOW);
delay(random(intMin,intMax)); //8
digitalWrite(strip1, LOW);
digitalWrite(strip2, LOW);
digitalWrite(strip4, HIGH);
delay(random(intMin,intMax)); //9
digitalWrite(strip3, LOW);
digitalWrite(strip4, LOW);
delay(random(intMin,intMax)); //10
digitalWrite(strip1, HIGH);
digitalWrite(strip4, HIGH);
delay(random(intMin,intMax)); //11
digitalWrite(strip1, LOW);
delay(random(intMin,intMax)); //12
digitalWrite(strip4, LOW);
delay(random(intMin,intMax)); //13
digitalWrite(strip1, LOW);
digitalWrite(strip2, LOW);
digitalWrite(strip3, LOW);
digitalWrite(strip4, LOW);
digitalWrite(strip5, LOW);
lightsOn = false; //Lights are off
Serial.println("lights are off");
}
else
{
Serial.println("flickering on");
digitalWrite(strip1, LOW);
digitalWrite(strip2, LOW);
digitalWrite(strip3, LOW);
digitalWrite(strip4, LOW);
digitalWrite(strip5, LOW);
delay(random(intMin,intMax)); //1
digitalWrite(strip2, HIGH);
delay(random(intMin,intMax)); //2
digitalWrite(strip4, HIGH);
delay(random(intMin,intMax)); //3
digitalWrite(strip1, HIGH);
digitalWrite(strip2, LOW);
delay(random(intMin,intMax)); //4
digitalWrite(strip1, LOW);
digitalWrite(strip3, HIGH);
digitalWrite(strip4, LOW);
delay(random(intMin,intMax)); //5
digitalWrite(strip1, HIGH);
digitalWrite(strip2, HIGH);
delay(random(intMin,intMax)); //6
digitalWrite(strip2, LOW);
digitalWrite(strip4, HIGH);
delay(random(intMin,intMax)); //7
digitalWrite(strip1, LOW);
digitalWrite(strip3, LOW);
digitalWrite(strip5, HIGH);
delay(random(intMin,intMax)); //8
digitalWrite(strip1, HIGH);
digitalWrite(strip2, HIGH);
digitalWrite(strip4, LOW);
delay(random(intMin,intMax)); //9
digitalWrite(strip3, HIGH);
digitalWrite(strip4, HIGH);
delay(random(intMin,intMax)); //10
digitalWrite(strip1, LOW);
digitalWrite(strip4, LOW);
delay(random(intMin,intMax)); //11
digitalWrite(strip1, HIGH);
delay(random(intMin,intMax)); //12
digitalWrite(strip4, HIGH);
delay(random(intMin,intMax)); //13
digitalWrite(strip1, HIGH);
digitalWrite(strip2, HIGH);
digitalWrite(strip3, HIGH);
digitalWrite(strip4, HIGH);
digitalWrite(strip5, HIGH);
Serial.println("lights are on");
lightsOn = true; // Lights are on
}
}
void firstcams()
{
// Turn on cams 1 to 4
digitalWrite (cam[0], HIGH);
delay(100);
digitalWrite (cam[1], HIGH);
delay(100);
digitalWrite (cam[2], HIGH);
delay(100);
digitalWrite (cam[3], HIGH);
delay(100);
// Attention getting buzzes
digitalWrite (buzzPin, HIGH);
delay(400);
digitalWrite (buzzPin, LOW);
delay(400);
digitalWrite (buzzPin, HIGH);
delay(400);
digitalWrite (buzzPin, LOW);
delay(400);
digitalWrite (buzzPin, HIGH);
delay(400);
digitalWrite (buzzPin, LOW);
delay(400);
// Cam 1 lights
digitalWrite (buzzPin, HIGH);
digitalWrite (r1, LOW);
delay(100);
digitalWrite (buzzPin, LOW);
digitalWrite (r1, HIGH);
delay(150);
digitalWrite (buzzPin, HIGH);
digitalWrite (r1, LOW);
delay(100);
digitalWrite (buzzPin, LOW);
digitalWrite (r1, HIGH);
delay(150);
digitalWrite (buzzPin, HIGH);
digitalWrite (r1, LOW);
delay(100);
digitalWrite (buzzPin, LOW);
digitalWrite (r1, HIGH);
delay(150);
digitalWrite (r1, LOW);
delay(50);
digitalWrite (buzzPin, HIGH);
digitalWrite (g1, HIGH);
delay(500);
digitalWrite (buzzPin, LOW);
delay(100);
// Cam 2 lights
digitalWrite (buzzPin, HIGH);
digitalWrite (r2, LOW);
delay(100);
digitalWrite (buzzPin, LOW);
digitalWrite (r2, HIGH);
delay(150);
digitalWrite (buzzPin, HIGH);
digitalWrite (r2, LOW);
delay(100);
digitalWrite (buzzPin, LOW);
digitalWrite (r2, HIGH);
delay(150);
digitalWrite (buzzPin, HIGH);
digitalWrite (r2, LOW);
delay(100);
digitalWrite (buzzPin, LOW);
digitalWrite (r2, HIGH);
delay(150);
digitalWrite (r2, LOW);
delay(50);
digitalWrite (buzzPin, HIGH);
digitalWrite (g2, HIGH);
delay(500);
digitalWrite (buzzPin, LOW);
delay(100);
// Cam 2 lights
digitalWrite (buzzPin, HIGH);
digitalWrite (r3, LOW);
delay(100);
digitalWrite (buzzPin, LOW);
digitalWrite (r3, HIGH);
delay(150);
digitalWrite (buzzPin, HIGH);
digitalWrite (r3, LOW);
delay(100);
digitalWrite (buzzPin, LOW);
digitalWrite (r3, HIGH);
delay(150);
digitalWrite (buzzPin, HIGH);
digitalWrite (r3, LOW);
delay(100);
digitalWrite (buzzPin, LOW);
digitalWrite (r3, HIGH);
delay(150);
digitalWrite (r3, LOW);
delay(50);
digitalWrite (buzzPin, HIGH);
digitalWrite (g3, HIGH);
delay(500);
digitalWrite (buzzPin, LOW);
delay(100);
// Cam 4 lights
digitalWrite (buzzPin, HIGH);
digitalWrite (r4, LOW);
delay(100);
digitalWrite (buzzPin, LOW);
digitalWrite (r4, HIGH);
delay(150);
digitalWrite (buzzPin, HIGH);
digitalWrite (r4, LOW);
delay(100);
digitalWrite (buzzPin, LOW);
digitalWrite (r4, HIGH);
delay(150);
digitalWrite (buzzPin, HIGH);
digitalWrite (r4, LOW);
delay(100);
digitalWrite (buzzPin, LOW);
digitalWrite (r4, HIGH);
delay(150);
digitalWrite (r4, LOW);
delay(50);
digitalWrite (buzzPin, HIGH);
digitalWrite (g4, HIGH);
delay(500);
digitalWrite (buzzPin, LOW);
delay(1000);
digitalWrite (relay3, HIGH);
digitalWrite (buzzPin, HIGH);
delay(600);
digitalWrite(buzzPin, LOW);
return;
}
void dooropen()
{
if (switchState == 0)
{
delay(JSmp3); // Provide delay for longer voice over
}
else
{
delay(NSmp3); // Provide delay for less scary voice over
}
// Turn on cams 5, 6 and 7
digitalWrite (cam[4], HIGH);
delay(100);
digitalWrite (cam[5], HIGH);
delay(100);
digitalWrite (cam[6], HIGH);
delay(100);
// Attention getting buzzes
for (int i=0;i<3;i++)
{
digitalWrite (r5, LOW);
digitalWrite (r6, LOW);
digitalWrite (r7, LOW);
digitalWrite (buzzPin, HIGH);
delay(400);
digitalWrite (buzzPin, LOW);
digitalWrite (r5, HIGH);
digitalWrite (r6, HIGH);
digitalWrite (r7, HIGH);
delay(400);
}
// Notify players that the cams have been made available
digitalWrite (r5, LOW);
digitalWrite (r6, LOW);
digitalWrite (r7, LOW);
delay(250);
digitalWrite (g5, HIGH);
digitalWrite (g6, HIGH);
digitalWrite (g7, HIGH);
digitalWrite (buzzPin, HIGH);
delay(1000);
digitalWrite (buzzPin, LOW);
// Unlock main door
digitalWrite (relay2, HIGH);
digitalWrite (buzzPin, HIGH);
delay(600);
digitalWrite (buzzPin, LOW);
if (switchState == 0) // If scare mode is on, start the sequence to control the volume up and down sequence
{
Serial.println("jump scares are on");
delay(1000);
jumpScare = true;
do
{
volDownSwitch.update();
// volUpSwitch.update();
Serial.println("listening for ultrasound and plug");
if(volDownSwitch.fell())
{
Serial.println("ultrasound triggered");
for (int i=0;i<30;i++)
{
mp3.volumeDec();
delay(volInterval);
}
mp3.volume(0);
}
if(volUpSwitch.fell())
{
Serial.println("plug connected");
delay(FHstrike); //Delay for the facehugger to strike
flicker(); // Flicker the lights on
mp3.playTrack(3); // Play the spooky alien stompy ambience
for (int i=0;i<30;i++)
{
mp3.volumeInc();
delay(volInterval);
}
mp3.volume(30);
jumpScare = false; // Now that the volumeUp has been triggered, push us out of the do while loop
}
} while (jumpScare == true);
Serial.println("left do loop");
}
return;
}
void firstobjective()
{
digitalWrite (wReed, LOW);
for (int i=0;i<8;i++)
{
digitalWrite (rReed, HIGH);
delay(250);
digitalWrite (rReed, LOW);
delay(250);
}
digitalWrite (gReed, HIGH);
digitalWrite (cam[7], HIGH); // Turn on camera
delay(2000);
for (int i=0;i<10;i++)
{
digitalWrite (r8, LOW);
digitalWrite (buzzPin, HIGH);
delay(250);
digitalWrite (r8, HIGH);
digitalWrite (buzzPin, LOW);
delay(250);
}
digitalWrite(r8, LOW);
delay(100);
digitalWrite (g8, HIGH);
digitalWrite (buzzPin, HIGH);
delay(1000);
digitalWrite (buzzPin, LOW);
delay(500);
return;
}
Mega diagram
Keypad code (This behaves in a way that it should)
// Include libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Length of password + 1 for null character
#define Password_Length 5
// Character to hold password input
char Data[Password_Length];
// Password
char pass[Password_Length] = "2193";
// Pin connected to LED control, relay and buzzer
int lockPin = 12;
int buzzPin = 13;
int rPin = 14;
int gPin = 27;
int successPin = 26;
// Counter for character entries
byte data_count = 0;
// Character to hold key input
char customKey;
// Constants for row and column sizes
const byte ROWS = 4;
const byte COLS = 4;
// Array to represent keys on keypad
char hexaKeys[ROWS][COLS] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Connections to Arduino
byte rowPins[ROWS] = {16, 17, 5, 18};
byte colPins[COLS] = {15, 2, 0, 4};
// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// Create LCD object
LiquidCrystal_I2C lcd(0x27, 16, 2); // other address is 3f
void setup()
{
// Setup LCD with backlight and initialize
lcd.backlight();
lcd.init();
Serial.begin (9600);
Serial.println ("Serial has begun");
// Set pin modes
pinMode(lockPin, OUTPUT);
digitalWrite(lockPin, HIGH);
pinMode(buzzPin, OUTPUT);
pinMode(rPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(successPin, OUTPUT);
// Initiate test
delay (2000);
for (int i = 0; i < 2; i++)
{
Serial.println ("test");
digitalWrite (rPin, LOW);
digitalWrite (gPin, LOW);
digitalWrite (buzzPin, LOW);
delay (200);
// lcd.print ("test");
digitalWrite (rPin, HIGH);
delay (200);
digitalWrite (gPin, HIGH);
delay (200);
digitalWrite (buzzPin, HIGH);
delay (200);
digitalWrite (buzzPin, LOW);
digitalWrite (rPin, LOW);
digitalWrite (gPin, LOW);
delay (200);
digitalWrite (rPin, HIGH);
delay (200);
digitalWrite (rPin, LOW);
delay (200);
digitalWrite (gPin, HIGH);
delay (200);
digitalWrite (gPin, LOW);
delay (200);
digitalWrite (buzzPin, HIGH);
delay (200);
digitalWrite (buzzPin, LOW);
delay (200);
}
}
void loop()
{
digitalWrite (rPin, HIGH);
// Initialize LCD and print
lcd.setCursor(0, 0);
lcd.print("JUMP 7 CODE:");
// Look for keypress
customKey = customKeypad.getKey();
if (customKey)
{
// Enter keypress into array and increment counter
Data[data_count] = customKey;
lcd.setCursor(data_count, 1);
lcd.print(Data[data_count]);
data_count++;
// Play a buzz when a key is pressed
digitalWrite (buzzPin, HIGH);
digitalWrite (gPin, HIGH);
digitalWrite (rPin, LOW);
delay (100);
digitalWrite (buzzPin, LOW);
digitalWrite (gPin, LOW);
digitalWrite (rPin, HIGH);
}
// See if we have reached the password length
if (data_count == Password_Length - 1)
{
lcd.clear();
if (!strcmp(Data, pass))
{
// Password is correct
lcd.print("VIEWPORT OPEN");
delay(500);
digitalWrite (gPin, HIGH);
digitalWrite (rPin, LOW);
delay (1000);
digitalWrite (successPin, LOW); // Tell mega to initiate firstCams
digitalWrite (buzzPin, HIGH);
digitalWrite (lockPin, LOW);
delay (600);
digitalWrite (successPin, HIGH);
digitalWrite (buzzPin, LOW);
digitalWrite (lockPin, HIGH);
delay(1800000);
lcd.clear();
clearData();
delay (100);
digitalWrite (buzzPin, LOW);
digitalWrite (rPin, HIGH);
digitalWrite (gPin, LOW);
return;
}
else
{
// Password is incorrect, play a quick double tone and display error message
lcd.print("INCORRECT CODE");
delay(500);
digitalWrite(buzzPin, HIGH);
digitalWrite(rPin, HIGH);
delay(50);
digitalWrite(buzzPin, LOW);
digitalWrite(rPin, LOW);
delay(75);
digitalWrite(buzzPin, HIGH);
digitalWrite(rPin, HIGH);
delay(50);
digitalWrite(buzzPin, LOW);
digitalWrite(rPin, LOW);
delay(75);
digitalWrite(buzzPin, HIGH);
digitalWrite(rPin, HIGH);
delay(50);
digitalWrite(buzzPin, LOW);
digitalWrite(rPin, LOW);
delay (1000);
}
// Clear data and LCD display
lcd.clear();
lcd.print("Data reset");
digitalWrite (buzzPin, HIGH);
delay (500);
digitalWrite (buzzPin, LOW);
lcd.clear();
clearData();
}
}
void clearData() {
// Go through array and clear data
while (data_count != 0)
{
Data[data_count--] = 0;
}
return;
}



