Buzzer doesn't create any sound

I put this code in both real Arduino and online simulation and sound doesn't work for both, although code compiles with no problem

#include <IRremote.h>

// CONFIG: MODIFY VALUES TO FIT PLAY STYLE
const int maxAmmo = 5; // maximum ammunition count (max is 9)
const int shootCooldown = 500; // interval between each shot (in ms)
const int reloadTime = 1600; // time it takes to reload the gun (in ms)
const int resetTime = 500; // time the reset button has to be pressed to reset (in ms)

byte triggerPin = 2;
byte reloadPin = 3;
byte resetPin = 13;
byte irPin = 9;
byte hitLedPin = 6;
byte buzzerPin = 7;
byte pinsSegments[7] = {A0, A1, A2, A3, A4, A5, 12};

//ir communication
IRsend irsend; // setup IR transmitter
const unsigned long irSendHex = 0xFFA25D; // define hex value to send

// arrays of digits for 7 segment display
const bool digits[10][7] = {
  {0, 0, 0, 0, 0, 0, 1},
  {1, 0, 0, 1, 1, 1, 1},
  {0, 0, 1, 0, 0, 1, 0},
  {0, 0, 0, 0, 1, 1, 0},
  {1, 0, 0, 1, 1, 0, 0},
  {0, 1, 0, 0, 1, 0, 0},
  {0, 1, 0, 0, 0, 0, 0},
  {0, 0, 0, 1, 1, 1, 1},
  {0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 1, 0, 0}
};

// reload animation on 7 segment display
const bool reloadAnimation[6][7] = {
  {0, 1, 1, 1, 1, 1, 1},
  {1, 0, 1, 1, 1, 1, 1},
  {1, 1, 0, 1, 1, 1, 1},
  {1, 1, 1, 0, 1, 1, 1},
  {1, 1, 1, 1, 0, 1, 1},
  {1, 1, 1, 1, 1, 0, 1}
};
const int animationLength = 6;
const int animationInterval = 100;

const int melodyLength = 2;
const int melodyInterval = 150;
// shoot sound effect
const int shootSoundStartTone = 1000;
const int shootSoundEndTone = 700;
const int shootSoundInterval = 1;

//other variables
int ammoCount = maxAmmo;
unsigned long lastShot = shootCooldown*-1;
unsigned long startedReload = reloadTime*-1;
bool reloading = false;
unsigned long startedResetting = 0;
bool resetting = false;
unsigned long millisStartedTone = 0;
bool playingMelody = false;
int currentToneIdx = 0;
bool playingShootSound = false;
unsigned long millisStartedFrame = 0;
int currentFrameIdx = 0;
bool hitIndicator = false;

// reset function
void(* reset) (void) = 0;

// function to display digits on the 7 segment display
void displaySegments(bool segments[7]) {
  for(byte seg=0; seg<7; seg+=1) {
    digitalWrite(pinsSegments[seg], segments[seg]);
  }
}

