Dont know where to put the reaping beep sound

Hi iim trying to make a fake csgo Bomb planting and defusing code and i added the code to make the double beeping sound with this code :

        int i=0;
  while (i < 20)
  {
       tone(buzzerPin, NOTE_G5, 100);
      delay(100);
      tone(buzzerPin, NOTE_G5, 100);
    i++;
    delay(800);
  }

and i dont know where to put it i tried many places but all of them got stuck in the place i put it like when i put in here :

 if (enteredCode > 0) {
      armingCode = enteredCode; 
      lcd.clear();
      lcd.print("Bomb is planted!");
      bombPlanted = true;
    }

i was just stuck in this code for 20 beeps and then it went to the next section
here is my code :


#include <LiquidCrystal.h>
#include <Keypad.h>
#include <pitches.h>

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const byte ROWS = 4;
const byte COLS = 4;

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};
byte colPins[COLS] = {A4, A5, A6, A7};

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

const int buzzerPin = 8; 

int armingCode = 9023; 
int defuseCode = 1290; 
bool bombPlanted = false;
bool isBeeping = false;
unsigned long lastBeepTime = 0;


void setup() {
  lcd.begin(16, 2);
  pinMode(buzzerPin, OUTPUT);
  lcd.print("Welcome to CS:Arduino");
  delay(2000);
  lcd.clear();
}

void loop() {
  if (!bombPlanted) {
    lcd.setCursor(0, 0);
    lcd.print("Enter planting code:");
    int enteredCode = 0;
    int position = 0;
    while (position < 4) {
      char key = keypad.getKey();
      if (key) {
        lcd.setCursor(position, 1);
        lcd.print('*');
        enteredCode = enteredCode * 10 + (key - '0');
        position++;
      }
    }

    if (enteredCode > 0) {
      armingCode = enteredCode; 
      lcd.clear();
      lcd.print("Bomb is planted!");
      bombPlanted = true;
    }
      
    } else {
      lcd.clear();
      lcd.print("Invalid code!");
      delay(2000);
      lcd.clear();
    }
  } else {
    lcd.setCursor(0, 0);
    lcd.print("Enter defuse code:");
    int enteredCode = 0;
    int position = 0;
 

    while (position < 4) {
      char key = keypad.getKey();
      if (key) {
        lcd.setCursor(position, 1);
        lcd.print('*');
        enteredCode = enteredCode * 10 + (key - '0');
        position++;
        tone(buzzerPin, NOTE_E3, 100);
     
      }
    }

    if (enteredCode == armingCode) {
      lcd.clear();
      lcd.print("Bomb defused!");
      noTone(buzzerPin);
      bombPlanted = false;
      delay(2000);
      lcd.clear();
      tone(buzzerPin, NOTE_E3, 100);
      delay(100);
      tone(buzzerPin, NOTE_E3, 100);
    } else {
      lcd.clear();
      lcd.print("Wrong code!");
      delay(1000);
      lcd.clear();
      
      }
    }
  }

Post an example of the code that "gets stuck".

Incidentally, 20 seconds of nothing but beeping can seem like a really long time.

You will get that using delay() - because delay() just stops the processor from doing anything else at all.

If you want to be able to do other things while the beeps are going on, you need to adopt a different strategy for your beep timing.

See:

https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay

It's available as an example included in the IDE:

1 Like

You do know that C/C++ has a for() loop, yes?

  for (int i=0; i < 20; i++)
  {
    tone(buzzerPin, NOTE_G5, 100);
    delay(100);
    tone(buzzerPin, NOTE_G5, 100);
    delay(800);
  }
1 Like

so where should i put the for() loop in?

Put it in the section of the code where you want nothing but beeping to happen for 20 seconds.

1 Like

When do you want the double-beep?
The double-beep works well immediately after this:

      lcd.print("Bomb is planted!");

Your code also beeps here:

  1. Every key press during defusing
  2. At the second (ad inf) time entering the Plant Code

Concerning your code... you added a close brace ( } ) ... (or partially deleted something) probably here...

    if (enteredCode > 0) {
      armingCode = enteredCode; 
      lcd.clear();
      lcd.print("Bomb is planted!");
      bombPlanted = true;
    }     //  <------------------------------------ THIS BRACE SHOULD NOT TO BE HERE

    } else {
      lcd.clear();
      lcd.print("Invalid code!");
      delay(2000);
      lcd.clear();
    }

I could not find a reference why A6 and A7 might not work as analog input... so I used two digital pins...

byte colPins[COLS] = {A4, A5, 10, 9};
// byte colPins[COLS] = {A4, A5, A6, A7};

Your screen is only 16 characters wide, so this...

lcd.print("Welcome to CS:Arduino");

... should be split on two lines, like this...

  lcd.setCursor(0,0);
  lcd.print("Welcome to");
  lcd.setCursor(6,1);
  lcd.print("CS:Arduino");

And this...

    lcd.setCursor(0, 0);
    lcd.print("Enter planting code:");

... should be something like this...

    lcd.setCursor(0, 0);
    lcd.print("Enter planting");
    lcd.setCursor(10, 1);
    lcd.print("code:");

