HELP WITH ARDUINO AIRSOFT BOMB CODE FOR FINAL PROJECT

Hello, everyone. I have been struggling to finish off an airsoft bomb I have created. I have created the timer, the keypad and all of the sounds, but I don't know how to get the bomb to defuse and I do not know why. My coding teacher has told me it might be because my delay function is not letting the rest of my code to run, but I am not sure if that is the only problem. If anyone can help revise my code for me or tell me anything that would be greatly appreciated.

How it works so far.

  1. blue light (10) turns on when not planted (before and after defuse or explosion)
  2. type in a random 9 digit code and one it reaches 9 the timer starts.
  3. while the sound is playing the light goes from green(13) to yellow(12) to red. (11)
  4. you should be able to defuse it but my keypad does not work once the bomb has been planted.

BOARD LAYOUT
(once my phone is out of the repair shop I will take a picture and post this but it is basically and keypad for the inputs. then the lights go green, yellow red blue in order from 13, 12, 11, 10. Each has 2 lights in series circuit layout. Out of the (9) output, the sound buzzer is placed.

MY CODE:

#include <Keypad.h>
int const sound = 9;
int delays = 1000;
int duration = 500;

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

byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad

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

char k1 = '';
char k2 = '
';
char k3 = '';
char k4 = '
';
char k5 = '';
char k6 = '
';
char k7 = '';
char k8 = '
';
char k9 = '';
char k10 = '
';

char kk1 = '';
char kk2 = '
';
char kk3 = '';
char kk4 = '
';
char kk5 = '';
char kk6 = '
';
char kk7 = '';
char kk8 = '
';
char kk9 = '';
char kk10 = '
';

int counter = 0;
int counterr = 0;

bool planted = false;
bool planting = false;

bool defused = false;
bool defusing = false;

void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, INPUT);
pinMode(7, INPUT);
pinMode(6, INPUT);
pinMode(5, INPUT);
pinMode(4, INPUT);
pinMode(3, INPUT);
pinMode(2, INPUT);

Serial.begin(9600);
Serial.print("[");
Serial.print((int)keypad.getKey());
Serial.println("]");

}//////////////////////////////////////////////////////////////beeping
void beep() {
if (duration > 335) {
tone(sound, 1500, duration);
duration -= 1;

delays = delays - 4;
///////////////////////////////////////GG
} else {
tone(sound, 1500, 100);
tone(sound,0,125);
tone(sound, 1500, 100);
tone(sound,0,125);
tone(sound, 1500, 100);
tone(sound,0,125);
tone(sound, 1500, 100);
tone(sound,0,800);

}
}
void loop() {
//////////////////////////////////////////////////////////////code to plant
if (planted == false) {
char key = keypad.getKey();
if ((int)key != 0)
{
Serial.print(key);
Serial.print("--> '");
Serial.print((int)key);
Serial.println("'");

if (planted == false && planting == false && key == '') {
planting = true;
tone(sound,291,100);
}
else if (planted == false && planting == true) {
if (key == '
') {
k1 = '';
k2 = '
';
k3 = '';
k4 = '
';
k5 = '';
k6 = '
';
k7 = '';
k8 = '
';
k9 = '_';
counter = 0;

}
else
{
if (counter == 0)
{
k1 = (char)key;
tone(sound,330,100);
}
else if (counter == 1)
{
k2 = (char)key;
tone(sound,365,100);
}
else if (counter == 2)
{
k3 = (char)key;
tone(sound,291,100);
}
else if (counter == 3)
{
k4 = (char)key;
tone(sound,291,100);
}
else if (counter == 4)
{
k5 = (char)key;
tone(sound,365,100);
}
else if (counter == 5)
{
k6 = (char)key;
tone(sound,330,100);
}
else if (counter == 6)
{
k7 = (char)key;
tone(sound,291,100);
}
else if (counter == 7)
{
k8 = (char)key;
tone(sound,330,100);
}
else if (counter == 8)
{
k9 = (char)key;
tone(sound,265,100);
}
else if (counter == 9)
{
k10 = (char)key;
tone(sound,365,100);
}
counter++;
if (counter > 8)
{
planted = true;
}
}
String code = "[";
code = code + k1 + k2 + k3 + k4 + k5 + k6 + k7 + k8 + k9 + ']';
Serial.println(code);

}
}
}
//////////////////////////////////////////////////////////////to defuse the bomb
if (planted == true && duration > 335) {
char key = keypad.getKey();
if ((int)key != 0)
{
Serial.print(key);
Serial.print(" '");
Serial.print((int)key);
Serial.println("'");

if (planted == true && defusing == false && key == '') {
defusing = true;
}
else if (planted == true && defusing == true) {
if (key == '
' || ((counterr = 8 && (k1 != kk1 || k2 != kk2 || k3 != kk3 || k4 != kk4 || k5 != kk5 || k6 != kk6 || k7 != kk7 || k8 != kk8 || k9 != kk9)))) {
kk1 = '\0';
kk2 = '\0';
kk3 = '\0';
kk4 = '\0';
kk5 = '\0';
kk6 = '\0';
kk7 = '\0';
kk8 = '\0';
kk9 = '\0';
counterr = 0;
}
else
{
if (counterr == 0)
{
kk1 = (char)key;
}
else if (counterr == 1)
{
kk2 = (char)key;
}
else if (counterr == 2)
{
kk3 = (char)key;
}
else if (counterr == 3)
{
k4 = (char)key;
}
else if (counterr == 4)
{
kk5 = (char)key;
} else if (counterr == 5)
{
kk6 = (char)key;
} else if (counterr == 6)
{
kk7 = (char)key;
} else if (counterr == 7)
{
kk8 = (char)key;
} else if (counterr == 8)
{
kk9 = (char)key;
} else if (counterr == 9)
{
kk10 = (char)key;
}

counterr++;
if (counterr > 8 && ((k1 == kk1 && k2 == kk2 && k3 == kk3 && k4 == kk4 && k5 == kk5 && k6 == kk6 && k7 == kk7 && k8 == kk8 && k9 == kk9)))
{
planted = false;
}
}
String codee = "[";
codee = codee + k1 + k2 + k3 + k4 + k5 + k6 + k7 + k8 + k9 + ']';
Serial.println(codee);

}
}
}

/////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////sounds
if (planted == true ) {
beep();
digitalWrite(10, LOW);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////lights
if (duration <= 500 - 41.25) {
digitalWrite(13, HIGH);
}
if (duration <= 500 - (2 * 41.25)) {
digitalWrite(12, HIGH);
}
if (duration <= 335 + 41.25) {
digitalWrite(11, HIGH);
}
if (planted == false) {
digitalWrite(10, HIGH);
}
}

I know my code is not very good as I am not an expert coder (Im 14 XD), so I id not use arrays for my simplicity and etc. Thanks for whoever can help me out with this :slight_smile:

Several things. First, learn about arrays and how to use them to simplify your code. Second, read up on the switch-case programming construct. It will also simplify all those if-else blocks. Third, be careful about this kind of error:

if (key == '*' || ((counterr = 8 && (k1 != kk1 || k2 != kk2 || k3 != kk3 || k4 != kk4 || k5 != kk5 || k6 != kk6 || k7 != kk7 || k8 != kk8 || k9 != kk9)))) {

That's an assignment and I think you want a logic test.

Finally, read Nick Gammon's post at the top of the Forum about the proper way to use the Forum, especially the use of code tags. It will increase the amount of help you'll receive.