void setup()
{
  // setup pins
  pinMode(triggerPin, INPUT_PULLUP);
  pinMode(reloadPin, INPUT_PULLUP);
  pinMode(resetPin, INPUT_PULLUP);
  pinMode(irPin, OUTPUT);
  pinMode(hitLedPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  for(byte i=0; i<7; i+=1) {
    pinMode(pinsSegments[i], OUTPUT);
  }

  // GAME SETUP
  displaySegments(digits[ammoCount]); // display ammo count
  digitalWrite(hitLedPin, HIGH); // turn on hit indicator LED

   // play startup sound effect
  int startTones[] = { 196, 262, 330, 392 };
  for(int i=0; i<4; i++)
  {
    tone(buzzerPin, startTones[i]);
    delay(200);
  }
  noTone(buzzerPin);
}

void loop()
{
  unsigned long currentMillis = millis();
    // display ammo count
  if(!reloading) {
    displaySegments(digits[ammoCount]);
  }
   // play sounds
  if (playingMelody) {
    if (currentMillis - millisStartedTone >= melodyInterval) { // we are playing a melody, check if we finished playing current tone
      if(currentToneIdx+1 == melodyLength){ // we finished playing a tone, check if we finished the melody
        // end of melody
        noTone(buzzerPin);
        playingMelody = false;
} else { // we haven't finished the melody
        // play next tone
        currentToneIdx++;
        millisStartedTone = currentMillis;
      }
    }
  } else if (playingShootSound) {
    if (currentMillis - millisStartedTone >= shootSoundInterval) {
      if(currentToneIdx <= shootSoundEndTone) {
        // end of melody
        noTone(buzzerPin);
        playingShootSound = false;
           
      } else {
        // play next tone
        currentToneIdx -= 1;
        millisStartedTone = currentMillis;
        tone(buzzerPin, currentToneIdx);
      }
    }
  }
 // play reload animation
  if (reloading) {
    
    if (currentMillis - millisStartedFrame >= animationInterval) { // we are playing reload animation, check if we finished playing current frame
      // play next frame
      currentFrameIdx++;
      // cycle to start if reached end of animation
      if (currentFrameIdx > animationLength-1) {
        currentFrameIdx = 0;
      }
      millisStartedFrame = currentMillis;
      displaySegments(reloadAnimation[currentFrameIdx]);
    }
  }
 // input: reset
  if (digitalRead(resetPin) == LOW) {
    if(resetting) { // check if we were already holding the reset button
      if(currentMillis - startedResetting >= resetTime) { // check if we held the reset button for enough time
        reset();
      }
    } else { // start a new timer when we press it
      startedResetting = currentMillis;
      resetting = true;
    }
  } else {
    resetting = false;
  }
  // input: shoot
  if (digitalRead(triggerPin) == LOW && ammoCount > 0 && !reloading && currentMillis - lastShot >= shootCooldown)
  {
    // SHOOT
    irsend.sendNEC(irSendHex, 32); // send IR
    
    ammoCount--;
    lastShot = currentMillis;
    
    // SHOOT SFX
    currentToneIdx = shootSoundStartTone;
    millisStartedTone = currentMillis;
    tone(buzzerPin, currentToneIdx);
    if(playingMelody) {
      playingMelody = false;
    }
    playingShootSound = true;
  }

  // input: reload
  if (digitalRead(reloadPin) == LOW && ammoCount < maxAmmo && !reloading) {
    reloading = true;
    startedReload = currentMillis;
    
    // RELOAD ANIMATION
    currentFrameIdx = 0;
    millisStartedFrame = currentMillis;
    displaySegments(reloadAnimation[currentFrameIdx]);
  }
  
  // reload gun
  if (reloading && currentMillis - startedReload >= reloadTime) {
    ammoCount = maxAmmo;
    reloading = false;
  }
}

How did you fix the problem with the bool arrays? Your other topic is still open and forum members are trying to help on that topic.

Sorry, I'm in rush, that's why I ask so many question at the same time. About bool arrays, it does work in arduino IDE, if I connect it (although it's weird, e.g., reload doesn't stop). But I can't use my code in tinkercard, it shows error that I sent in previous topic. By now I'd say that I'm more concerned about the fact that a buzzer doesn't work anywhere, although code compiles without any problem, so I can't find error by myself

And a third topic on the same subject.

OP copied a GitHub project, copy/pasted from other random programs, dumped it here and has no intention of learning how to fix any of them (all of them, because they are all the same subject).

Step 1. Stop starting new topics with the same subject.
Step 2. Ask questions about ONE problem.
Step 3. Using the answers, LEARN how YOU fix this one problem.
Step 4. Repeat Steps 2 and 3 until you have a working project.

You used NONE of my help before, so you will use none of my help ever.

Read my other posts, I actually don't have time to submit this project, please help me with this. For me sound thing is different problem, because I DID SOLVE my previous problems, using your tips. As I mentioned before, I have no idea why sound is not working as there is no errors in code.

I do.

So at least give me a hint of what's wrong, you don't have to write the complete solution for the problem

Hi,
Have you got some code that JUST makes the buzzer turn ON and OFF?
To prove you have the buzzer connected properly.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
PLEASE NO FRITZY cut and paste.

If not, then do that now and see if it works.

Tom... :smiley: :+1: :coffee: :australia:

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