# Programming Issue-Buzzer

**URL:** <https://forum.arduino.cc/t/programming-issue-buzzer/1105835>\
**Category:** Programming\
**Created:** [March 23, 2023, 6:20pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835 "2023-03-23T18:20:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSUOO](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@SSUOO](https://forum.arduino.cc/u/SSUOO)\
**Post date:** [March 23, 2023, 6:20pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/1 "2023-03-23T18:20:44Z")

</div>

Hello,

I am trying to get the buzzer to buzz to match the results of the Dice result on the LCD screen. For example, I hold down the button, the dice runs through numbers 1-6, when i release the button a number will display on the LCD screen at random. The issue I am having is trying to get the buzzer to buzz to match the result on the LCD screen. For example, if i roll a 6, i want the buzzer to buzz 6 times. Then i hold the button and roll again, if i role a 3 i want to buzzer to buzz 3 times and so on. I have the current code so far, can someone help please?

This code rolls the dice (constantly runs through numbers 1-6 whilst pressed) and then displays a random result on the LCD screen.

I have used pin 15 (analogue input) for the buzzer and turned it into an output.

```cpp
// Arduino Dice Roller on Wokwi Arduino Simulator
//Resistors are omitted. wires are hidden for simplicity
#include <LiquidCrystal.h>
#define BUTTON_PIN A0

const byte die1Pins[] = { 2, 3, 4, 5, 6, 7, 8};
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 13, d7 = 1;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
int buzzer = 15;

void setup() {

lcd.begin(16,2);

lcd.setCursor(3,0);
lcd.print("Welcome to");
lcd.setCursor(2,1);
lcd.print("The Dice Game!");
delay(2000);
lcd.clear();

lcd.setCursor(0,0);
lcd.print("Hold the button");
lcd.setCursor(0,1);
lcd.print("to roll the dice!");
delay(2000);
lcd.clear();

pinMode(A0, INPUT_PULLUP);
pinMode (buzzer, OUTPUT);
for (byte i = 0; i < 7; i++) {
pinMode(die1Pins[i], OUTPUT);
}

}

void displayNumber(const byte pins[], byte number) {
digitalWrite(pins[0], number > 1 ? HIGH : LOW); // top-left
digitalWrite(pins[1], number > 3 ? HIGH : LOW); // top-right
digitalWrite(pins[2], number == 6 ? HIGH : LOW); // middle-left
digitalWrite(pins[3], number % 2 == 1 ? HIGH : LOW); // center
digitalWrite(pins[4], number == 6 ? HIGH : LOW); // middle-right
digitalWrite(pins[5], number > 3 ? HIGH : LOW); // bottom-left
digitalWrite(pins[6], number > 1 ? HIGH : LOW); // bottom-right
}

bool randomReady = false;

void loop(){

bool buttonPressed = digitalRead(BUTTON_PIN) == LOW;
bool buttonNotPressed = digitalRead(BUTTON_PIN) == HIGH;

if(!randomReady && buttonPressed) {

// to initialize the random number generator
randomSeed(micros());
randomReady = true;
}

if(buttonPressed) {
for (byte i = 0; i < 10; i++) {
int num1 = random(1, 7);
displayNumber(die1Pins, num1);
delay(50 + i * 20);
lcd.setCursor(4,0);
lcd.print("Rolling!");
lcd.setCursor(7,1);
lcd.print(num1);

  }
}

if(buttonNotPressed && randomReady) {
lcd.setCursor(4,0);
lcd.print("!Winner!");
delay(750);
lcd.setCursor(4,0);
lcd.print(" ");
lcd.setCursor(4,0);
lcd.print("!Winner!");
delay(750);
lcd.setCursor(4,0);
lcd.print(" ");

  lcd.setCursor(2,0);
  lcd.print("!Roll Again!");
  delay(1000);
  lcd.setCursor(0,0);
  lcd.print(" ");
}
}

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [March 23, 2023, 6:22pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/2 "2023-03-23T18:22:52Z")

</div>

please edit your post, select the code part and press the \</\> icon in the tool bar to mark it as code. It's not readable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)

* * *

Note that I locked your other exactly similar post. Please don't double post again in the future.

---

<div class="post-metadata">

**Author:** ![FernandoGarcia](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/fernandogarcia/32/214937_2.png) [@FernandoGarcia](https://forum.arduino.cc/u/FernandoGarcia)\
**Post date:** [March 23, 2023, 8:02pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/3 "2023-03-23T20:02:48Z")

</div>

You can't use pins 0 and 1 as output or input while using serial monitor.

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [March 23, 2023, 8:22pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/4 "2023-03-23T20:22:19Z")

</div>

Hello [summeranderson](https://forum.arduino.cc/u/summeranderson)

Design and code a event controlled timer based on the milli() function.

Have a nice day and enjoy coding in C++.

---

<div class="post-metadata">

**Author:** ![SSUOO](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@SSUOO](https://forum.arduino.cc/u/SSUOO)\
**Post date:** [March 23, 2023, 8:34pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/5 "2023-03-23T20:34:12Z")

</div>

Hi Paul, thanks for the reply.

I have never used one of those before, is there any chance you could demonstrate please if you have time?

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [March 23, 2023, 8:36pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/6 "2023-03-23T20:36:07Z")

</div>

For extra information and examples look at

- [Using millis() for timing. A beginners guide](https://forum.arduino.cc/index.php?topic=503368.0)
- [Several things at the same time](https://forum.arduino.cc/index.php?topic=223286.0)
- [Flashing multiple LEDs at the same time](https://forum.arduino.cc/t/flashing-multiple-leds-at-the-same-time/1065564)

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [March 23, 2023, 9:05pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/7 "2023-03-23T21:05:35Z")

</div>

> [@SSUOO](#):
>
> `const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 13, d7 = 1;`

Here you using pin 1 for LCD, pin 1 is part of serial.  
Use a pin like A2 to A4.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [March 23, 2023, 9:11pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/8 "2023-03-23T21:11:30Z")

</div>

> [@ruilviana](#):
>
> Here you using pin 1 for LCD, pin 1 is part of serial.

but he is not using Serial.begin() so it should be fine

---

<div class="post-metadata">

**Author:** ![SSUOO](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@SSUOO](https://forum.arduino.cc/u/SSUOO)\
**Post date:** [March 23, 2023, 9:13pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/9 "2023-03-23T21:13:33Z")

</div>

I am not having issues with the LCD, this is working. I cannot seem to get the buzzer to replicate the amount shown on the LCD screen. For Example, if the number is 5, the buzzer should buzz 5 times.

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [March 23, 2023, 9:19pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/10 "2023-03-23T21:19:34Z")

</div>

Hi,  
Do you want it to play a single sequence or keep playing the winning value several times?  
I'm gonna explain.  
If the winning number is 3, do you want the buzzer to ring 3 times and silence, or do you want it to ring 3 times, wait a while, ring 3 times, and so on until the button is pressed again?

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 23, 2023, 9:21pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/11 "2023-03-23T21:21:46Z")

</div>

@SSUOO can you supply a link to the actual sim you are working off?

```cpp
// Arduino Dice Roller on Wokwi Arduino Simulator

```

TIA

a7

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [March 23, 2023, 9:25pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/12 "2023-03-23T21:25:37Z")

</div>

Hello [summeranderson](https://forum.arduino.cc/u/summeranderson)

Consider

```cpp
#define usl unsigned long
constexpr int BuzzerPin {9};
struct BUZZERTIMER
{
  int buzzerPin;
  usl timeNow;
  usl timeSpan;
  int onOff;
} buzzerTimer {BuzzerPin, 0, 250, false};

void setup()
{
  pinMode(buzzerTimer.buzzerPin, OUTPUT);
  Serial.begin(9600);
  Serial.println(F("insert number of events {1 - 9}"));
}

void loop()
{
  // make test case
  if (Serial.available() > 0)
  {
    char readChar = Serial.read();
    switch (readChar)
    {
      case '0' ... '9':
        // start event timer
        buzzerTimer.onOff = (readChar & 0x0f) << 1;
        buzzerTimer.timeNow = millis();
        break;
    }
  }
  // run event timer
  if (millis() - buzzerTimer.timeNow >= buzzerTimer.timeSpan && buzzerTimer.onOff!=0)
  {
    buzzerTimer.timeNow=millis();
    buzzerTimer.onOff--;
    digitalWrite(buzzerTimer.buzzerPin, digitalRead(buzzerTimer.buzzerPin) == HIGH ? LOW : HIGH);
  }
}

```

Have a nice day and enjoy coding in C++.

---

<div class="post-metadata">

**Author:** ![SSUOO](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@SSUOO](https://forum.arduino.cc/u/SSUOO)\
**Post date:** [March 23, 2023, 9:27pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/13 "2023-03-23T21:27:54Z")

</div>

Hi,

I want it to ring 3 times and then silence. Same for the other numbers, if it is 6, ring 6 times and then silence.

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [March 23, 2023, 9:32pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/14 "2023-03-23T21:32:11Z")

</div>

Your buzzer is a active or passive buzzer?

---

<div class="post-metadata">

**Author:** ![SSUOO](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@SSUOO](https://forum.arduino.cc/u/SSUOO)\
**Post date:** [March 23, 2023, 9:33pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/15 "2023-03-23T21:33:01Z")

</div>

Hello, i have attached the link below, the blue LED i am using as the buzzer as this software does not have sound. So i'm using the blue LED just to check it works.

> **[Dice roller Ass2-Buzzer - Wokwi Arduino and ESP32 Simulator](https://wokwi.com/projects/360016265123479553)**
>
> Run IoT and embedded projects in your browser: ESP32, Arduino, Pi Pico, and more. No installation required!

---

<div class="post-metadata">

**Author:** ![SSUOO](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@SSUOO](https://forum.arduino.cc/u/SSUOO)\
**Post date:** [March 23, 2023, 9:33pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/16 "2023-03-23T21:33:47Z")

</div>

Hello,

I have attached a link

the blue LED i am using as the buzzer as this software does not have sound. So i'm using the blue LED just to check it works

> **[Dice roller Ass2-Buzzer - Wokwi Arduino and ESP32 Simulator](https://wokwi.com/projects/360016265123479553)**
>
> Run IoT and embedded projects in your browser: ESP32, Arduino, Pi Pico, and more. No installation required!

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [March 23, 2023, 9:38pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/17 "2023-03-23T21:38:28Z")

</div>

Try this code.

```cpp

// Arduino Dice Roller on Wokwi Arduino Simulator
//Resistors are omitted. wires are hidden for simplicity
#include <LiquidCrystal.h>
#define BUTTON_PIN A0

const byte die1Pins[] = { 2, 3, 4, 5, 6, 7, 8};
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 13, d7 = 1;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int buzzer = 15;

int tone_On = 0;
int num1;
void setup() {

  lcd.begin(16, 2);

  lcd.setCursor(3, 0);
  lcd.print("Welcome to");
  lcd.setCursor(2, 1);
  lcd.print("The Dice Game!");
  delay(2000);
  lcd.clear();

  lcd.setCursor(0, 0);
  lcd.print("Hold the button");
  lcd.setCursor(0, 1);
  lcd.print("to roll the dice!");
  delay(2000);
  lcd.clear();

  pinMode(A0, INPUT_PULLUP);
  pinMode (buzzer, OUTPUT);
  for (byte i = 0; i < 7; i++) {
    pinMode(die1Pins[i], OUTPUT);
  }

}

void displayNumber(const byte pins[], byte number) {
  digitalWrite(pins[0], number > 1 ? HIGH : LOW); // top-left
  digitalWrite(pins[1], number > 3 ? HIGH : LOW); // top-right
  digitalWrite(pins[2], number == 6 ? HIGH : LOW); // middle-left
  digitalWrite(pins[3], number % 2 == 1 ? HIGH : LOW); // center
  digitalWrite(pins[4], number == 6 ? HIGH : LOW); // middle-right
  digitalWrite(pins[5], number > 3 ? HIGH : LOW); // bottom-left
  digitalWrite(pins[6], number > 1 ? HIGH : LOW); // bottom-right
}

bool randomReady = false;

void loop() {

  bool buttonPressed = digitalRead(BUTTON_PIN) == LOW;
  bool buttonNotPressed = digitalRead(BUTTON_PIN) == HIGH;

  if (!randomReady && buttonPressed) {

    // to initialize the random number generator
    randomSeed(micros());
    randomReady = true;
  }

  if (buttonPressed) {
    for (byte i = 0; i < 10; i++) {
      num1 = random(1, 7);
      displayNumber(die1Pins, num1);
      delay(50 + i * 20);
      lcd.setCursor(4, 0);
      lcd.print("Rolling!");
      lcd.setCursor(7, 1);
      lcd.print(num1);
    }
  }

  if (buttonNotPressed && randomReady) {
    lcd.setCursor(4, 0);
    lcd.print("!Winner!");
    tone_On++;
    delay(750);
    lcd.setCursor(4, 0);
    lcd.print(" ");
    lcd.setCursor(4, 0);
    lcd.print("!Winner!");
    delay(750);
    lcd.setCursor(4, 0);
    lcd.print(" ");

    lcd.setCursor(2, 0);
    lcd.print("!Roll Again!");
    delay(1000);
    lcd.setCursor(0, 0);
    lcd.print(" ");
  }
  if (tone_On == 1)
  {
    for (int i = 0; i < num1; i++)
    {
      tone(buzzer, 1500);
      delay(500);
      noTone(buzzer);
      delay(500);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [March 23, 2023, 9:44pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/18 "2023-03-23T21:44:07Z")

</div>

Hi,  
I made a mistake but below it is corrected.

```cpp

// Arduino Dice Roller on Wokwi Arduino Simulator
//Resistors are omitted. wires are hidden for simplicity
#include <LiquidCrystal.h>
#define BUTTON_PIN A0

const byte die1Pins[] = { 2, 3, 4, 5, 6, 7, 8};
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 13, d7 = 1;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int buzzer = 15;

int tone_On = 0;
int num1;
void setup() {

  lcd.begin(16, 2);

  lcd.setCursor(3, 0);
  lcd.print("Welcome to");
  lcd.setCursor(2, 1);
  lcd.print("The Dice Game!");
  delay(2000);
  lcd.clear();

  lcd.setCursor(0, 0);
  lcd.print("Hold the button");
  lcd.setCursor(0, 1);
  lcd.print("to roll the dice!");
  delay(2000);
  lcd.clear();

  pinMode(A0, INPUT_PULLUP);
  pinMode (buzzer, OUTPUT);
  for (byte i = 0; i < 7; i++) {
    pinMode(die1Pins[i], OUTPUT);
  }

}

void displayNumber(const byte pins[], byte number) {
  digitalWrite(pins[0], number > 1 ? HIGH : LOW); // top-left
  digitalWrite(pins[1], number > 3 ? HIGH : LOW); // top-right
  digitalWrite(pins[2], number == 6 ? HIGH : LOW); // middle-left
  digitalWrite(pins[3], number % 2 == 1 ? HIGH : LOW); // center
  digitalWrite(pins[4], number == 6 ? HIGH : LOW); // middle-right
  digitalWrite(pins[5], number > 3 ? HIGH : LOW); // bottom-left
  digitalWrite(pins[6], number > 1 ? HIGH : LOW); // bottom-right
}

bool randomReady = false;

void loop() {

  bool buttonPressed = digitalRead(BUTTON_PIN) == LOW;
  bool buttonNotPressed = digitalRead(BUTTON_PIN) == HIGH;

  if (!randomReady && buttonPressed) {

    // to initialize the random number generator
    randomSeed(micros());
    randomReady = true;
  }

  if (buttonPressed) {
    tone_On = 0;
    for (byte i = 0; i < 10; i++) {
      num1 = random(1, 7);
      displayNumber(die1Pins, num1);
      delay(50 + i * 20);
      lcd.setCursor(4, 0);
      lcd.print("Rolling!");
      lcd.setCursor(7, 1);
      lcd.print(num1);
    }
  }

  if (buttonNotPressed && randomReady) {
    lcd.setCursor(4, 0);
    lcd.print("!Winner!");
    tone_On++;
    delay(750);
    lcd.setCursor(4, 0);
    lcd.print(" ");
    lcd.setCursor(4, 0);
    lcd.print("!Winner!");
    delay(750);
    lcd.setCursor(4, 0);
    lcd.print(" ");

    lcd.setCursor(2, 0);
    lcd.print("!Roll Again!");
    delay(1000);
    lcd.setCursor(0, 0);
    lcd.print(" ");
  }
  if (tone_On == 1)
  {
    for (int i = 0; i < num1; i++)
    {
      tone(buzzer, 1500);
      delay(500);
      noTone(buzzer);
      delay(500);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![SSUOO](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@SSUOO](https://forum.arduino.cc/u/SSUOO)\
**Post date:** [March 23, 2023, 9:45pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/19 "2023-03-23T21:45:44Z")

</div>

Hi, thank you for getting back to me. I have tried this code and this only seems to work the first time? If i want to re roll the dice a re-hold the button and a different number will display on the screen, however the LED (buzzer) does not replicate the new number.

The dice can be re rolled an infinite number of times, the LED (buzzer) needs to also do the same

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [March 23, 2023, 9:47pm UTC](https://forum.arduino.cc/t/programming-issue-buzzer/1105835/20 "2023-03-23T21:47:28Z")

</div>

Hi,  
Yes, I saw this problem and fixed it in the new version I posted.

Look post #18.

[Next page](https://forum.arduino.cc/t/programming-issue-buzzer/1105835.md?page=2)
