Bomb Defuse Game Programming Help

Hi all,

First time posting and Arduino newbie. I am currently working on a project for an escape-room type puzzle where the players have to defuse a fake bomb by cutting the wires in the correct sequence.

I have been using Tinkercad to build a proof of concept but am having some issues with the code; specifically building a conditional with multiple layers. I have some programming experience with Python but I am new to Arduino’s.

/*Redpin/greenpin determines wires set to light an RGB LED \
red/green and the Stat varibales are for the wires to be cut.*/
int redPin1 = 13;
int greenPin1 = 12;
int redPin2 = 11;
int greenPin2 = 10;
int redPin3 = 9;
int greenPin3 = 8;
int redPin4 = 7;
int greenPin4 = 6;
int redPin5 = 5;
int greenPin5 = 4;
int redStat = 3;
int orangeStat = 2;
int yellowStat = 1;

/*Setting my Ins/Outs, and also setting inital lighting for my \
LED's as all red.*/
void setup()
{
pinMode(redPin1, OUTPUT);
pinMode(greenPin1, OUTPUT); 
pinMode(redPin2, OUTPUT);
pinMode(greenPin2, OUTPUT);
pinMode(redPin3, OUTPUT);
pinMode(greenPin3, OUTPUT);
pinMode(redPin4, OUTPUT);
pinMode(greenPin4, OUTPUT);
pinMode(redPin5, OUTPUT);
pinMode(greenPin5, OUTPUT);
pinMode(redStat, INPUT);
pinMode(orangeStat, INPUT);
pinMode(yellowStat, INPUT);
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
digitalWrite(10,  LOW);
digitalWrite(9, HIGH);
digitalWrite(8, LOW);
}

/*A function that if the incorrect wire is cut flips all lights \
to red.*/
void failWrite()
{
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
digitalWrite(8, LOW);
}

void loop()
{
if ((digitalRead(redStat) == LOW) && (digitalRead(orangeStat)==HIGH))
{
 if (digitalRead(yellowStat)==HIGH)
  {
  digitalWrite(13, LOW);
  digitalWrite(12, HIGH);
  }
  else 
  {
  failWrite();
  }
}
if ((digitalRead(orangeStat)==LOW) && (digitalRead(redStat)==LOW))
{
  if(digitalRead(yellowStat)==HIGH)
  { 
  digitalWrite(11, LOW);
  digitalWrite(10, HIGH);
  }
  else
 {
  failWrite();
 }
}
}

I have attached a graphic of my wiring as well. I know my logic is off because when the red and orange wires are cut, red goes back to red but orange stays green. I am not sure how to build a function with multiple conditionals that would satisfy my requirements (some kind of nested function maybe?) If there are any tips you can give in regards to my logic or even my wiring it would be greatly appreciated. Sorry for being a noob!

Did you look at that image before you attached it?

Pete

Not well enough apparently. Correct picture is now attached.

It appears that you have all your Stat wires connected to 5V and declared as INPUTs. When you "cut" one of those wires, the input is neither connected to 5V or GND so who knows what the state of the pin will be when you read it.

What you need to do is connect them all to GND and declare them as INPUT_PULLUP and then change your logic around such that reading the pin as LOW means it is connected and reading the pin as HIGH means the wire has been cut.

You also go do the trouble of declaring your pins as variables at the beginning of the program (GOOD)
(Note: they should be 'const int' since they never change)

int redPin1 = 13;
int greenPin1 = 12;
int redPin2 = 11;
int greenPin2 = 10;
int redPin3 = 9;
int greenPin3 = 8;
int redPin4 = 7;
int greenPin4 = 6;
int redPin5 = 5;
int greenPin5 = 4;

and then proceed to use the actual pin numbers in your code (BAD). That kinda defeats the purpose.

