Programming a scenario arduino bomb (stuck on errors)

dear arduino enthousiasts.

for the last couple of months. I am trying to make a arduino scenario bomb.
it is meant for paintball or airsoft. to make the game more exciting.
I found a example code before. in instructables. however. I thought it could be done better. more cleaner, and less delays. the example code and scematic, can be found here :

(note that I have made a few adjustments. I replaced the LCD, with a I2C LCD

the problem is. with my new code. that it doesnt memorize the disarm code. for example :

  • my disarm code will be ; 12345
  • you have to put 12345 in the correct order.
  • is the code correct? then it will disarm the bomb. thus winning a match.
  • is the code incorrect ? the bomb will “explode”(not going to happen for real!) and thus losing a match.

I dont get the disarm code in a temporaly memory. what is the solution on this code ?
and yes. there are more code lines. because the project uses multiple tabs.

I am getting stuck on this particular tab. its tab is called bombControls.

boolean buzzed = false;
int gameOverCountdown_MAX = 10;
int gameOverCountdown_CURRENT;



void setupBomb() {
  Serial.print("Enter Code: ");
  lcd.setCursor (3,0);
  lcd.print ("enter Code:");

  for (int i = 0; i < 6; i++) {
    password[i] = NO_KEY;
  }

  while (currentLength < 6)
  {
    char key = keypad.getKey();
    key == NO_KEY;
    if (key != NO_KEY)
    {
      if ((key != '*') && (key != '#'))
      {
        Serial.print(key);
        lcd.setCursor (currentLength + 2,1);
        lcd.print (key);
        password[currentLength] = key;
        currentLength++;
        playSound(note_ready, 200);
      }
    }
  }
  if (currentLength == 6) {
    bombState = 1;
    Serial.println("...Ready!");
  }
}

/* The different states of the bomb
   BOMB STANDBY          0
   BOMB READY            1
   ARMED WITHOUT CODE    2
   ARMED WITH CODE       3
   BOMB DISARMED         4
   BOMB EXPLODED         5
   BOMB DAMAGED          6
   BOMB GAMEOVER         7
   */   
void checkBombState() {
    unsigned long currentMillis = millis();
    /**
     * Case 0 - BOMB STANDBY
     */
    if (bombState == 0) {
      setupBomb();
      setupTimer(hours, minutes, seconds);
      gameOverCountdown_CURRENT = gameOverCountdown_MAX;
    }
    /**
     * Case 1 - BOMB READY
     */
    else if (bombState == 1) {
      playSound(note_ready, 250);
      bombState = 2;
      printTime();
    }
    /**
     * Case 2 - ARMED WITHOUT CODE
     */
    else if (bombState == 2 && currentMillis - previousMillis >= interval) {
      // Resets previousMillis
      previousMillis = currentMillis;
      
      playSound(note_counting, 500);
      countDownTick();
      printTime();
      
      key = keypad.getKey();
      key == NO_KEY;
      if (key != NO_KEY) {
      if (key = '*') {
          bombState = 3;
        }
      }
    }
    /**
     * Case 3 - ARMED WITH CODE
     */
    else if (bombState == 3 && currentMillis - previousMillis >= interval) {
      // Resets previousMillis
      previousMillis = currentMillis;
      
      playSound(note_counting, 200);
      countDownTick();
      printTime();
    for (int i = 0; i < 6; i++) {
        entered[i] = NO_KEY;
      }
    
      while (currentLength < 6)
      {
        char key = keypad.getKey();
        key == NO_KEY;
        if (key != NO_KEY)
        {
          if ((key != '*') && (key != '#'))
          {
            entered[currentLength] = key;
            currentLength++;
            playSound(note_ready, 200);
          }
        }
      }

      char key = keypad.getKey(); // get the key

  if (key == '*')
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Enter");
    delay(100);
    lcd.print("Code: ");
    lcd.setCursor(10, 0);
    lcd.print(password[0]);
    lcd.print(password[1]);
    lcd.print(password[2]);
    lcd.print(password[3]);
    lcd.print(password[4]);
    lcd.print(password[5]);
    lcd.noCursor();
    
      /*if (currentLength == 6)
      {
        int nCorrect = 0; // Number of correct keys of entered code
        for (int i = 0; i < 6; i++) {
          Serial.print("Checking code: ");
          Serial.print(i);
          Serial.print("of 6...");
          // Add one if entered code is correct
          Serial.print(entered[i]);
          Serial.print("_");
          Serial.print(password[i]);
          if (entered[i] == password[i]) {
            nCorrect++;
            Serial.println(" CORRECT");*/
            if (currentLength == 6)
    {
      if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3] && entered[4] == password[4] && entered[5] == password[5])
      {
        lcd.noCursor();
        lcd.clear();
        lcd.home();
        lcd.print("Bomb Defused");
        currentLength = 0;
        bombState = 4;
            
          }
          else {
            Serial.println(" FAILED");
            break;
          }
        }

        Serial.print("Code checked: ");
        // If all entered keys are correct - go to bombState 4 - bomb disarmed
        // Else - go to bombState 5 - bomb exploded
        if (nCorrect == 6) {
          buzzed = false;
          bombState = 4;
          Serial.println("CORRECT");
        }
        else {
          buzzed = false;
          bombState = 5;
          Serial.println("FAILED");
        }
      }
    }
    /**
     * Case 4 - BOMB DISARMED
     */
    else if (bombState == 4) {
      if (!buzzed) {
        playSound(note_disarmed, 100);
        Serial.print("bomb disarmed");
        lcd.clear();
        lcd.setCursor(3,0);
        lcd.print("bomb disarmed");
      }
    }
    /**
     * Case 5 - BOMB EXPLODED
     */
    else if (bombState == 5) {
    if (!buzzed) {
        buzzed = true;
        Serial.println("BOMB EXPLODED!");
        lcd.clear();
        lcd.setCursor(2,0);
        lcd.print("BOMB EXPLODED!");
      }
      if (gameOverCountdown_CURRENT <= 0) {
        bombState = 7;
        buzzed = false;
      }
      else {
        playSound(note_exploded, 400);
        gameOverCountdown_CURRENT--;
      }
    }
    /**
     * Case 6 - BOMB DAMAGED
     */
    else if (bombState == 6) {
      if (!buzzed) {
        buzzed = true;
        playSound(note_damaged, 300);
      }
    }
    /**
     * Case 7 - BOMB GAMEOVER
     */
    else if (bombState == 7) {
      if (!buzzed) {
        buzzed = true;
        playSound(note_ready, 100);
        Serial.println("Game over! - Press # to restart");
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Game Over! press");
        delay(1000);
        lcd.setCursor(2,1);
        lcd.print("# to restart");
      }

      key = keypad.getKey();
      key == NO_KEY;
      if (key != NO_KEY)
      {
        if (key = '*')
        {
          Serial.println("RESETTING...");
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("RESETTING");
          playSound(note_ready, 500);
          delay(500);
          resetFunc();
        }
      }
    }

}

