AIrsoft Time Bomb Game

Hey everyone....I know this topic has been discussed before but I didn't see any resolutions on, only that it was closed. I have found many codes online for airsoft time bomb games and as I am just getting started in learning how to code, I am hoping some of you can help me out a little.
Here is what I want to do.

  1. Have a menu to select the game time.
  2. Arm the bomb with a 4 digit code.
  3. Use an RGB strip to chase each second with a tone playing for the second.
  4. Use the same RGB strip to blink Green and another color if the bomb is disabled while playing 2 alternating tones.
  5. If the bomb code is put in wrong, say wrong code on the LCD and take 2 minutes off the game time and play an irritating tone....lol
  6. When under 2 minutes, if wrong code entered, take 59 secs off.
  7. If the time runs out or too many attempts to disarm have caused the time to run out, play alternating horrible tones, display BOOM You're DEAD and use the RGB strip to flash RED, YELLOW and ORANGE colors.

Honestly, a lot of that is already in the code with some changes I have already made but I am having issues. Here are the issues:

  1. I think some of what is in the void setup area should be in the void loop area but when I move it, it constantly loops putting in a new code and never starts the game.
  2. If I leave the voide setup alone and add the RGB strip to the places where I want to use it instead of a single LED, the countdown part works, but now you cant push the key on the keypad to enter the disable code.
    Those are my issues. Here is the code.....Im sure I am going to post this wrong so please tell me how to do it right.

This is the code with it working without the RGB Strip:

#include <Tone.h>


#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Tone.h>
#define pound 14

Tone tone1;

int Scount = 00; // count seconds
int Mcount = 10; // count minutes
int Hcount = 0; // count hours
int DefuseTimer = 60; // set timer to 0

long secMillis = 0; // store last time for second add
long interval = 1000; // interval for seconds

char password[6]; // number of characters in our password
int currentLength = 0; //defines which number we are currently writing
int i = 0; 
char entered[6];

int redPin = 11; //red led
int ledPin = 10; //yellow led
int greenPin = 10; //green led

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3','a'}
  ,
  {'4','5','6','b'}
  ,
  {'7','8','9','c'}
  ,
  {'*','0','#','d'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A4, A5, 13,12}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup(){
  pinMode(redPin, OUTPUT); // sets the digital pin as output
  pinMode(redPin, OUTPUT); // sets the digital pin as output
  pinMode(greenPin, OUTPUT); // sets the digital pin as output
  tone1.begin(8);
  lcd.begin(16, 2);
  Serial.begin(9600);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Enter Arm Code: ");
  lcd.setCursor(0,1);
  while (currentLength < 4)
  {
    lcd.setCursor(currentLength + 6, 1);
    lcd.cursor();
    char key = keypad.getKey();
    key == NO_KEY;
    if (key != NO_KEY)
    {
      if ((key != '*')&&(key != '#'))
      { 
      lcd.print(key);
      password[currentLength] = key;
      currentLength++;
      tone1.play(NOTE_C6, 200);
      }
    }
  }

  if (currentLength == 4)
  {
    delay(500);
    lcd.noCursor();
    lcd.clear();
    lcd.home();
    lcd.print("You've Entered: ");
    lcd.setCursor(6,1);
    lcd.print(password[0]);
    lcd.print(password[1]);
    lcd.print(password[2]);
    lcd.print(password[3]);
    

    tone1.play(NOTE_E6, 200);
    delay(3000);
    lcd.clear();
    currentLength = 0;
  }
}

