Programming Issue-Buzzer

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.

// 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("                      ");
}
}

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.

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

Hello summeranderson

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

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

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?

For extra information and examples look at

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

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

1 Like

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.

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?

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

// Arduino Dice Roller on Wokwi Arduino Simulator

TIA

a7

Hello summeranderson

Consider

#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++.

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.

Your buzzer is a active or passive buzzer?

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.

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

Try this code.



// 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);
    }
  }
}

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



// 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);
    }
  }
}

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

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

Look post #18.