//void setBombState() {
//     switch(bombState) {
//      case 0:
//        isRunning = false;
//        break;
//      case 1:
//        isRunning = false;
//        break;
//      case 2:
//        isRunning = false;
//        break;
//      case 3:
//        isRunning = false;
//        break;
//      case 4:
//        isRunning = false;
//        break;
//      case 5:
//        isRunning = false;
//        break;
//      case 6:
//       isRunning = false;
//        break;
//      case 7:
//        ledState = 0;
//        break;
//      default:
//        break;
//    }
//}

void blinkLEDS() {
  switch (bombState) {
    case 2:
      blinkCounting();
      break;
    case 3:
      blinkCounting();
      break;
    case 5:
      blinkExploded();
      break;
    case 6:
      blinkDamaged();
      break;
    default:
      break;
  }
}

much appreciated if you guys can find the solution.

bombControls.ino (6.72 KB)

buzzerControl.ino (177 Bytes)

timerControl.ino (1.91 KB)

paintballBomb_v0.5.ino (1.88 KB)

These lines do nothing an can be removed:

    key == NO_KEY;

The code that sets nCorrect is commented out so this is probably always going to fail:

      if (nCorrect == 6) {
        buzzed = false;
        bombState = 4;
        Serial.println("CORRECT");
      }

okay, I tried your suggestion, but now. an error comes up in the timer control. expected } token at line 97. wich, is a end line. it is already there. did I stumbled upon a bug of the program software ?

I tried your suggestion

. . . but you didn't post the amended code.

]Arduino: 1.6.1 (Windows 8.1), Board:"Arduino Uno"

bombControls.ino: In function 'void checkBombState()':

bombControls.ino:183:5: error: expected '}' before 'else'

bombControls.ino:287:18: error: a function-definition is not allowed here before '{' token

timerControl.ino:99:1: error: expected '}' at end of input

timerControl.ino:99:1: error: expected '}' at end of input