void loop()
{
 
  timer();
  char key2 = keypad.getKey(); // get the key
  
  if (key2 == '#')
    {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Code: ");
      
      while (currentLength < 4)
        {
          
          timer();
          
          char key2 = keypad.getKey(); 
          if (key2 == '*')
            {
              currentLength = 0;
              lcd.clear();
              lcd.setCursor(0,0);
              lcd.print("Code: ");
            }
          else                  
          if (key2 != NO_KEY)    
            {
              
              lcd.setCursor(currentLength + 7, 0);
              lcd.cursor();
              
              lcd.print(key2);
              entered[currentLength] = key2;
              currentLength++;
              tone1.play(NOTE_C6, 200);
              delay(100);
              lcd.noCursor();
              lcd.setCursor(currentLength + 6, 0);
              lcd.print("*");
              lcd.setCursor(currentLength + 7, 0);
              lcd.cursor();
            }
        }

      if (currentLength == 4) 
        {
          if (entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3] && entered[4] == password[4])
            {
              lcd.noCursor();
              lcd.clear();
              lcd.home();
              lcd.print("Bomb Defused");
              currentLength = 4;
              digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100); 
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
              digitalWrite(greenPin, HIGH);
              delay(2500);
              lcd.setCursor(0,1);
              lcd.print("Press Reset");
              delay(1000000);
            }
      else
        {
          lcd.noCursor();
          lcd.clear();
          lcd.home();
          lcd.print("Wrong De-arm");
          lcd.setCursor (0,1);
          lcd.print("     Code!");
          tone1.play(NOTE_A2, 90);
          delay(100);
          tone1.play(NOTE_A2, 90);
              
          if (Hcount > 0)
            {
              Hcount = Hcount - 1;
            }
    
          if (Mcount > 0)
            {
              Mcount = Mcount - 2;
            }
          if (Scount > 0)
            {
              Scount = Scount - 59;
            }
        delay(1500);
        currentLength = 0;
  
        }
      }  
    }
}

void timer()
{
  Serial.print(Scount);
  Serial.println();
  
  if (Hcount <= 0)
  {
   if ( Mcount < 0 )
    {
      
      lcd.noCursor();
      lcd.clear();
      lcd.home();
      lcd.print("* * BOOM * *");
      lcd.setCursor (0,1);
      lcd.print("You're DEAD!");
      
      while (Mcount < 0) 
      {
        digitalWrite(redPin, HIGH); // sets the LED on
        tone1.play(NOTE_A2, 90);
        delay(100); 
        digitalWrite(redPin, LOW); // sets the LED off
        tone1.play(NOTE_A2, 90);
        delay(100); 
        digitalWrite(redPin, HIGH); // sets the LED on
        tone1.play(NOTE_A2, 90);
        delay(100); 
        digitalWrite(redPin, LOW); // sets the LED off
        tone1.play(NOTE_A2, 90);
        delay(100); 
        digitalWrite(greenPin, HIGH); // sets the LED on
        tone1.play(NOTE_A2, 90);
        delay(100); 
        digitalWrite(greenPin, LOW); // sets the LED off
        tone1.play(NOTE_A2, 90);
        delay(100);
      }
    } 
  }

  lcd.setCursor (0,1); // sets cursor to 2nd line
  lcd.print ("Timer:");

  if (Hcount >= 10)
    {
      lcd.setCursor (7,1);
      lcd.print (Hcount);
    }
  if (Hcount < 10) 
    {
      lcd.setCursor (7,1);
      lcd.write ("0");
      lcd.setCursor (8,1);
      lcd.print (Hcount);
    }

  lcd.print (":");

  if (Mcount >= 10)
    {
      lcd.setCursor (10,1);
      lcd.print (Mcount);
    }
  if (Mcount < 10) 
    {
      lcd.setCursor (10,1);
      lcd.write ("0");
      lcd.setCursor (11,1);
      lcd.print (Mcount);
    }
    
  lcd.print (":");

  if (Scount >= 10) 
    {
      lcd.setCursor (13,1);
      lcd.print (Scount);
    }
  if (Scount < 10) 
    {
      lcd.setCursor (13,1);
      lcd.write ("0");
      lcd.setCursor (14,1);
      lcd.print (Scount);
    }

  if (Hcount <0) 
    {
      Hcount = 0; 
    }

  if (Mcount <0) 
    {
      Hcount --; 
      Mcount = 59; 
    }

  if (Scount <1) // if 60 do this operation
    {
      Mcount --; // add 1 to Mcount
      Scount = 59; // reset Scount
    }

  if (Scount > 0) // do this oper. 59 times
    {
      unsigned long currentMillis = millis();
  
      if(currentMillis - secMillis > interval) 
        {
          tone1.play(NOTE_G5, 200);
          secMillis = currentMillis;
          Scount --; // add 1 to Scount
          digitalWrite(redPin, HIGH); // sets the LED on
          delay(10); // waits for a second
          digitalWrite(redPin, LOW); // sets the LED off
          delay(10); // waits for a second
          //lcd.clear();
        }
    }
}

This is the code with the RGB strip and not working:

#include <Tone.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Tone.h>
#define pound 14
#define NUM_LEDS 10
#include <FastLED.h>
#define DATA_PIN 9
CRGB leds[NUM_LEDS];