...because the bottom LCD line will be for entering code

    int position = 0;
    while (position < 4) {

And this...

    lcd.setCursor(0, 0);
    lcd.print("Enter defuse code:");

... should be split like this...

    lcd.setCursor(0, 0);
    lcd.print("Enter defusing");
    lcd.setCursor(10, 1);
    lcd.print("code:");

And this means you can not use 0000 or any combination containing only 0, * and #

 if (enteredCode > 0) {
1 Like

no i mean where should i put it to make the sound and ask for defuse code

The Arduino executes the code in order, one line at a time.

Your code should be written so that the actions follow one another in the order you wish.

Thanks it worked !

1 Like

Excellent!

You can simulate your project and make adjustment and additions using WOKWI.COM.
Your code in wokwi works here:

#include <LiquidCrystal.h>
#include <Keypad.h>
#include "pitches.h"
// #include <pitches.h>

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const byte ROWS = 4;
const byte COLS = 4;

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};
byte colPins[COLS] = {A4, A5, 10, 9};
// byte colPins[COLS] = {A4, A5, A6, A7};

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

const int buzzerPin = 8;

int armingCode = 9023;
int defuseCode = 1290;
bool bombPlanted = false;
bool isBeeping = false;
unsigned long lastBeepTime = 0;


void setup() {
  lcd.begin(16, 2);
  pinMode(buzzerPin, OUTPUT);
  lcd.setCursor(0, 0);
  lcd.print("Welcome to");
  lcd.setCursor(6, 1);
  lcd.print("CS:Arduino");
  delay(2000);
  lcd.clear();
}

void loop() {
  if (!bombPlanted) {
    lcd.setCursor(0, 0);
    lcd.print("Enter planting");
    lcd.setCursor(10, 1);
    lcd.print("code:");
    int enteredCode = 0;
    int position = 0;
    while (position < 4) {
      char key = keypad.getKey();
      if (key) {
        lcd.setCursor(position, 1);
        lcd.print('*');
        enteredCode = enteredCode * 10 + (key - '0');
        position++;
      }
    }

    if (enteredCode > 0) {
      armingCode = enteredCode;
      lcd.clear();
      lcd.print("Bomb is planted!");

      int i = 0;
      while (i < 5) {
        tone(buzzerPin, NOTE_G5, 100);
        delay(200);
        tone(buzzerPin, NOTE_G5, 100);
        i++;
        delay(500);
      }

      lcd.clear();
      bombPlanted = true;
      // } // <--------------------------REMOVE THIS CLOSE BRACE

    } else {
      lcd.clear();
      lcd.print("Invalid code!");
      delay(2000);
      lcd.clear();
    }
  } else {
    lcd.setCursor(0, 0);
    lcd.print("Enter defusing");
    lcd.setCursor(10, 1);
    lcd.print("code:");
    int enteredCode = 0;
    int position = 0;


    while (position < 4) {
      char key = keypad.getKey();
      if (key) {
        lcd.setCursor(position, 1);
        lcd.print('*');
        enteredCode = enteredCode * 10 + (key - '0');
        position++;
        tone(buzzerPin, NOTE_E3, 100);

      }
    }

    if (enteredCode == armingCode) {
      lcd.clear();
      lcd.print("Bomb defused!");
      noTone(buzzerPin);
      bombPlanted = false;
      delay(2000);
      lcd.clear();
      tone(buzzerPin, NOTE_E3, 100);
      delay(100);
      tone(buzzerPin, NOTE_E3, 100);
    } else {
      lcd.clear();
      lcd.print("Wrong code!");
      delay(1000);
      lcd.clear();

    }
  }
}

diagram.json for wokwi.com

{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
    { "type": "wokwi-lcd1602", "id": "lcd1", "top": -188.57, "left": -41.6, "attrs": {} },
    {
      "type": "wokwi-buzzer",
      "id": "bz1",
      "top": -50.7,
      "left": 186.9,
      "rotate": 90,
      "attrs": { "volume": "0.1" }
    },
    { "type": "wokwi-membrane-keypad", "id": "keypad1", "top": -194, "left": -320.8, "attrs": {} }
  ],
  "connections": [
    [ "nano:5V", "lcd1:VDD", "red", [ "v14.4", "h-134.9" ] ],
    [ "nano:GND.1", "lcd1:VSS", "black", [ "v24", "h-192.5" ] ],
    [ "lcd1:RS", "nano:12", "green", [ "v0" ] ],
    [ "lcd1:E", "nano:11", "green", [ "v28.8", "h-9.6" ] ],
    [ "nano:GND.2", "lcd1:K", "black", [ "v-24", "h9.1" ] ],
    [ "nano:5V", "lcd1:A", "red", [ "v14.4", "h47.5", "v-105.6", "h-47.9" ] ],
    [ "lcd1:D7", "nano:2", "green", [ "v0" ] ],
    [ "lcd1:D6", "nano:3", "green", [ "v0" ] ],
    [ "lcd1:D5", "nano:4", "green", [ "v0" ] ],
    [ "lcd1:D4", "nano:5", "green", [ "v0" ] ],
    [ "keypad1:R1", "nano:A0", "green", [ "v19.2", "h278.4" ] ],
    [ "keypad1:R2", "nano:A1", "green", [ "v28.8", "h268.9" ] ],
    [ "keypad1:R3", "nano:A2", "green", [ "v38.4", "h287.7" ] ],
    [ "keypad1:R4", "nano:A3", "green", [ "v48", "h268.6" ] ],
    [ "keypad1:C1", "nano:A4", "green", [ "v57.6", "h268.7" ] ],
    [ "keypad1:C2", "nano:A5", "green", [ "v67.2", "h278.4" ] ],
    [ "nano:GND.2", "bz1:1", "black", [ "v0" ] ],
    [ "bz1:2", "nano:8", "green", [ "h0" ] ],
    [ "keypad1:C3", "nano:10", "green", [ "v76.8", "h201.45" ] ],
    [ "keypad1:C4", "nano:9", "green", [ "v86.4", "h201.3" ] ]
  ],
  "dependencies": {}
}

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