[/code

this is what I get after I adjusted the code. here is the full code, of the bombcontrols tab :
[code]boolean buzzed = false;
int gameOverCountdown_MAX = 10;
int gameOverCountdown_CURRENT;



void setupBomb() {
  Serial.print("Enter Code: ");
  lcd.setCursor (3,0);
  lcd.print ("enter Code:");

  for (int i = 0; i < 6; i++) {
    password[i] = NO_KEY;
  }

  while (currentLength < 6)
  {
    char key = keypad.getKey();
    key == NO_KEY;
    if (key != NO_KEY)
    {
      if ((key != '*') && (key != '#'))
      {
        Serial.print(key);
        lcd.setCursor (currentLength + 2,1);
        lcd.print (key);
        password[currentLength] = key;
        currentLength++;
        playSound(note_ready, 200);
      }
    }
  }
  if (currentLength == 6) {
    bombState = 1;
    Serial.println("...Ready!");
  }
}

/* The different states of the bomb
   BOMB STANDBY          0
   BOMB READY            1
   ARMED WITHOUT CODE    2
   ARMED WITH CODE       3
   BOMB DISARMED         4
   BOMB EXPLODED         5
   BOMB DAMAGED          6
   BOMB GAMEOVER         7
   */   
void checkBombState() {
    unsigned long currentMillis = millis();
    /**
     * Case 0 - BOMB STANDBY
     */
    if (bombState == 0) {
      setupBomb();
      setupTimer(hours, minutes, seconds);
      gameOverCountdown_CURRENT = gameOverCountdown_MAX;
    }
    /**
     * Case 1 - BOMB READY
     */
    else if (bombState == 1) {
      playSound(note_ready, 250);
      bombState = 2;
      printTime();
    }
    /**
     * Case 2 - ARMED WITHOUT CODE
     */
    else if (bombState == 2 && currentMillis - previousMillis >= interval) {
      // Resets previousMillis
      previousMillis = currentMillis;
      
      playSound(note_counting, 500);
      countDownTick();
      printTime();
      
      key = keypad.getKey();
      key == NO_KEY;
      if (key != NO_KEY) {
      if (key = '*') {
          bombState = 3;
        }
      }
    }
    /**
     * Case 3 - ARMED WITH CODE
     */
    else if (bombState == 3 && currentMillis - previousMillis >= interval) {
      // Resets previousMillis
      previousMillis = currentMillis;
      
      playSound(note_counting, 200);
      countDownTick();
      printTime();
    for (int i = 0; i < 6; i++) {
        entered[i] = NO_KEY;
      }
    
      while (currentLength < 6)
      {
        char key = keypad.getKey();
        if (key != NO_KEY)
        {
          if ((key != '*') && (key != '#'))
          {
            entered[currentLength] = key;
            currentLength++;
            playSound(note_ready, 200);
          }
        }
      }

      char key = keypad.getKey(); // get the key

  if (key == '*')
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Enter");
    delay(100);
    lcd.print("Code: ");
    lcd.setCursor(10, 0);
    lcd.print(password[0]);
    lcd.print(password[1]);
    lcd.print(password[2]);
    lcd.print(password[3]);
    lcd.print(password[4]);
    lcd.print(password[5]);
    lcd.noCursor();
    
      if (currentLength == 6)
      {
        int nCorrect = 0; // Number of correct keys of entered code
        for (int i = 0; i < 6; i++) {
          Serial.print("Checking code: ");
          Serial.print(i);
          Serial.print("of 6...");
          // Add one if entered code is correct
          Serial.print(entered[i]);
          Serial.print("_");
          Serial.print(password[i]);
          if (entered[i] == password[i]) {
            nCorrect++;
            Serial.println(" CORRECT");
            
            if (currentLength == 6)
    {
      if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3] && entered[4] == password[4] && entered[5] == password[5])
      {
        lcd.noCursor();
        lcd.clear();
        lcd.home();
        lcd.print("Bomb Defused");
        currentLength = 0;
        bombState = 4;
            
          }
          else {
            Serial.println("FAILED");
            break;
          }
        }

        Serial.print("Code checked: ");
        // If all entered keys are correct - go to bombState 4 - bomb disarmed
        // Else - go to bombState 5 - bomb exploded
        if (nCorrect == 6) {
          buzzed = false;
          bombState = 4;
          Serial.println("CORRECT");
        }
        else {
          buzzed = false;
          bombState = 5;
          Serial.println("FAILED");
        }
      }
    }
    /**
     * Case 4 - BOMB DISARMED
     */
    else if (bombState == 4) {
      if (!buzzed) {
        playSound(note_disarmed, 100);
        Serial.print("bomb disarmed");
        lcd.clear();
        lcd.setCursor(3,0);
        lcd.print("bomb disarmed");
      }
    }
    /**
     * Case 5 - BOMB EXPLODED
     */
    else if (bombState == 5) {
    if (!buzzed) {
        buzzed = true;
        Serial.println("BOMB EXPLODED!");
        lcd.clear();
        lcd.setCursor(2,0);
        lcd.print("BOMB EXPLODED!");
      }
      if (gameOverCountdown_CURRENT <= 0) {
        bombState = 7;
        buzzed = false;
      }
      else {
        playSound(note_exploded, 400);
        gameOverCountdown_CURRENT--;
      }
    }
    /**
     * Case 6 - BOMB DAMAGED
     */
    else if (bombState == 6) {
      if (!buzzed) {
        buzzed = true;
        playSound(note_damaged, 300);
      }
    }
    /**
     * Case 7 - BOMB GAMEOVER
     */
    else if (bombState == 7) {
      if (!buzzed) {
        buzzed = true;
        playSound(note_ready, 100);
        Serial.println("Game over! - Press # to restart");
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Game Over! press");
        delay(1000);
        lcd.setCursor(2,1);
        lcd.print("# to restart");
      }

      key = keypad.getKey();
      key == NO_KEY;
      if (key != NO_KEY)
      {
        if (key = '*')
        {
          Serial.println("RESETTING...");
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("RESETTING");
          playSound(note_ready, 500);
          delay(500);
          resetFunc();
        }
      }
    }

}

//void setBombState() {
//     switch(bombState) {
//      case 0:
//        isRunning = false;
//        break;
//      case 1:
//        isRunning = false;
//        break;
//      case 2:
//        isRunning = false;
//        break;
//      case 3:
//        isRunning = false;
//        break;
//      case 4:
//        isRunning = false;
//        break;
//      case 5:
//        isRunning = false;
//        break;
//      case 6:
//       isRunning = false;
//        break;
//      case 7:
//        ledState = 0;
//        break;
//      default:
//        break;
//    }
//}

void blinkLEDS() {
  switch (bombState) {
    case 2:
      blinkCounting();
      break;
    case 3:
      blinkCounting();
      break;
    case 5:
      blinkExploded();
      break;
    case 6:
      blinkDamaged();
      break;
    default:
      break;
  }
}
bombControls.ino:287:18: error: a function-definition is not allowed here before '{' token
        }
      }
    }

}