Tone tone1;

int Scount = 00; // count seconds
int Mcount = 10; // count minutes
int Hcount = 0; // count hours
int DefuseTimer = 60; // set timer to 0

long secMillis = 0; // store last time for second add
long interval = 1000; // interval for seconds

char password[6]; // number of characters in our password
int currentLength = 0; //defines which number we are currently writing
int i = 0; 
char entered[6];

int redPin = 11; //red led
int ledPin = 10; //yellow led
int greenPin = 10; //green led

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3','a'}
  ,
  {'4','5','6','b'}
  ,
  {'7','8','9','c'}
  ,
  {'*','0','#','d'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A4, A5, 13,12}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup(){
//  pinMode(redPin, OUTPUT); // sets the digital pin as output
//  pinMode(redPin, OUTPUT); // sets the digital pin as output
//  pinMode(greenPin, OUTPUT); // sets the digital pin as output
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  tone1.begin(8);
  lcd.begin(16, 2);
  Serial.begin(9600);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Enter Arm Code: ");
  lcd.setCursor(0,1);
  while (currentLength < 4)
  {
    lcd.setCursor(currentLength + 6, 1);
    lcd.cursor();
    char key = keypad.getKey();
    key == NO_KEY;
    if (key != NO_KEY)
    {
      if ((key != '*')&&(key != '#'))
      { 
      lcd.print(key);
      password[currentLength] = key;
      currentLength++;
      tone1.play(NOTE_C6, 200);
      }
    }
  }

  if (currentLength == 4)
  {
    delay(500);
    lcd.noCursor();
    lcd.clear();
    lcd.home();
    lcd.print("You've Entered: ");
    lcd.setCursor(6,1);
    lcd.print(password[0]);
    lcd.print(password[1]);
    lcd.print(password[2]);
    lcd.print(password[3]);
    

    tone1.play(NOTE_E6, 200);
    delay(3000);
    lcd.clear();
    currentLength = 0;
  }
}

void loop()
{
 
  timer();
  char key2 = keypad.getKey(); // get the key
  
  if (key2 == '#')
    {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Code: ");
      
      while (currentLength < 4)
        {
          
          timer();
          
          char key2 = keypad.getKey(); 
          if (key2 == '*')
            {
              currentLength = 0;
              lcd.clear();
              lcd.setCursor(0,0);
              lcd.print("Code: ");
            }
          else                  
          if (key2 != NO_KEY)    
            {
              
              lcd.setCursor(currentLength + 7, 0);
              lcd.cursor();
              
              lcd.print(key2);
              entered[currentLength] = key2;
              currentLength++;
              tone1.play(NOTE_C6, 200);
              delay(100);
              lcd.noCursor();
              lcd.setCursor(currentLength + 6, 0);
              lcd.print("*");
              lcd.setCursor(currentLength + 7, 0);
              lcd.cursor();
            }
        }

      if (currentLength == 4) 
        {
          if (entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3] && entered[4] == password[4])
            {
              lcd.noCursor();
              lcd.clear();
              lcd.home();
              lcd.print("Bomb Defused");
              currentLength = 4;
              digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100); 
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
               digitalWrite(greenPin, HIGH);
              tone1.play(NOTE_A7, 90);
              delay(100); 
              digitalWrite(greenPin, LOW);
              tone1.play(NOTE_A5, 90);
              delay(100);
              digitalWrite(greenPin, HIGH);
              delay(2500);
              lcd.setCursor(0,1);
              lcd.print("Press Reset");
              delay(1000000);
            }
      else
        {
          lcd.noCursor();
          lcd.clear();
          lcd.home();
          lcd.print("Wrong De-arm");
          lcd.setCursor (0,1);
          lcd.print("     Code!");
          tone1.play(NOTE_A2, 90);
          delay(100);
          tone1.play(NOTE_A2, 90);
              
          if (Hcount > 0)
            {
              Hcount = Hcount - 1;
            }
    
          if (Mcount > 0)
            {
              Mcount = Mcount - 2;
            }
          if (Scount > 0)
            {
              Scount = Scount - 59;
            }
        delay(1500);
        currentLength = 0;
  
        }
      }  
    }
}