void failWrite()
{
  digitalWrite(13, HIGH);
  digitalWrite(12, LOW);
  digitalWrite(11, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(9, HIGH);
  digitalWrite(8, LOW);
}

a fake bomb
And we are supposed to believe you?

+5v to pin one can blow up your bomb !

Updated my code as per your recommendations, thank you for the input. Thanks for your patience with my rookie mistakes.

Also changed my wiring as per your recommendations. I will test when I get home as I have been working with this simulation while at work. My hope is as long as this works currently to expand it to 5-6 wires. As it stands currently are there any potential glaring issues having made the change to INPUT_PULLDOWN? Not 100% sure I incorporated that correctly as my simulation seems to be acting funky compared to how it did before. Thanks again for your input.

/*Redpin/greenpin determines whether wires are set to light an RGB LED 
red/green, wireState is used to determine whether wire is off/on and 
priorState is set initially to 0 and changes to one after it has been cut.
Success tracks number of wires successfully cut. */

const int redPin1 = 13;
const int greenPin1 = 12;
const int redPin2 = 11;
const int greenPin2 = 10;
const int redPin3 = 9;
const int greenPin3 = 8;
const int redPin4 = 7;
const int greenPin4 = 6;
const int redPin5 = 5;
const int greenPin5 = 4;
const int redWirePin = 3;
const int orangeWirePin = 2;
const int yellowWirePin = 1;
int redWireState = 0;
int orangeWireState = 0;
int yellowWireState = 0;
int redPriorState = 0;
int orangePriorState = 0;
int yellowPriorState = 0;
int success = 0;
  
/*Setting my Ins/Outs, and also setting inital lighting for my 
LED's as all red.*/
void setup()
{
  pinMode(redPin1, OUTPUT);
  pinMode(greenPin1, OUTPUT); 
  pinMode(redPin2, OUTPUT);
  pinMode(greenPin2, OUTPUT);
  pinMode(redPin3, OUTPUT);
  pinMode(greenPin3, OUTPUT);
  pinMode(redPin4, OUTPUT);
  pinMode(greenPin4, OUTPUT);
  pinMode(redPin5, OUTPUT);
  pinMode(greenPin5, OUTPUT);
  pinMode(redWirePin, INPUT_PULLUP);
  pinMode(orangeWirePin, INPUT_PULLUP);
  pinMode(yellowWirePin, INPUT_PULLUP);
  digitalWrite(redPin1, HIGH);
  digitalWrite(greenPin1, LOW);
  digitalWrite(redPin2, HIGH);
  digitalWrite(greenPin2,  LOW);
  digitalWrite(redPin3, HIGH);
  digitalWrite(greenPin3, LOW);
  redWireState = digitalRead(redWirePin);
  orangeWireState = digitalRead(orangeWirePin);
  yellowWireState = digitalRead(yellowWirePin);
}

/*A function that if the incorrect wire is cut flashes all lights 
to red.*/
void failWrite()
{
  digitalWrite(redPin1, HIGH);
  delay(200);
  digitalWrite(redPin1, LOW);
  delay(200);
  digitalWrite(greenPin1, LOW);
  digitalWrite(redPin2, HIGH);
  delay(200);
  digitalWrite(redPin2, LOW);
  delay(200);
  digitalWrite(greenPin2, LOW);
  digitalWrite(redPin3, HIGH);
  delay(200);  
  digitalWrite(redPin3, LOW);
  delay(200);
  digitalWrite(greenPin3, LOW);
}

/*Used with success counter to flash lights green.*/
void victoryWrite()
{
  digitalWrite(redPin1, LOW);
  digitalWrite(greenPin1, HIGH);
  delay(200);
  digitalWrite(greenPin1, LOW);
  delay(200);
  digitalWrite(redPin2, LOW);
  digitalWrite(greenPin2, HIGH);
  delay(200);
  digitalWrite(greenPin2, LOW);
  delay(200);
  digitalWrite(redPin3, LOW);
  digitalWrite(greenPin3, HIGH);
  delay(200);
  digitalWrite(greenPin3, LOW);
  delay(200);
}

void loop()
{
 redWireState = digitalRead(redWirePin);
 orangeWireState = digitalRead(orangeWirePin);
 yellowWireState = digitalRead(yellowWirePin);
  
  if ((redWireState==HIGH)&&(redPriorState==0))
  {
    if ((orangePriorState==0)&&(yellowPriorState==0))
        {
        digitalWrite(redPin1, LOW);
        digitalWrite(greenPin1, HIGH);
      	redPriorState==1;
      	success=+1;
        }
     else 
        {
          failWrite();
        }
  }
  if ((orangeWireState==HIGH)&&(orangePriorState==0))
  {
    if ((redPriorState==1)&&(yellowPriorState==0))
    {
      digitalWrite(redPin2, LOW);
      digitalWrite(greenPin2, HIGH);
      orangePriorState==1;
      success=+1;
    }
    else
    {
      failWrite();
    }
  }
  if ((yellowWireState==HIGH)&&(orangePriorState==0))
  {
    if ((redPriorState==1)&&(orangePriorState==1))
    {
      digitalWrite(redPin3, LOW);
      digitalWrite(greenPin3, HIGH);
      yellowPriorState==1;
      success=+1;
    }
    else
    {
      failWrite();
    }
   if (success==3)
   {
     victoryWrite();
   }
  }
 
}

Now you have pin 1 going to ground.
This can blow up your Arduino.

Pin 1 is the serial transmit pin.
At power up this pin is set as output.
If you connect pin 1 to either +5v or 0v you will blow the pin, but this is a bomb so maybe you want this to happen. :wink:

BTW, you can use A0-A5 (14-19) as digital inputs or outputs.

Oh man, thanks a ton for the heads up there! I will actually be using a Mega board which will have more than sufficient I/O's for my purposes- the simulator just doesn't have that. But valuable knowledge gained nonetheless!

What do I need to change in terms of my wiring? Am I incorrect in grounding them like that or should they be how they were before? Glad I did not test it that way when I got home.

Suggest you put input pins like this to GND through a minimum of 1K.

Arduino pin ———— GND <——-<<<< No!

Use a resistor:
Arduino pin ———— 1K through 10K ——— GND <——-<<<< Yes

Thanks all for the help- with Larry's wiring advice everything is working like a charm this morning. Thanks for helping a newbie.

larryd:
Arduino pin ———— 1K through 10K ——— GND <——-<<<< Yes

Would 10K definitely be ok? The internal pull-ups could be around 30K, resulting in around 1V at the input pin. Is that guaranteed to read LOW?

Listen to Paul, 1K sould be used.