That's a bit of clue anyway.

You need to match your { } braces.
Consistent indentation would help a lot.

Hi,
Where is the include LCD library?

Tom.... :slight_smile:

Hi Tom,
this code exists out of multiple tabs. if you are looking for the library. it is in the paintballbomb.0.5 tab

#include <LiquidCrystal.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Tone.h>
#include <Keypad.h>

LiquidCrystal_I2C lcd (0x3f, 16, 2);

int hours = 0; // set hours
int minutes = 0; // set minutes
int seconds = 12; // set seconds

unsigned long previousMillis = 0;
const long interval = 1000;

/*  The different states of the leds
    0 = all leds off
    1 = led1 on
    2 = led2 on
    3 = led3 on
    4 = all leds on  */
int ledState = 0;

/* The different states of the bomb
   BOMB STANDBY          0
   BOMB READY            1
   ARMED WITHOUT CODE    2
   ARMED WITH CODE       3
   BOMB DISARMED         4
   BOMB EXPLODED         5
   BOMB DAMAGED          6
   BOMB GAMEOVER         7   */
int bombState = 0;


/* The different notes for each state of the bomb */
boolean mute = false;
int note_ready = NOTE_G1;
int note_counting = NOTE_C6;
int note_exploded = NOTE_D1;
int note_disarmed = NOTE_A7;
int note_damaged = NOTE_B1;
int note_error = NOTE_A1;

/* Keymapping keypad */
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {5, 6, 7, A2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A1, A0, A3}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char key;
/* Password */
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];

// RESET PROGRAM
void(* resetFunc) (void) = 0; 

/**
 * Setup method
 */
void setup() {
  setupLed();
  setupBuzzer();
  Serial.begin(9600);
  lcd.backlight();
  lcd.clear();
  delay(1000);
  lcd.init();
  lcd.init();
  
}

/**
 * Loop method
 */
void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    checkBombState();
  }

  checkLedState();
  blinkLEDS();
}