void timer()
{
  Serial.print(Scount);
  Serial.println();
  
  if (Hcount <= 0)
  {
   if ( Mcount < 0 )
    {
      
      lcd.noCursor();
      lcd.clear();
      lcd.home();
      lcd.print("* * BOOM * *");
      lcd.setCursor (0,1);
      lcd.print("You're DEAD!");
      
      while (Mcount < 0) 
      {
        digitalWrite(redPin, HIGH); // sets the LED on
        tone1.play(NOTE_A2, 90);
        delay(100); 
        digitalWrite(redPin, LOW); // sets the LED off
        tone1.play(NOTE_A2, 90);
        delay(100); 
        digitalWrite(redPin, HIGH); // sets the LED on
        tone1.play(NOTE_A2, 90);
        delay(100); 
        digitalWrite(redPin, LOW); // sets the LED off
        tone1.play(NOTE_A2, 90);
        delay(100); 
        digitalWrite(greenPin, HIGH); // sets the LED on
        tone1.play(NOTE_A2, 90);
        delay(100); 
        digitalWrite(greenPin, LOW); // sets the LED off
        tone1.play(NOTE_A2, 90);
        delay(100);
      }
    } 
  }

  lcd.setCursor (0,1); // sets cursor to 2nd line
  lcd.print ("Timer:");

  if (Hcount >= 10)
    {
      lcd.setCursor (7,1);
      lcd.print (Hcount);
    }
  if (Hcount < 10) 
    {
      lcd.setCursor (7,1);
      lcd.write ("0");
      lcd.setCursor (8,1);
      lcd.print (Hcount);
    }

  lcd.print (":");

  if (Mcount >= 10)
    {
      lcd.setCursor (10,1);
      lcd.print (Mcount);
    }
  if (Mcount < 10) 
    {
      lcd.setCursor (10,1);
      lcd.write ("0");
      lcd.setCursor (11,1);
      lcd.print (Mcount);
    }
    
  lcd.print (":");

  if (Scount >= 10) 
    {
      lcd.setCursor (13,1);
      lcd.print (Scount);
    }
  if (Scount < 10) 
    {
      lcd.setCursor (13,1);
      lcd.write ("0");
      lcd.setCursor (14,1);
      lcd.print (Scount);
    }

  if (Hcount <0) 
    {
      Hcount = 0; 
    }

  if (Mcount <0) 
    {
      Hcount --; 
      Mcount = 59; 
    }

  if (Scount <1) // if 60 do this operation
    {
      Mcount --; // add 1 to Mcount
      Scount = 59; // reset Scount
    }

  if (Scount > 0) // do this oper. 59 times
    {
      unsigned long currentMillis = millis();
  
      if(currentMillis - secMillis > interval) 
        {
          tone1.play(NOTE_G5, 200);
          secMillis = currentMillis;
          Scount --; // add 1 to Scount
          for(int dot=(NUM_LEDS-1) ; dot >=0 ; dot--)

{

leds[dot] = CRGB::Green;

FastLED.show();

leds[dot] = CRGB::Black;

delay(50);

}

for(int dot = 0;dot < NUM_LEDS; dot++)

{ leds[dot] = CRGB::Red;

FastLED.show();

leds[dot] = CRGB::Black;

delay(50);
        }
    }
    }
}

You should focus on this first. You want to eat more than you can chew…

Do simple projects with each component and understand the foundation of the language

Thank you for your reply but I am looking for help and not words of wisdom. :wink:

OK I thought I was being helpful. Good luck

Your help is very appreciated, but I still need to solve the issue. Maybe some guidance would help?

really the code is just a big mess... with blocking sections and delays or pieces that show you just don't understand what you do like

    char key = keypad.getKey();
    key == NO_KEY; // <==== WHY ??
    if (key != NO_KEY)

it would need a complete rewrite as a state machine for example.. hence the recommendation to start from building a better foundation...

Gottcha.....yeah I dont know why the No Key is there.....but Im trying to do this for my kids and its taking too long to learn it on my own. Im watching youtube videos but I am actually learning more from trying to fix code like this but I cant figure out why when I add the couple of lines at the end of the code to replace the digitalwrite for the LEDS with the chaser on the RGB strip, why the disarm part stops working

look at Using millis() for timing. A beginners guide and Several things at the same time

that would give you some hint on how to structure code so that you can blink the leds and listen to the keypad at the same time

Amazingly time-consuming and sadly unreliable way to learn! :face_with_raised_eyebrow:

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